From: Joseph Jones Date: 2015-12-17T21:13:27-07:00 Subject: [ruby-core:72347] [Ruby trunk - Feature #11786] [Open] [PATCH] micro-optimize case dispatch even harder --567387e7_e7ffa2b_16c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Joseph Jones liked your message with Boxer. On December 7, 2015 at 20:36:= 29 MST, normalperson=40yhbt.net wrote:Issue =2311786 has been reported by= Eric Wong.----------------------------------------=46eature =2311786: =5B= PATCH=5D micro-optimize case dispatch even harderhttps://bugs.ruby-lang.o= rg/issues/11786* Author: Eric Wong* Status: Open* Priority: Normal* Assig= nee: ----------------------------------------I noticed these optimization= s while working on r52931.By using a bare hash table, we avoid the overhe= ad of rb=5Fhash=5F*functions as well as the cost of translating =46IX2INT= for jumplabels.This also reduces GC overhead, as the iseq mark array no = longercarries redundant objects nor the Hash object.Summary speedup:loop=5F= whileloop2 1.000vm2=5Fcase* 1.225vm2=5Fcase=5Flit* 1.000vm2=5Fcase=5Fsmal= l* 1.162Passes all tests and specs, so probably safe to commit soon...=46= ull results:2015-12-08 03:26:19 +0000target 0: a (ruby 2.3.0dev (2015-12-= 08 trunk 52932) =5Bx86=5F64-linux=5D) at =22/home/ew/rrrr/b/ruby=22target= 1: b (ruby 2.3.0dev (2015-12-08 master 52932) =5Bx86=5F64-linux=5Dlast=5F= commit=3Dmicro-optimize case dispatch even harder) at =22/home/ew/ruby/b/= ruby=22-----------------------------------------------------------loop=5F= whileloop2i =3D 0while i i +=3D 1enda 0.10256672604009509a 0.102795576909= 55698a 0.10286601004190743a 0.10278227413073182a 0.10278794192709029b 0.1= 0288732498884201b 0.10327051300555468b 0.1026718900538981b 0.102634883020= 07318b 0.10256404406391084-----------------------------------------------= ------------vm2=5Fcasei =3D 0while i case :foo when :bar raise when :baz = raise when :boo raise when :foo i +=3D 1 endenda 0.18099936190992594a 0.1= 836838680319488a 0.1825075231026858a 0.18087006197310984a 0.1837554578669= 3692b 0.16652655508369207b 0.16673082998022437b 0.16754083498381078b 0.16= 704767406918108b 0.16648077592253685-------------------------------------= ----------------------vm2=5Fcase=5Fliti =3D 0=40ret =3D =5B =22foo=22, tr= ue, false, :sym, 6, nil, 0.1, 0xffffffffffffffff =5Ddef foo(i) =40ret=5Bi= % =40ret.size=5Dendwhile i case foo(i) when =22foo=22 then :foo when tru= e then true when false then false when :sym then :sym when 6 then :fix wh= en nil then nil when 0.1 then :float when 0xffffffffffffffff then :big en= d i +=3D 1enda 0.670805990928784a 0.6559841448906809a 0.6560367799829692a= 0.655022048857063a 0.671947613125667b 0.659776204964146b 0.6549702121410= 519b 0.6788892599288374b 0.6793588208965957b 0.6878500080201775----------= -------------------------------------------------vm2=5Fcase=5Fsmalli =3D = 0while i case :foo when :foo i +=3D 1 else raise endenda 0.16676308005116= 88a 0.16669297800399363a 0.1665362489875406a 0.16644068900495768a 0.16646= 360605955124b 0.15790433110669255b 0.15774213010445237b 0.157536468934267= 76b 0.15781028987839818b 0.15771835204213858Elapsed time: 11.069307098 (s= ec)-----------------------------------------------------------benchmark r= esults:minimum results in each 5 measurements.Execution time (sec)name a = bloop=5Fwhileloop2 0.103 0.103vm2=5Fcase* 0.078 0.064vm2=5Fcase=5Flit* 0.= 552 0.552vm2=5Fcase=5Fsmall* 0.064 0.055---=46iles-----------------------= ---------0001-micro-optimize-case-dispatch-even-harder.patch (9.34 KB)-- = https://bugs.ruby-lang.org/ --567387e7_e7ffa2b_16c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Joseph Jones liked your message with Boxer.


= On December 7, 2015 at 20:36:29 MST, normalperson=40yhbt.net wrote:
<= blockquote type=3D=22cite=22 style=3D=22border-left-style:solid;border-wi= dth:1px;margin-left:0px;padding-left:10px;=22>Issue =2311786 has been rep= orted by Eric Wong.

----------------------------------------=46eature =2311786: =5BPATCH=5D micro-optimize case dispatch even har= der
https://bugs.ruby-lang.org/issues/11786

* Author: Eri= c Wong
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
I noticed these optimizati= ons while working on r52931.

By using a bare hash table, we av= oid the overhead of rb=5Fhash=5F*
functions as well as the cost of t= ranslating =46IX2INT for jump
labels.

This also reduces G= C overhead, as the iseq mark array no longer
carries redundant objec= ts nor the Hash object.

Summary speedup:

loop=5Fwhi= leloop2 1.000
vm2=5Fcase* 1.225
vm2=5Fcase=5Flit* 1.000
vm= 2=5Fcase=5Fsmall* 1.162

Passes all tests and specs, so probabl= y safe to commit soon...

=46ull results:

2015-12-08= 03:26:19 +0000
target 0: a (ruby 2.3.0dev (2015-12-08 trunk 52932) = =5Bx86=5F64-linux=5D) at =22/home/ew/rrrr/b/ruby=22
target 1: b (rub= y 2.3.0dev (2015-12-08 master 52932) =5Bx86=5F64-linux=5D
last=5Fcom= mit=3Dmicro-optimize case dispatch even harder) at =22/home/ew/ruby/b/rub= y=22

---------------------------------------------------------= --
loop=5Fwhileloop2

i =3D 0
while i< 6=5F000=5F000 = =23 benchmark loop 2
i +=3D 1
end

a 0.102566726040= 09509
a 0.10279557690955698
a 0.10286601004190743
a 0.1027= 8227413073182
a 0.10278794192709029
b 0.10288732498884201
= b 0.10327051300555468
b 0.1026718900538981
b 0.1026348830200731= 8
b 0.10256404406391084

---------------------------------= --------------------------
vm2=5Fcase

i =3D 0
while = i<6=5F000=5F000 =23 while loop 2
case :foo
when :bar
= raise
when :baz
raise
when :boo
raise<= br /> when :foo
i +=3D 1
end
end

a 0.180= 99936190992594
a 0.1836838680319488
a 0.1825075231026858
a= 0.18087006197310984
a 0.18375545786693692
b 0.1665265550836920= 7
b 0.16673082998022437
b 0.16754083498381078
b 0.16704767= 406918108
b 0.16648077592253685

-------------------------= ----------------------------------
vm2=5Fcase=5Flit

i =3D= 0
=40ret =3D =5B =22foo=22, true, false, :sym, 6, nil, 0.1, 0xfffff= fffffffffff =5D
def foo(i)
=40ret=5Bi % =40ret.size=5D
e= nd

while i<6=5F000=5F000 =23 while loop 2
case foo(i) when =22foo=22 then :foo
when true then true
when fal= se then false
when :sym then :sym
when 6 then :fix
w= hen nil then nil
when 0.1 then :float
when 0xffffffffffffff= ff then :big
end
i +=3D 1
end

a 0.670805990= 928784
a 0.6559841448906809
a 0.6560367799829692
a 0.65502= 2048857063
a 0.671947613125667
b 0.659776204964146
b 0.654= 9702121410519
b 0.6788892599288374
b 0.6793588208965957
b = 0.6878500080201775

-------------------------------------------= ----------------
vm2=5Fcase=5Fsmall

i =3D 0
while i<= 6=5F000=5F000 =23 while loop 2
case :foo
when :foo
= i +=3D 1
else
raise
end
end

a 0.1= 667630800511688
a 0.16669297800399363
a 0.1665362489875406
a 0.16644068900495768
a 0.16646360605955124
b 0.15790433110669= 255
b 0.15774213010445237
b 0.15753646893426776
b 0.157810= 28987839818
b 0.15771835204213858

Elapsed time: 11.069307= 098 (sec)
----------------------------------------------------------= -
benchmark results:
minimum results in each 5 measurements.Execution time (sec)
name a b
loop=5Fwhileloop2 0.103 0.103<= br />vm2=5Fcase* 0.078 0.064
vm2=5Fcase=5Flit* 0.552 0.552
vm2=5F= case=5Fsmall* 0.064 0.055


---=46iles--------------------= ------------
0001-micro-optimize-case-dispatch-even-harder.patch (9.= 34 KB)


--
https://bugs.ruby-lang.org/
--567387e7_e7ffa2b_16c--