Wednesday, September 17th, 2008

V8 Internals by Kevin Millikin

Category: JavaScript

My favourite presentation at Google Developer Day in London yesterday was a dive into the internals of how V8 works by Kevin Millikin, an V8 engineer who has been there and done that by creating Scheme compilers :)

He discussed the key implementation details of V8, including:

  • Hidden classes
  • Hidden class transitions
  • Inline caching
  • Native code compilation
  • Memory management / GC

He shows how the hidden class system is able to watch the “shape” of JavaScript objects with a simple Point example that showed us following the references. In their testing, it turned out that roughly 90% of the JavaScript code that they ran fell into a pattern where they did have shapes that made sense for optimization. Although JavaScript is very dynamic, at runtime it doesn’t act that differently to statically typed languages, which allowed the V8 team to conclude that they could then apply the similar tricks from static VMs such as the JVM (Lars and his team worked on HotSpot) to JavaScript.

When V8 sees code, it makes sure that it marks it, and if seen again if then applies a compilation to native code (there is no IL, no interpreter, straight to machine code). This means that the third time the code is run it is fast. Of course, this is to make sure that it is worth taking the time to compile the code. If it is for init, one-time like work, it doesn’t make sense.

The GC system is a fairly simple generational collector with young and old generations, and some special buckets within the old. The GC is very fast indeed, even though with WebKit it also manages internal objects and resources to keep it clean.

Kevin also shared other interesting points on what JavaScript can run fast, and features such as eval() and with() that result in much slower execution.

The final piece that I found interesting was the fact that the team decided to implement a bunch of the “standard library” side of the JavaScript spec (the objects available) in JavaScript itself.

You can take a peak at the source to see it at work:

javascript

  1. InstallProperties($Array.prototype, DONT_ENUM, {
  2.     constructor: $Array,
  3.     toString: ArrayToString,
  4.     toLocaleString: ArrayToLocaleString,
  5.     join: ArrayJoin,
  6.     pop: ArrayPop,
  7.     push: ArrayPush,
  8.     concat: ArrayConcat,
  9.     reverse: ArrayReverse,
  10.     shift: ArrayShift,
  11.     unshift: ArrayUnshift,
  12.     slice: ArraySlice,
  13.     splice: ArraySplice,
  14.     sort: ArraySort,
  15.     filter: ArrayFilter,
  16.     forEach: ArrayForEach,
  17.     some: ArraySome,
  18.     every: ArrayEvery,
  19.     map: ArrayMap,
  20.     indexOf: ArrayIndexOf,
  21.     lastIndexOf: ArrayLastIndexOf
  22.   });

The reasoning here is to get a turtles-all-the-way-down-like symbiotic loop. As they optimized for the standard library, all execution got faster. As they make the VM faster, it in turn makes the standard library faster.

The really good news, is the fact that there is a lot of headroom to make things a lot faster (ditto for TraceMonkey, SquirrelFish Extreme) so we have only just begun with the next gen JSVMs :)

Posted by Dion Almaer at 9:11 am
3 Comments

+++--
3.7 rating from 23 votes

3 Comments »

Comments feed TrackBack URI

Any chance of getting video of the talk?

Comment by tlrobinson — September 17, 2008

tlrobinson,

The video is coming actually :)

http://www.youtube.com/view_play_list?p=5C5DE5D3E276DD39 should have it.

Cheers,

Dion

Comment by Dion Almaer — September 18, 2008

I also attended the GDD. And I also really liked this talk. Maybe because I have some experience in compiler construction. Some plus, minus, zero observations:
+ no intensive optimization (difficult in dynamic OO-code)
o hidden classes (can’t tell yet, I would have used a different implementation)
– coupling of code generator to syntax tree (there are tools doing much better)
– no byte code (makes porting difficult, would have been simple)
– GC times (OK for interactive use, but slow anyway)

Well, V8 is in an early stage. Seems like very good work so far but still a lot of work to do.

virtuPIC

Comment by virtuPIC — September 18, 2008

Leave a comment

You must be logged in to post a comment.