Edit: Node uses bytecode since Node 8.3, before that, sources were compiled directly to machine code.
编辑:节点使用自Node 8.3以来的字节码,在此之前,源代码直接编译为机器代码。
I do a lot of Python coding, and there's always bytecode lying around in .pyc files.
我做了很多Python编码,并且.pyc文件中总是存在字节码。
I was wondering if node stores its machine code in similar files, eg it would make sense to keep the machine code representation around on disk and re-use it if a file's source is unchanged.
我想知道节点是否将其机器代码存储在类似的文件中,例如,将机器代码表示保存在磁盘上并在文件源不变的情况下重新使用它是有意义的。
If so, where does node/v8 store this machine code?
如果是这样,node / v8在哪里存储此机器代码?
Edit 2: As @dystroy mentions below this is a dupe of How can I see the machine code generated by v8?
编辑2:正如@dystroy下面提到的这是一个骗局我如何看到v8生成的机器代码?
3 个解决方案
#1
4
V8 introduced a bytecode interpreter, Ignition, in 2016. You can print the bytecode with --print-bytecode
(Node 8.3 and newer).
V8在2016年引入了字节码解释器Ignition。您可以使用--print-bytecode(Node 8.3和更新版本)打印字节码。
$ node --print-bytecode incrementX.js -e 'function incrementX(obj) {return 1 + obj.x;} incrementX({x: 42});`
...
[generating bytecode for function: incrementX]
Parameter count 2
Frame size 8
12 E> 0x2ddf8802cf6e @ StackCheck
19 S> 0x2ddf8802cf6f @ LdaSmi [1]
0x2ddf8802cf71 @ Star r0
34 E> 0x2ddf8802cf73 @ LdaNamedProperty a0, [0], [4]
28 E> 0x2ddf8802cf77 @ Add r0, [6]
36 S> 0x2ddf8802cf7a @ Return
Constant pool (size = 1)
0x2ddf8802cf21: [FixedArray] in OldSpace
- map = 0x2ddfb2d02309 <Map(HOLEY_ELEMENTS)>
- length: 1
0: 0x2ddf8db91611 <String[1]: x>
Handler Table (size = 16)
See Understanding V8's Bytecode.
请参阅了解V8的字节码。
To see the machine code, use --print-opt-code --code-comments
.
要查看机器代码,请使用--print-opt-code --code-comments。
#2
8
V8 is a just in time compiler. So JavaScript cannot be compiled just once like python compiler which is static compilation. It is compiled as and when it needs to be executed.
V8是一个及时的编译器。所以JavaScript不能像python编译器那样只编译一次,这是静态编译。它是在需要执行时编译的。
You cannot see the generated machine code for JavaScript, because it is not stored. It does not make sense to store the machine code that was compiled, as compilation happens repeatedly and is affected by runtime optimisations. You don't get fixed machine code like for python, every time it happens.
您无法看到生成的JavaScript机器代码,因为它未存储。存储编译的机器代码没有意义,因为编译反复发生并受运行时优化的影响。每次发生时,你都没有得到像python这样的固定机器代码。
#3
4
From the project's page :
从项目的页面:
V8 compiles JavaScript source code directly into machine code when it is first executed. There are no intermediate byte codes, no interpreter.
V8首次执行时,会将JavaScript源代码直接编译为机器代码。没有中间字节代码,没有解释器。
That's why you won't find the bytecode, there is none.
这就是为什么你不会找到字节码,没有。
Regarding the new question following your edit, I think this related question answers it mostly. Of course there's no reason in general for V8 to write the machine code on disk with the default setup. As this code changes a lot (see the link above, explaining how dynamic classes are created), that would be a gigantic overhead.
关于编辑后的新问题,我认为这个相关问题主要是回答它。当然,V8通常没有理由使用默认设置在磁盘上写入机器代码。由于此代码发生了很大变化(请参阅上面的链接,解释了如何创建动态类),这将是一个巨大的开销。
#1
4
V8 introduced a bytecode interpreter, Ignition, in 2016. You can print the bytecode with --print-bytecode
(Node 8.3 and newer).
V8在2016年引入了字节码解释器Ignition。您可以使用--print-bytecode(Node 8.3和更新版本)打印字节码。
$ node --print-bytecode incrementX.js -e 'function incrementX(obj) {return 1 + obj.x;} incrementX({x: 42});`
...
[generating bytecode for function: incrementX]
Parameter count 2
Frame size 8
12 E> 0x2ddf8802cf6e @ StackCheck
19 S> 0x2ddf8802cf6f @ LdaSmi [1]
0x2ddf8802cf71 @ Star r0
34 E> 0x2ddf8802cf73 @ LdaNamedProperty a0, [0], [4]
28 E> 0x2ddf8802cf77 @ Add r0, [6]
36 S> 0x2ddf8802cf7a @ Return
Constant pool (size = 1)
0x2ddf8802cf21: [FixedArray] in OldSpace
- map = 0x2ddfb2d02309 <Map(HOLEY_ELEMENTS)>
- length: 1
0: 0x2ddf8db91611 <String[1]: x>
Handler Table (size = 16)
See Understanding V8's Bytecode.
请参阅了解V8的字节码。
To see the machine code, use --print-opt-code --code-comments
.
要查看机器代码,请使用--print-opt-code --code-comments。
#2
8
V8 is a just in time compiler. So JavaScript cannot be compiled just once like python compiler which is static compilation. It is compiled as and when it needs to be executed.
V8是一个及时的编译器。所以JavaScript不能像python编译器那样只编译一次,这是静态编译。它是在需要执行时编译的。
You cannot see the generated machine code for JavaScript, because it is not stored. It does not make sense to store the machine code that was compiled, as compilation happens repeatedly and is affected by runtime optimisations. You don't get fixed machine code like for python, every time it happens.
您无法看到生成的JavaScript机器代码,因为它未存储。存储编译的机器代码没有意义,因为编译反复发生并受运行时优化的影响。每次发生时,你都没有得到像python这样的固定机器代码。
#3
4
From the project's page :
从项目的页面:
V8 compiles JavaScript source code directly into machine code when it is first executed. There are no intermediate byte codes, no interpreter.
V8首次执行时,会将JavaScript源代码直接编译为机器代码。没有中间字节代码,没有解释器。
That's why you won't find the bytecode, there is none.
这就是为什么你不会找到字节码,没有。
Regarding the new question following your edit, I think this related question answers it mostly. Of course there's no reason in general for V8 to write the machine code on disk with the default setup. As this code changes a lot (see the link above, explaining how dynamic classes are created), that would be a gigantic overhead.
关于编辑后的新问题,我认为这个相关问题主要是回答它。当然,V8通常没有理由使用默认设置在磁盘上写入机器代码。由于此代码发生了很大变化(请参阅上面的链接,解释了如何创建动态类),这将是一个巨大的开销。