Bug #9227

use opt_aset ?

Added by Aman Gupta 4 months ago. Updated 4 months ago.

[ruby-core:58956]
Status:Closed
Priority:Normal
Assignee:Koichi Sasada
Category:-
Target version:2.1.0
ruby -v:trunk Backport:1.9.3: UNKNOWN, 2.0.0: UNKNOWN

Description

I noticed we have an opt_aset instruction, but nothing is using it. Is there some reason?

diff --git a/compile.c b/compile.c
index 812f692..9d9f14f 100644
--- a/compile.c
+++ b/compile.c
@@ -1955,6 +1955,11 @@ iseqspecializedinstruction(rbiseqt *iseq, INSN *iobj)
case idAREF: SPINSN(aref); return COMPILEOK;
}
break;
+ case 2:
+ switch (ci->mid) {
+ case idASET: SPINSN(aset); return COMPILEOK;
+ }
+ break;
}
}
if (ci->flag & VMCALLARGSSKIPSETUP) {

Associated revisions

Revision 44136
Added by Aman Gupta 4 months ago

compile.c: add opt_aset instruction for faster Hash#[]= and Array#[]=

  • compile.c (iseqspecializedinstruction): emit opt_aset instruction to optimize Hash#[]= and Array#[]= when called with Fixnum argument. [Bug #9227]

History

#1 Updated by Aman Gupta 4 months ago

Maybe some minor improvement on microbenchmarks.

Before patch:

./miniruby -I. benchmark/bmsoknucleotide.rb > /dev/null 1.54s user 0.02s system 99% cpu 1.572 total
./miniruby -I. benchmark/bm
soknucleotide.rb > /dev/null 1.58s user 0.02s system 99% cpu 1.608 total
./miniruby -I. benchmark/bmsoknucleotide.rb > /dev/null 1.54s user 0.02s system 99% cpu 1.565 total
./miniruby -I. benchmark/bm
soknucleotide.rb > /dev/null 1.57s user 0.02s system 99% cpu 1.605 total
./miniruby -I. benchmark/bmsok_nucleotide.rb > /dev/null 1.54s user 0.02s system 99% cpu 1.569 total

After patch:

./miniruby -I. benchmark/bmsoknucleotide.rb > /dev/null 1.51s user 0.02s system 99% cpu 1.535 total
./miniruby -I. benchmark/bm
soknucleotide.rb > /dev/null 1.52s user 0.02s system 99% cpu 1.545 total
./miniruby -I. benchmark/bmsoknucleotide.rb > /dev/null 1.49s user 0.02s system 99% cpu 1.515 total
./miniruby -I. benchmark/bm
soknucleotide.rb > /dev/null 1.54s user 0.02s system 99% cpu 1.567 total
./miniruby -I. benchmark/bmsok_nucleotide.rb > /dev/null 1.50s user 0.02s system 99% cpu 1.530 total

#2 Updated by Koichi Sasada 4 months ago

  • Assignee set to Koichi Sasada

I forget why I remove it.
Maybe I can't measure improvement (and possible to have trouble with this insn).

If there are no regression, I don't against to use it.

#3 Updated by Aman Gupta 4 months ago

I did some more microbenchmarks. In the Array#[Fixnum]= case, the new instruction makes a huge difference.

Before:
array aset 1.430000 0.000000 1.430000 ( 1.430370)
array aset 1.430000 0.000000 1.430000 ( 1.429422)
array aset 1.520000 0.000000 1.520000 ( 1.544912)
array aset 1.440000 0.000000 1.440000 ( 1.444454)

After:
array aset 0.910000 0.000000 0.910000 ( 0.908866)
array aset 0.910000 0.000000 0.910000 ( 0.910356)
array aset 0.890000 0.000000 0.890000 ( 0.891267)
array aset 0.920000 0.000000 0.920000 ( 0.921012)

Benchmark:

require 'benchmark'
Benchmark.bmbm(20) do |b|
ary = [1,2,3,4]
b.report('array aset') do
10000000.times do
ary[2] = 2
ary[3] = 3
end
end
end

Here's a copy of the patch that applies on trunk:

diff --git a/compile.c b/compile.c
index af11baf..25e3652 100644
--- a/compile.c
+++ b/compile.c
@@ -1955,6 +1955,11 @@ iseqspecializedinstruction(rbiseqt *iseq, INSN *iobj)
case idAREF: SPINSN(aref); return COMPILEOK;
}
break;
+ case 2:
+ switch (ci->mid) {
+ case idASET: SPINSN(aset); return COMPILEOK;
+ }
+ break;
}
}
if (ci->flag & VMCALLARGSSKIPSETUP) {

#4 Updated by Koichi Sasada 4 months ago

OK. Please enable it.

#5 Updated by Aman Gupta 4 months ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r44136.
Aman, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


compile.c: add opt_aset instruction for faster Hash#[]= and Array#[]=

  • compile.c (iseqspecializedinstruction): emit opt_aset instruction to optimize Hash#[]= and Array#[]= when called with Fixnum argument. [Bug #9227]

Also available in: Atom PDF