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

空扰寡人 提交于 2019-12-20 14:36:20

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/12653479/why-does-v8-saves-the-source-code-of-native-javascript-in-generated-binaries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!