Sam
Sam

Reputation: 4643

javascript engine v8 inline cache

as we have known that v8 enables inline caching to improve the performance,

Can anyone explain what v8 exactly does and how it works for improving the performance?

That will be nice if an example is shown.

thanks in advance.

Upvotes: 2

Views: 1529

Answers (1)

jermel
jermel

Reputation: 2336

Taken directly from Chrome V8 page

V8 compiles JavaScript source code directly into machine code when it is first executed. There are no intermediate byte codes, no interpreter. Property access is handled by inline cache code that may be patched with other machine instructions as V8 executes....

and

...V8 optimizes property access by predicting that this [the object's] class will also be used for all future objects accessed in the same section of code and uses the information in the class to patch the inline cache code to use the hidden class. If V8 has predicted correctly the property's value is assigned (or fetched) in a single operation. If the prediction is incorrect, V8 patches the code to remove the optimisation.

For example, the JavaScript code to access property x from a Point object is:

point.x

In V8, the machine code generated for accessing x is:

# ebx = the point object
cmp [ebx,<hidden class offset>],<cached hidden class>
jne <inline cache miss>
mov eax,[ebx, <cached x offset>]

Upvotes: 3

Related Questions