简体   繁体   English

为什么在执行时而不是在安装时编译字节码JIT?

[英]Why is bytecode JIT compiled at execution time and not at installation time?

Compiling a program to bytecode instead of native code enables a certain level of portability, so long a fitting Virtual Machine exists. 将程序编译为字节码而不是本机代码可以实现一定程度的可移植性,因此存在适合的虚拟机。

But I'm kinda wondering, why delay the compilation? 但我有点疑惑,为什么要推迟编译? Why not simply compile the byte code when installing an application? 为什么不在安装应用程序时简单地编译字节代码?

And if that is done, why not adopt it to languages that directly compile to native code? 如果这样做,为什么不将它采用直接编译为本机代码的语言? Compile them to an intermediate format, distribute a "JIT" compiler with the installer and compile it on the target machine. 将它们编译为中间格式,使用安装程序分发“JIT”编译器并在目标机器上编译它。

The only thing I can think of is runtime optimization. 我唯一能想到的是运行时优化。 That's about the only major thing that can't be done at installation time. 这是安装时唯一无法完成的重大事情。 Thoughts? 思考?

Often it is precompiled. 通常它是预编译的。 Consider, for example, precompiling .NET code with NGEN . 例如,考虑使用NGEN预编译.NET代码。

One reason for not precompiling everything would be extensibility. 不预编译所有内容的一个原因是可扩展性。 Consider those languages which allow use of reflection to load additional code at runtime. 考虑那些允许使用反射在运行时加载其他代码的语言。

Some JIT Compilers (Java HotSpot, for example) use type feedback based inlining. 一些JIT编译器(例如Java HotSpot)使用基于类型反馈的内联。 They track which types are actually used in the program, and inline function calls based on the assumption that what they saw earlier is what they will see later. 它们跟踪程序中实际使用的类型,以及内联函数调用基于他们之前看到的内容是他们稍后会看到的内容。 In order for this to work, they need to run the program through a number of iterations of its "hot loop" in order to know what types are used. 为了使其工作,他们需要通过其“热循环”的多次迭代来运行程序,以便知道使用了什么类型。

This optimization is totally unavailable at install time. 此优化在安装时完全不可用。

The bytecode has been compiled just as well as the C++ code has been compiled. 字节码的编译编译的C ++代码一样。

Also the JIT compiler, ie .NET and the Java runtimes are massive and dynamic; JIT编译器,即.NET和Java运行时也是庞大而动态的; And you can't foresee in a program which parts the apps use so you need the entire runtime. 并且您无法在程序中预见应用程序使用哪些部分,因此您需要整个运行时。

Also one has to realize that a language targeted to a virtual machine has very different design goals than a language targeted to bare metal. 还必须意识到,针对虚拟机的语言与针对裸机的语言具有非常不同的设计目标。

Take C++ vs. Java. 以C ++与Java相结合。

  • C++ wouldn't work on a VM, In particular a lot of the C++ language design is geared towards RAII. C ++不适用于VM,特别是很多C ++语言设计都适用于RAII。
  • Java wouldn't work on bare metal for so many reasons. 出于这么多原因,Java不会在裸机上工作。 primitive types for one. 一个原始类型。

EDIT: As delnan points out correctly; 编辑:正如德尔南指出的那样; JIT and similar technologies, though hugely benificial to bytecode performance, would likely not be available at install time. JIT和类似技术虽然对字节码性能非常有益,但在安装时可能无法使用。 Also compiling for a VM is very different from compiling to native code. 编译VM也与编译为本机代码有很大不同。

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

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