Does nodejs/V8 store compiled machine code anywhere on disk?

£可爱£侵袭症+ 提交于 2021-02-18 10:46:12

问题


Edit: Node uses bytecode since Node 8.3, before that, sources were compiled directly to machine code.

I do a lot of Python coding, and there's always bytecode lying around in .pyc files.

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?

Edit 2: As @dystroy mentions below this is a dupe of How can I see the machine code generated by v8?


回答1:


V8 introduced a bytecode interpreter, Ignition, in 2016. You can print the bytecode with --print-bytecode (Node 8.3 and newer).

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

To see the machine code, use --print-opt-code --code-comments.




回答2:


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.

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.




回答3:


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.

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.



来源:https://stackoverflow.com/questions/16673515/does-nodejs-v8-store-compiled-machine-code-anywhere-on-disk

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