Question

I've been studying the v8 source, particularly at how the 'mksnapshot' tool includes a compiled image of the native javascript files(runtime.js, json.js...) in the v8 binaries and noticed that it also includes a (somewhat) minified version of the source. For example, when inspecting the contents of the d8 executable, I see the following snippet:

var $JSON=global.JSON;

function Revive(a,b,c){
var d=a[b];
if((%_IsObject(d))){
if((%_IsArray(d))){
var g=d.length;

and at the start of 'src/json.js' I see:

var $JSON = global.JSON;

function Revive(holder, name, reviver) {
  var val = holder[name];
  if (IS_OBJECT(val)) {
    if (IS_ARRAY(val)) {
      var length = val.length;

clearly both snippets are equivalent but the second was transformed into the first in the compilation process.

I would have understood if the original code was included for inspecting with 'toString' but when I enter 'JSON.stringify' in d8 all I see is 'function stringify() { [native code] }', so what is the point of this?

Was it helpful?

Solution

Actually snapshot does not include all builtins in the compiled form.

V8 in general prefers lazy compilation to save space and time. If you compile things that are not used you waste memory for generated code (and code generated by a non-optimizing compiler is quite "verbose") and time (either on compilation or on deserialization if we are talking about snapshot).

So everything that it can compile lazily V8 does compile lazily and this includes builtins. Thus snapshot does not actually contain compiled versions for all functions and source is required to compile rest.

Another thing that becomes possible when source is present is optimization: V8 has to have access to the source to apply its adaptive optimization pipeline.

OTHER TIPS

Probably because caching the binary is what makes v8 so incredibly fast: It was built to be very fast. So they have taken extreme steps to make it fast. Pre-generated binaries of native code take away the thinking from the client, making it run just that much faster. There are optimizations like this all over v8. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top