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?

Thiago Padilha
  • 4,590
  • 5
  • 44
  • 69
  • You should've asked this question during Google IO 2012 :) – Michał Miszczyszyn Sep 29 '12 at 15:10
  • Here are some of my observations on this subject. Can you some how save the output of this http://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. – Tegra Detra Sep 30 '13 at 21:11
  • @MichałMiszczyszyn What was at Google I/O 2012? – Melab Sep 05 '22 at 21:41

2 Answers2

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.

Vyacheslav Egorov
  • 10,302
  • 2
  • 43
  • 45
  • 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? – Thiago Padilha Oct 01 '12 at 13:33
  • That depends on many factors, in most common cases only one used function will be compiled. You can start reading code in `compiler.cc`: http://code.google.com/p/v8/source/browse/trunk/src/compiler.cc?r=12566#935 – Vyacheslav Egorov Oct 02 '12 at 11:26
  • What is `snapshot`? – Melab Sep 05 '21 at 22:06
-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. :)

L0j1k
  • 12,255
  • 7
  • 53
  • 65
  • Thats exactly why I made this question. If they already save the compiled machine code why also save the source which generated it? – Thiago Padilha Sep 29 '12 at 14:27
  • 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. – L0j1k Sep 29 '12 at 14:31