如何通俗易懂地介绍「即时编译」(JIT),它的优点和缺点是什么?

PyPy 用 JIT (Just-in-time compilation) 来大幅提高性能。既然这么好,为什么 CPython,Ruby 不用 JIT…
关注者
541
被浏览
263,000

17 个回答

这是stackoverflow回答:(已经非常通俗易懂)

A JIT compiler runs after the program has started and compiles the code (usually bytecode or some kind of VM instructions) on the fly (or just-in-time, as it's called) into a form that's usually faster, typically the host CPU's native instruction set. A JIT has access to dynamic runtime information whereas a standard compiler doesn't and can make better optimizations like inlining functions that are used frequently.

This is in contrast to a traditional compiler that compiles all the code to machine language before the program is first run.

To paraphrase, conventional compilers build the whole program as an EXE file BEFORE the first time you run it. For newer style programs, an assembly is generated with pseudocode (p-code). Only AFTER you execute the program on the OS (e.g., by double-clicking on its icon) will the (JIT) compiler kick in and generate machine code (m-code) that the Intel-based processor or whatever will understand.

再上个图,参考。

从语言的特性来说,JIT 恰恰更适合动态语言。这是 LuaJIT 的作者对 static optimization 和 JIT 优化的分析:

Re: [ANN] llvm-lua 1.0