V8 Internals: Building A High Performance JavaScript Engine
V8 Internals: Building A High Performance JavaScript Engine
function Point(x, y) {
this.x = x;
this.y = y;
}
var p1 = new Point(0,1);
var p2 = new Point(2,3);
Hidden Classes by Example
function Point(x, y) {
this.x = x;
this.y = y;
}
var p1 = new Point(0,1);
var p2 = new Point(2,3);
Hidden Classes by Example
function Point(x, y) {
this.x = x;
this.y = y;
}
var p1 = new Point(0,1);
var p2 = new Point(2,3);
Hidden Classes by Example
function Point(x, y) {
this.x = x;
this.y = y;
}
var p1 = new Point(0,1);
var p2 = new Point(2,3);
Hidden Classes by Example
function Point(x, y) {
this.x = x;
this.y = y;
}
var p1 = new Point(0,1);
var p2 = new Point(2,3);
Hidden Classes by Example
function Point(x, y) {
this.x = x;
this.y = y;
}
var p1 = new Point(0,1);
var p2 = new Point(2,3);
Hidden Classes by Example
function Point(x, y) {
this.x = x;
this.y = y;
}
var p1 = new Point(0,1);
var p2 = new Point(2,3);
Hidden Classes by Example
function Point(x, y) {
this.x = x;
this.y = y;
}
var p1 = new Point(0,1);
var p2 = new Point(2,3);
Hidden Classes by Example
function Point(x, y) {
this.x = x;
this.y = y;
}
var p1 = new Point(0,1);
var p2 = new Point(2,3);
Hidden Classes by Example
function Point(x, y) {
this.x = x;
this.y = y;
}
var p1 = new Point(0,1);
var p2 = new Point(2,3);
How Dynamic is JavaScript?
In 90% of the cases, only objects having the same map are
seen at an access site
...
load 'x'
...
load 'y'
...
Inline Caching
...
load 'x'
...
load 'y'
...
Inline Caching
...
load 'x'
...
load 'y'
...
Inline Caching
...
load 'x'
...
load 'y'
...
Monomorphic Load Inline Cache
0xf7c0d32d (size = 37):
0 mov eax,[esp+0x4]
4 test al,0x1
6 jz 32
12 cmp [eax+0xff],0xf78fab81
19 jnz 32
25 mov ebx,[eax+0x3]
28 mov eax,[ebx+0x7]
31 ret
32 jmp LoadIC_Miss
Monomorphic Load Inline Cache
0xf7c0d32d (size = 37):
0 mov eax,[esp+0x4] ; receiver load
4 test al,0x1 ; object check
6 jz 32
12 cmp [eax+0xff],0xf78fab81
19 jnz 32
25 mov ebx,[eax+0x3]
28 mov eax,[ebx+0x7]
31 ret
32 jmp LoadIC_Miss
Monomorphic Load Inline Cache
0xf7c0d32d (size = 37):
0 mov eax,[esp+0x4] ; receiver load
4 test al,0x1 ; object check
6 jz 32
12 cmp [eax+0xff],0xf78fab81 ; class check
19 jnz 32
25 mov ebx,[eax+0x3]
28 mov eax,[ebx+0x7]
31 ret
32 jmp LoadIC_Miss
Monomorphic Load Inline Cache
0xf7c0d32d (size = 37):
0 mov eax,[esp+0x4] ; receiver load
4 test al,0x1 ; object check
6 jz 32
12 cmp [eax+0xff],0xf78fab81 ; class check
19 jnz 32
25 mov ebx,[eax+0x3] ; load properties
28 mov eax,[ebx+0x7] ; load property
31 ret
32 jmp LoadIC_Miss
Monomorphic Load Inline Cache
0xf7c0d32d (size = 37):
0 mov eax,[esp+0x4] ; receiver load
4 test al,0x1 ; object check
6 jz 32
12 cmp [eax+0xff],0xf78fab81 ; class check
19 jnz 32
25 mov ebx,[eax+0x3] ; load properties
28 mov eax,[ebx+0x7] ; load property
31 ret
32 jmp LoadIC_Miss ; fallback to
; generic lookup
Inline Cache States
Scavenge
Copying collection of only the young generation
Pause times ~2ms
Full non-compacting collection
Mark-Sweep collection of both generations
Free memory gets added to free lists
Might cause fragmentation
Pause times ~50ms
Full compacting collection
Mark-Sweep-Compact collection of both generations
Pause times ~100ms
Recent developments
Irregexp: New Regular Expression Engine
V8 initially used a library from WebKit called JSCRE
JSCRE did not fit well with the string types in V8 and did
not perform well
Implemented Irregexp giving a 10x speedup on regular
expression matching on benchmark programs
Irregexp implements full JS regular expressions and there
is no fallback to other libraries
Irregexp Internals
Builds an automaton from the input regular expression
Performs analysis and optimization on the automaton
Generates native code
Uses a number of tricks to avoid backtracking
Reorders operations to perform the least expensive
operations first
Irregexp Examples
Use masks to search for common parts in alternatives first
To search for
/Sun|Mon/
/??n/
/foobar/
0x666f6f62 0x6172
Irregexp Examples
Perform cheap operations first
For
/([fF]oo[bB]ar)/
Bigger is better!
Scalability
This experiment is artificial