Bug #9382

[patch] add opt_aref_str and opt_aset_str

Added by tmm1 (Aman Gupta) over 6 years ago. Updated over 6 years ago.

Target version:


charliesome (Charlie Somerville), normalperson (Eric Wong) and I have been working on a patch to add two new instructions:

These optimize Hash#[] and Hash#[]=, when used with a string literal key.


0010 putstring "str"
0012 opt_aref


0010 opt_aref_str , "str"

The opt_(aref|aset)_str instructions avoid str_duplicate, resulting in reduced allocation overhead. For example, this loop requires zero allocations after the patch:

hash={}; loop{ hash["key"] += 1 }

In our rails app, 2.5% of iseqs can benefit from these optimizations:

$ ruby -rconfig/environment -e' p ObjectSpace.each_object(RubyVM::InstructionSequence).count '

$ ruby -rconfig/environment -e' p ObjectSpace.each_object(RubyVM::InstructionSequence).select{ |i| i.disasm.match(/ opt_(aref|aset)_str /) }.size '

headius (Charles Nutter) is also adding a similar optimization to jruby:

I plan to commit the patch above, so if you have any feedback on the implementation please share.

Related issues

Related to Ruby master - Feature #8998: string keys for hash literals should use fstringsClosed10/08/2013Actions
Related to Ruby master - Misc #9188: r43870 make benchmark/bm_so_k_nucleotide.rb slowClosedtmm1 (Aman Gupta)12/01/2013Actions
Related to Ruby master - Bug #12855: Inconsistent keys identity in compare_by_identity Hash when using literalsClosedtmm1 (Aman Gupta)Actions

Updated by tmm1 (Aman Gupta) over 6 years ago

With the patch, "allocations per request" in our app is reduced by ~3%.

Updated by nobu (Nobuyoshi Nakada) over 6 years ago

  • Backport changed from 1.9.3: UNKNOWN, 2.0.0: UNKNOWN, 2.1: UNKNOWN to 1.9.3: DONTNEED, 2.0.0: DONTNEED, 2.1: DONTNEED
  • Status changed from Open to Closed

Updated by shyouhei (Shyouhei Urabe) over 3 years ago

  • Related to Bug #12855: Inconsistent keys identity in compare_by_identity Hash when using literals added

Also available in: Atom PDF