How V8 engine works?

How V8 engine works?

This translation from: How at The V8 Engine Works?

V8 is Google development center in Germany to build a JavaScript engine . It is written in C ++ open source project, and is applied using a client (Google browser) and server (Node.js).

V8 was originally designed to improve the performance of JavaScript to run a web browser. To improve performance, V8 JavaScript code that will translate into a more efficient machine language, rather than using an interpreter. It is by implementing a JIT (Just-In-Time, real-time) compiler to compile the JavaScript code into machine language, like many modern JavaScript engines such as SpiderMonkey or Rhino (Mozilla) do. V8 and the main difference between them is that it does not generate other intermediate code or bytecode.

The main purpose of this article is to demonstrate and understand V8 is how to optimize the code to work in order to generate (for the client or server-side application). If you had "? Should I care about what the performance of JavaScript," such doubts, I will quote Daniel Clifford (V8 R & D team leader and manager) of these words to answer you:

It is not just to make your application run faster now, it is for the impossible.

v8 engine

Hidden class

JavaScript is a prototype-based language: when using the cloning process, and no classes and objects. JavaScript is dynamically typed: type and type of information is not clear, the object's properties can be dynamically added or deleted. How to efficiently access types and properties is the first big challenge for the V8. Not be used like most of the JavaScript engine to do dictionary-like data structure stored object properties and dynamic properties to find the location, V8 created at runtime hidden classes (behind class) to generate an internal representation of the type of system and improving property access speed.

For example, we create a Pointconstructor and two Pointobjects:

hidden class

If the layout identical to this case, for example, pand qbelong to the same hidden class V8 created. It also shows a further advantage of the use of hidden classes: it allows V8 same object attributes can be classified as a group. Here pand qthe same optimized code .

Now suppose we want to give a statement after qthe object add an zattribute (dynamically typed language, which is normal).

V8会如何处理这种情况?实际上,V8在每一次构建函数声明新的属性是都会创建一个新的hidden class,并持续跟踪hidden class 的变化。为什么?因为如果创建了两个对象(pq),在创建后第二个对象q又被添加了一个属性,V8需要在保持上一个创建的hidden class(为第一个对象 p 创建) 的同时,为新的动态添加的属性创建一个新的hidden class(为第二个对象 q 创建)。

transition

每次创建一个新的hidden class时,前一个hidden class都进行一次类转换更新,指示要使用哪个hidden class而不是它。

代码优化

因为V8为每一个属性创建一个新的hidden class,hidden class的创建应该尽量少。因此,我们需要尽量避免在对象创建后添加属性,同时以相同的顺序初始化对象成员(来减少hidden classes的不同树的创建)。

单态操作(Monomorphic operations)指只在对象上的使用相同hidden class的操作。V8在我们调用函数时会新建一个hidden class。如果我们使用不同的参数类型再次调用此函数,V8需要创建另一个hidden class:因此尽量编写单态代码而不是多态代码

V8优化JavaScript代码的更多例子

切换值

为了更高效地描述数和JavaScript对象,在V8中,两者均使用32位值表示。其中1位表示这个值是对象(flag = 1)还是数(flag = 0)。如果一个数比31位大,V8会把它转换为double存储在新建的一个对象中。

代码优化:如果可能的话,尽量使用31位有符号数,来减少上述的高代价的操作。

数组

V8使用两种不同的方法来操作数组:

  • 快速元素(Fast elements):为无间隙的密集数组设计。它们使用线性存储缓存,使得访问非常高效。([1,2,4,5,8])
  • 字典元素(Dictionary elements):为有间隙的稀疏数组设计。使用哈希表缓存,较之快速元素访问代价更高。([1,2,,5,8])

代码优化:尽量使用 V8 会使用快速元素方法来操作的数组。即减少使用键不是递增数的数组的使用。同时,尽量避免预分配大数组。在使用中让它自己慢慢增加会更好。同时,不要删除数组中的元素:它会使得数组稀疏。

V8如何编译JavaScript代码?

V8有两个编译器:

  • 一个是"完整"编译器,可以编译任何的JavaScript代码:编译结果为好的代码但不是好的JIT代码。这个编译器的目的就是快速生成代码。为了实现这个目的,它不会进行任何的类型分析,因此它对类型一无所知。相反的,它使用内联缓存(Inline Caches)策略来在程序运行中精炼类型信息。内联缓存非常高效,带来了20倍的速度提升。
  • 另一个是优化编译器,可以编译大多数JavaScript代码,生成更好的代码。它出现的更晚,并对热函数进行了重编译。优化编译器从内联缓存中获取类型并决定如何对代码进行优化。然而,有些语言特性并没有被支持或可能会抛出错误。(应对方法是使用try catch)

代码优化:V8同样支持去优化:优化编译器根据从内联缓存中获取的类型信息进行优化,当此优化后有问题时会去优化。例如,如果生成的hidden class不是期望的那样,V8会抛弃优化代码,返回到完整编译器生成的代码,并从内联缓存中重新获取类型。这个过程很慢,因此应尽量在函数被优化后不去修改它。

参考资料

  • Google I/O 2012 “Breaking the JavaScript Speed Limit with V8” with Daniel Clifford, tech lead and manager of the V8 team: video and slides.
  • V8: an open source JavaScript engine: video of Lars Bak, V8 core engineer.
  • Nikkei Electronics Asia blog post: Why Is the New Google V8 Engine So Fast?

Guess you like

Origin www.cnblogs.com/zmj97/p/10956912.html