V8 JavaScript engine Research (ii) High Performance quest

Source: Internet
Author: User
Tags access properties

V8 's high-performance quest

V8 Project leader Lars Bak:v8 all the optimizations are not original. V8 combines various optimization techniques for dynamic languages in the past, thus having a very efficient performance.

Quick Property Access

JavaScript is a dynamic programming language, which means that the properties of an object can be dynamically added or deleted.

Past implementations

Most JavaScript engines use a dictionary-like data structure to hold the properties of an object, where each access to a property requires a dynamic lookup of its location in memory.

    • Advantage:

    Simple to implement.

    • Disadvantage:

    Low efficiency. As opposed to Java, Smalltalk, and other languages, because the class of the object has a fixed layout, the property in memory has a fixed displacement of the compiler generated relative to the object, so the Access property is much faster, it may only require a machine instruction.

The realization of V8

To reduce the time-consuming nature of accessing properties, V8 does not use dynamic dynamic lookups to access properties.

V8 creates a hidden class (Hidden Class) for each object, replacing the new hidden class whenever an attribute is added.

To illustrate:

1 function{3 this.x = x;4 this.y = y;5}  

When a new point (x, y) is called, the V8 is processed by first creating a hidden class of point, which in this case is called C0, when the object has no properties and the initial class is empty. At this stage, the hidden class of the point object is C0.

When executing the first statement in point this.x = x; Creates a new property x for the Point object. At this point V8 creates another C0-based hidden class C1, and then adds descriptive information to the C1, indicating that the point object owns the attribute x and is stored at offset 0 relative to the points object. The class transfer information for C0 is also updated to indicate that the hidden class of this object will be replaced by C0 with C1 when attribute x is added to an object based on the C0 hidden class. At this stage, the hidden class of the point object is C1.

When executing the first statement in point this.y = y; Creates a new property y for the point object. At this point V8 creates another C1-based hidden class C2, and then adds descriptive information to the C2, indicating that the point object also owns the property y and is stored at offset 1 relative to the points object. Also updates the class transfer information for C1, indicating that the hidden class of this object will be replaced by C1 with C2 when attribute y is added to an object based on the C1 hidden class. At this stage, the hidden class of the point object is C2.

It may seem very inefficient to create a hidden class whenever an attribute is added, but the hidden class can be reused because of the class's transfer information. Imagine a scenario where the next time a new point object is created, you no longer need to create a new hidden class, and the new Point object will share the series of hidden classes created for the first point object, as follows:

Initializes a point object without any attributes, at which time the initial hidden class C0 applies to this object.

When attribute x is added, V8 follows the class transfer information from C0 to C1, and then writes the value of x at the offset specified by the C1.

When attribute y is added, V8 follows the class transfer information from C1 to C2, and then writes the value of Y at the offset specified by the C2.

Although JavaScript is more dynamic than most object-oriented languages, the run-time behavior of most JavaScript programs results in a shared structure that is highly used by the above methods.

    • Advantage:

    You do not need to use a dynamic lookup dictionary to access the properties of an object, especially when you create large numbers of identical objects with significant efficiency gains.

You can have V8 use an optimization technique based on hidden classes-inline caching (inline Caching).

    • Disadvantage:

Implementation is slightly more complex.

Creating a new hidden class every time the object is added is very inefficient in extreme cases.

Inline Caching & Dynamic machine code generation

V8 's inline caching relies on hidden class technology, where inline with C + + is a two different concept.

The first time the JavaScript code is executed, V8 directly compiles it into machine code without producing an intermediate bytecode. (*google has released a new JavaScript interpreter ignition, designed to reduce memory consumption and use on some devices with limited memory)

The first time you specify code to access the properties of a given object, V8 determines the hidden class for this object. V8 predicts that all object property accesses that will be performed on this code snippet in the future have the same hidden class and code that accesses the property by using this hidden class, hitting a patch that uses an inline cache access instruction, which is usually an access instruction to the memory offset address, which is the concept of inline. If the V8 predicts a hit, it greatly optimizes the time to access the property, and if V8 misses, removes the patch code that was hit, re-creates the hidden class and accesses the property.

This greatly improves the efficiency of processing large numbers of objects of the same type and frequently creating and accessing them (the vast majority of JavaScript code does so).

Efficient garbage Collection implementation Introduction to V8 garbage collection mechanism

V8 automatically reclaims object memory that is no longer needed at run time, which is also garbage collection.

V8 uses a combination of full-pause (Stop-the-world), generational (generational), and precise (accurate) garbage collection mechanisms to ensure faster object memory allocations, shorter pauses when garbage collection is triggered, and no memory fragmentation.

V8 garbage collection has the following characteristics:

    • Suspends execution of all programs when a garbage collection cycle is processed.
    • In most garbage collection cycles, only the objects in the partial heap are processed at a time, minimizing the impact of the pause program.
    • Know exactly what objects and pointers are in memory and avoid mistakenly treating objects as a memory leak caused by pointers.

V8 JavaScript engine Research (ii) High Performance quest

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.