简体   繁体   English

V8中的JavaScript编译

[英]JavaScript compilation in V8

In the V8 home (the Google's JavaScript engine) we read this: V8主页 (Google的JavaScript引擎)中,我们阅读:

V8 compiles and executes JavaScript source code V8编译并执行JavaScript源代码

  • Does it mean that JavaScript is not an interpreted language in V8? 这是否意味着JavaScript不是V8中的解释语言?

  • Does V8 use a just-in-time compilation approach for JavaScript? V8是否对JavaScript使用即时编译方法?


Edit : There is another existing question which already address my first question, but not the second. 编辑 :还有一个现有的问题已经解决了我的第一个问题,但不是第二个。

Does it mean that JavaScript is not an interpreted language in V8? 这是否意味着JavaScript不是V8中的解释语言?

The answer to this is "it depends". 答案是“取决于”。

Historically V8 has compiled directly to machine code using its "full-codegen" compiler, which produces unoptimized code which uses inline caching to implement most operations such as arithmetic operations, load and stores of variables and properties, etc. 从历史上看,V8使用其“ full-codegen”编译器直接编译为机器代码,该编译器生成未优化的代码,该代码使用内联缓存来实现大多数操作,例如算术运算,变量和属性的加载和存储等。

The code generated by full-codegen keeps track of how "hot" each function is, by adjusting a counter when the function is called and when it jumps back to the top of loops. full-codegen生成的代码通过在调用函数时以及跳转到循环顶部时调整计数器来跟踪每个函数的“热度”。

It also keeps track of the types of the variables used in each expression. 它还跟踪每个表达式中使用的变量的类型。

If it determines that a function (or part of a function) is very hot, and it has collected enough type information, it triggers the "Crankshaft" compiler which generates much better code. 如果确定某个函数(或函数的一部分)非常热,并且已经收集了足够的类型信息,则它将触发“曲轴”编译器,该编译器会生成更好的代码。

However, the V8 developers are actively working on moving to a different system where they start off with an interpreter called "Ignition" and then use a compiler called "Turbofan" to produce optimized code for hot functions. 但是,V8开发人员正在积极地迁移到另一个系统,在该系统中,他们首先使用称为“点火”的解释器,然后使用名为“ Turbofan”的编译器为热功能生成优化的代码。

Here are a couple of posts from the V8 developers blog describing this: 这是V8开发人员博客中的几篇帖子,对此进行了描述:

Does V8 use a just-in-time compilation approach for JavaScript? V8是否对JavaScript使用即时编译方法?

Yes, in a number of ways. 是的,有多种方式。

Firstly, it has a lazy parsing and lazy compilation mechanism. 首先,它具有延迟解析和延迟编译机制。 This means that when it parses a Javascript source file it parses the outermost scope eagerly, generating the full-codegen code immediately. 这意味着当它解析Javascript源文件时,它会急切地解析最外层作用域,并立即生成完整的代码生成代码。

However, for functions defined within the file, it skips over them and just records the name of the function and the location of its source code. 但是,对于文件中定义的函数,它会跳过它们,仅记录函数的名称及其源代码的位置。 It generates a dummy function which simply calls into the V8 runtime to trigger the actual compilation of the function. 它生成一个伪函数,该伪函数只是调用V8运行时以触发该函数的实际编译。

Secondly, it has a two stage compiler pipeline as described above, using either full-codegen+crankshaft or ignition+turbofan. 其次,它具有如上所述的两级编译器流水线,使用全码本+曲轴或点火+涡轮增压器。

When the compilation is triggered it will initially generate unoptimized code or ignition bytecode (which it can do very quickly), and then later if the code is hot it triggers an optimized re-compilation (which is much slower but generates much better code). 触发编译时,它将首先生成未优化的代码或点火字节码(可以非常快速地执行),然后,如果代码很热,它将触发优化的重新编译(速度较慢,但​​生成的代码更好)。

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

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