Ruby代码编译:从AST到YARV指令
在Ruby编程中,了解代码是如何被编译的有助于我们更好地理解语言的工作原理,优化代码性能。下面将深入探讨Ruby代码编译的过程,包括YARV指令优化、块调用编译、AST遍历以及不同类型参数的编译等内容。
1. YARV指令优化
Ruby在执行YARV指令之前会进行优化,其中一项优化是将一些常用操作的YARV指令替换为专门的指令。例如,对于 puts 2+2 这样的代码,最初的YARV指令如下:
NODE_SCOPE
putself
putobject
putobject
send
send
NODE_FCALL
2
2
<callinfo!mid:+, argc:1, ...
<callinfo!mid:puts, argc:1, ...
YARV instructions
table: [ none ]
args: [ none ]
method id:
puts
在优化过程中, send <callinfo!mid:+... 会被替换为 opt_plus ,第二个 send 会被替换为 opt_send_simple ,优化后的指令如下:
NODE_SCOPE
putself
putobject
putobject
opt_plus
opt_send_simple
NODE_F
超级会员免费看
订阅专栏 解锁全文
28

被折叠的 条评论
为什么被折叠?



