简体   繁体   English

什么静态编译器可以优化JIT不能?

[英]what static compiler could optimize when JIT could not ?

Are there examples of what static compiler could optimize when JIT could not ? 有什么静态编译器可以在JIT无法优化的例子吗?

For example, some optimization of C++ compiler which could not be done by .NET JIT ? 例如,.NET JIT无法完成C ++编译器的一些优化?

None. 没有。 Proof: Take any compiler and use it as a JIT. 证明:接受任何编译器并将其用作JIT。 QED. QED。

However , JIT is limited in execution time, so it's unacceptable to do complex optimizations there. 但是 ,JIT的执行时间有限,因此在那里进行复杂的优化是不可接受的。

This answer has a list of optimizations and strategies used by the JIT optimizer. 这个答案有一个JIT优化器使用的优化和策略列表。 They are not fundamentally different from what a native code optimizer does. 它们与本机代码优化器的作用并没有根本的区别。

One constraint in the jitter is that it cannot spend a great deal of time analyzing the code. 抖动中的一个限制是它不能花费大量时间来分析代码。 Every millisecond it burns affects the responsiveness of the program. 它燃烧的每毫秒都会影响程序的响应性。 Two strategies help. 两种策略有帮助。 Assemblies can be pre-jitted by ngen.exe, all of the .NET assemblies are and it is wise to do the same with your own assemblies. 程序集可以由ngen.exe预先安装,所有.NET程序集都是如此,并且对自己的程序集执行相同的操作也是明智之举。 As long as they are 'large', less time might be spent jitting the code than finding and loading the .ni.dll file on disk. 只要它们“大”,与在磁盘上查找和加载.ni.dll文件相比,可能花费更少的时间来查找代码。

And the jitter compiles code on demand, just before a method starts executing. 并且抖动在方法开始执行之前按需编译代码。 Which tends to spread the cost over time, relevant to code that runs at human time where responsiveness matters. 随着时间的推移,这往往会分摊成本,这与在响应性很重要的人类时间运行的代码相关。

One detail worth noting is that a jitter can take advantage of core specific instructions. 值得注意的一个细节是抖动可以利用核心特定指令。 That's an edge that is largely gone, cores are not that different these days. 这是一个基本消失的边缘,核心现在并没有那么不同。 But it will readily generate 64-bit code on a x64 operating system without doing anything special. 但它很容易在x64操作系统上生成64位代码,而不会做任何特殊操作。

A typical number that's bandied about is that jitted machine code is 85% as efficient as native precompiled code. 一个典型的数字是,jitted机器代码的效率是本机预编译代码的85%。 Take that with several large lumps of salt, the quality and nature of the source code is always first. 拿几块大块的盐来说,源代码的质量和性质始终是第一位的。

In general there are no things that a static complier can do that a JIT compiler cannot - since the JIT compiler has all the information that an equivalent static compiler would have - and more. 一般情况下,静态编译器无法做到JIT编译器无法做到的事情 - 因为JIT编译器具有等效静态编译器所具有的所有信息 - 以及更多。 A JIT compiler has one huge advantage - it knows the exact environment the code will be run in. JIT编译器有一个巨大的优势 - 它知道代码运行的确切环境。

There may be specific optimizations available in C++ that aren't in C# due to language features. 由于语言功能,C ++中可能存在不在C#中的特定优化。 But again it's more likely to be the other way around - it's easier to reason about C# in the absence of pointers for example. 但同样,它更可能是另一种方式 - 例如,在没有指针的情况下,更容易推理C#。

When looking at optimization in C# much of the optimization occurs in the translation from C# to IL, not by the JIT. 在C#中查看优化时,大部分优化发生在从C#到IL的转换中,而不是JIT。

A static compiler is run well in advance, while a JIT is done shortly before the code is executed. 静态编译器提前运行良好,而JIT在代码执行前不久完成。 A static compiler has a lot more time to run, and so optimizations which take a long time to perform are often done by static compilers but not by JIT compilers. 静态编译器有更多的时间来运行,因此需要很长时间才能执行的优化通常由静态编译器完成,而不是由JIT编译器完成。 In principle a JIT could do these optimizations, but the additional time it takes to perform them makes them impractical. 原则上,JIT 可以进行这些优化,但执行它们所需的额外时间使它们变得不切实际。


On the other hand, a modern JIT can in some cases make more aggressive assumptions about the code (producing faster code than a static compiler can), because it has the option of falling back to a less-optimized version if those assumptions turn out to be incorrect. 另一方面,现代JIT在某些情况下可以对代码做出更积极的假设(产生比静态编译器更快的代码),因为如果这些假设结果可以选择回退到不太优化的版本。不对。

The primary issue here is run-time and resources. 这里的主要问题是运行时和资源。 It takes 61 hours to compile Visual Studio- and I don't even know what kind of build machine they have. 编译Visual Studio需要61个小时 - 我甚至不知道他们有什么样的构建机器。 JITs, however, have to run with a fraction of the machine's real resources and in milliseconds. 但是,JIT必须以机器的实际资源的一小部分运行,并且以毫秒为单位运行。 There's no way that they could possibly optimize as strongly. 他们无法强有力地进行优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM