Skip to content

Instantly share code, notes, and snippets.

@hibariya
Created August 30, 2016 03:41
Show Gist options
  • Save hibariya/eb0225db8de6ca06e0739f2ffb348af2 to your computer and use it in GitHub Desktop.
Save hibariya/eb0225db8de6ca06e0739f2ffb348af2 to your computer and use it in GitHub Desktop.
I never knew 'literal'.freeze is compiled in 1 instruction (not 2)
# 1 instruction
puts RubyVM::InstructionSequence.compile(%('hi')).disasm
# == disasm: #<ISeq:<compiled>@<compiled>>================================
# 0000 trace 1 ( 1)
# 0002 putstring "hi"
# 0004 leave
# 1 instruction
puts RubyVM::InstructionSequence.compile(%('hi'.freeze)).disasm
# == disasm: #<ISeq:<compiled>@<compiled>>================================
# 0000 trace 1 ( 1)
# 0002 opt_str_freeze "hi"
# 0004 leave
# 3 instructions
puts RubyVM::InstructionSequence.compile(%('hi'.itself.freeze)).disasm
# == disasm: #<ISeq:<compiled>@<compiled>>================================
# 0000 trace 1 ( 1)
# 0002 putstring "hi"
# 0004 opt_send_without_block <callinfo!mid:itself, argc:0, ARGS_SIMPLE>, <callcache>
# 0007 opt_send_without_block <callinfo!mid:freeze, argc:0, ARGS_SIMPLE>, <callcache>
# 0010 leave
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment