Why does v8 saves the source code of native javascript in generated binaries?
Asked Answered
K

2

5

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?

Khajeh answered 29/9, 2012 at 14:18 Comment(3)
You should've asked this question during Google IO 2012 :)Columbus
Here are some of my observations on this subject. Can you some how save the output of this nodejs.org/api/vm.html#vm_vm_createscript_code_filename and run it at a latter time? When you compile nodejs it does not seem to still use the javascript files as I can not find or locate them any where except in the source.Dupondius
@MichałMiszczyszyn What was at Google I/O 2012?Gunn
F
4

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.

Fluted answered 30/9, 2012 at 16:41 Comment(3)
So if I run a nodejs script with two functions and only one is used the other will never be compiled? What file in the source I could find more about this lazy compilation behavior?Khajeh
That depends on many factors, in most common cases only one used function will be compiled. You can start reading code in compiler.cc: code.google.com/p/v8/source/browse/trunk/src/…Fluted
What is snapshot?Gunn
L
-1

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. :)

Lovesick answered 29/9, 2012 at 14:22 Comment(2)
Thats exactly why I made this question. If they already save the compiled machine code why also save the source which generated it?Khajeh
Just in case there is something insane going on and the client decides it needs the source. Bandwidth is easy to come by, especially when you can cache things. v8 was written with speed in mind, so bandwidth costs took the back seat to a faster engine.Lovesick

© 2022 - 2024 — McMap. All rights reserved.