Uncovering Ruby Bytecode Patterns
Since Ruby 1.9, Ruby runs your code in a bytecode VM. That means that the ruby compiler converts your code to a series of bytecode instructions. For example,
ruby --dump=insns -e '5 * 10' == disasm: <ISeq:<main>@-e:1 (1,0)-(1,6)> (catch: false) 0000 putobject 5 0002 putobject 10 0004 opt_mult <calldata!mid:*, argc:1, ARGS_SIMPLE>[CcCr] 0006 leave
The bytecode instructions
putobject is called twice,
opt_mult is called next, then lastly
leave. These are the bytecode instructions that Ruby runs when executing
5 * 10. Ruby uses a stack based VM, so after
putobject 5 is called,
5 is on the stack to be used by other instructions.
What instructions the Ruby VM is actually running? Finding common patterns could lead to interesting optimizations and a better understanding of the Ruby VM. Ruby...
Continue reading →