V8引擎是否有基准编译器?

huangapple go评论80阅读模式
英文:

Does the V8 engine have a baseline compiler?

问题

我目前正在研究V8引擎如何处理JavaScript源代码。然而,有一些事情引起了我的注意。在所有这些中等文章中,他们解释如下:

源代码 -> 解析器 -> 抽象语法树(AST) -> 解释器 -> 字节码

他们说,当生成字节码时,代码在那个阶段被执行。这里没有基线编译器吗?我知道有一个优化编译器会收集分析数据等,但为什么没有提及基线编译器?

我在这个网站上看到了一篇名为“Sparkplug”的文章:https://v8.dev/blog/sparkplug。Sparkplug是基线编译器吗?

总结一下:

  • V8引擎是否有基线编译器?如果没有,字节码是如何执行的?它不需要被转换成机器码吗?
  • 为什么需要字节码?

我很好奇是否有基线编译器。

英文:

I'm currently researching how the V8 engine processes JavaScript source code. However, something has caught my attention. In all these medium articles, they explain it as follows:

source code -> parser -> abstract syntax tree (AST) -> interpreter -> bytecode

They say that when bytecode is generated, the code is executed at that stage. Is there no baseline compiler? I know that there is an optimizing compiler that collects profiling data, etc., but why is there no mention of a baseline compiler?

I came across an article called 'Sparkplug' on this website: https://v8.dev/blog/sparkplug. Is Sparkplug the baseline compiler?

To summarize:

  • Does the V8 engine have a baseline compiler? If not, how is bytecode executed? Doesn't it need to be translated into machine code?
  • Why is bytecode needed?

I am curious whether there is a baseline compiler or not.

答案1

得分: 2

是的,Sparkplug是一个“基线”或非优化的编译器。

V8架构一直在不断变化,我相信这个答案很快就会过时。我认为非优化编译器的添加是最近的发展,所以这可能是你听说V8没有非优化编译器的原因。但这是我对当前架构的理解。

V8引擎有多种执行代码的方式。它有一个解释器、一个基线编译器(Sparkplug)和一个优化编译器。采用多种策略的原因是编译代码所需的时间和执行代码所需的时间之间的权衡。一个较慢的编译器可以生成更优化的代码。但如果一段代码只执行一次或几次,优化编译器可能比解释器慢得多。因此,引擎根据代码执行的次数来选择策略。

但无论如何,源代码首先会被编译成字节码。解释器和编译器都使用这个字节码作为输入。

因此,流程更正确地表示如下:

源代码 -> 解析器 -> AST -> 字节码 -|-> 解释器
                                          |-> 基线编译器 -> 机器代码
                                          |-> 优化编译器 -> 机器代码

术语“编译器”既用于将源代码转换为字节码的部分,也用于将字节码转换为机器代码的部分。

字节码是源代码和机器代码之间的中间格式。它比源代码低级,但不像机器代码那样特定于CPU。使用字节码是因为解析源代码的成本很高,所以没有必要多次解析。

解释器不会将字节码编译为机器代码。它逐个读取和执行相应的字节码指令。这会在读取和解码每个字节码指令时产生一些开销,但优势在于不需要单独的编译过程,所以启动速度非常快。

英文:

Yes, Sparkplug is a "baseline" or non-optimizing compiler.

The V8 architecture is changing all the time, and I'm sure this answer will soon be out of date. I believe the addition of a non-optimizing compiler is a recent development, so this might be why you have heard V8 didn't have one. But this is my understanding of the current architecture.

The V8 engine has multiple ways to execute code. It has an interpreter, a baseline compiler (Sparkplug), and an optimizing compiler. The reason for the multiple strategies is a tradeoff between how long it takes to compile code and how long it takes to execute code. A slower compiler can generate more optimized code. But if a block of code is only executed once or a few times, an optimizing compiler might actually be a lot slower than an interpreter. So the engine shifts strategy based on how many times the code is executed.

But in all cases, the source code is first compiled into bytecode. The interpreter and the compilers all use this bytecode as input.

So the flow is more correctly illustrated like:

source code -> parser -> AST -> bytecode -|-> interpreter
                                          |-> baseline compiler -> machine code
                                          |-> optimizing compiler -> machine code

The term "compiler" is used both for the part transforming the source code into bytecode, and the parts transforming bytecode into machine code.

Bytecode is an intermediate format between source code and machine code. It is much lower level than source code, but not CPU specific like machine code. Bytecode is used because parsing source code is expensive, so there is no reason to do it more than once.

The interpreter does not compile the bytecode to machine code. It reads the bytecode instructions one at a time and executes the corresponding code. This has some overhead in reading and decoding each bytecode instruction, but the advantage is there is no separate compilation pass necessary, so it will be extremely fast to get started.

huangapple
  • 本文由 发表于 2023年6月5日 05:50:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定