为什么v8将原生JavaScript的源代码保存在生成​​的二进制文件中? [英] Why does v8 saves the source code of native javascript in generated binaries?

查看:108
本文介绍了为什么v8将原生JavaScript的源代码保存在生成​​的二进制文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究v8源码,特别是在"mksnapshot"工具如何在v8二进制文件中包含本机javascript文件(runtime.js,json.js ...)的编译图像的同时,我注意到包括源代码(略微)的缩小版本.例如,在检查d8可执行文件的内容时,我看到以下代码段:

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;

在"src/json.js"的开头,我看到:

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.

我会理解是否包含原始代码以供使用'toString'进行检查,但是当我在d8中输入'JSON.stringify'时,我看到的只是'function stringify(){[native code]}',那是什么呢?重点是什么?

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?

推荐答案

实际上,快照并不包含编译形式的所有内置函数.

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

V8通常更喜欢延迟编译,以节省空间和时间.如果您编译未使用的东西,则会浪费内存来生成代码(并且由非优化编译器生成的代码相当冗长")和时间(如果我们谈论快照,则是在编译或反序列化上).

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

因此它可以延迟编译的所有内容V8都可以延迟编译,并且其中包括内置函数.因此,快照实际上并不包含所有功能的已编译版本,因此需要使用源来编译其余版本.

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.

存在源时,另一件事是可能的:优化:V8必须有权访问源才能应用其自适应优化流水线.

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.

这篇关于为什么v8将原生JavaScript的源代码保存在生成​​的二进制文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆