Project

General

Profile

Actions

Feature #11786

open

[PATCH] micro-optimize case dispatch even harder

Added by normalperson (Eric Wong) almost 9 years ago. Updated almost 9 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:71931]

Description

I noticed these optimizations while working on r52931.

By using a bare hash table, we avoid the overhead of rb_hash_*
functions as well as the cost of translating FIX2INT for jump
labels.

This also reduces GC overhead, as the iseq mark array no longer
carries redundant objects nor the Hash object.

Summary speedup:

loop_whileloop2 1.000
vm2_case* 1.225
vm2_case_lit* 1.000
vm2_case_small* 1.162

Passes all tests and specs, so probably safe to commit soon...

Full results:

2015-12-08 03:26:19 +0000
target 0: a (ruby 2.3.0dev (2015-12-08 trunk 52932) [x86_64-linux]) at "/home/ew/rrrr/b/ruby"
target 1: b (ruby 2.3.0dev (2015-12-08 master 52932) [x86_64-linux]
last_commit=micro-optimize case dispatch even harder) at "/home/ew/ruby/b/ruby"


loop_whileloop2

i = 0
while i< 6_000_000 # benchmark loop 2
i += 1
end

a 0.10256672604009509
a 0.10279557690955698
a 0.10286601004190743
a 0.10278227413073182
a 0.10278794192709029
b 0.10288732498884201
b 0.10327051300555468
b 0.1026718900538981
b 0.10263488302007318
b 0.10256404406391084


vm2_case

i = 0
while i<6_000_000 # while loop 2
case :foo
when :bar
raise
when :baz
raise
when :boo
raise
when :foo
i += 1
end
end

a 0.18099936190992594
a 0.1836838680319488
a 0.1825075231026858
a 0.18087006197310984
a 0.18375545786693692
b 0.16652655508369207
b 0.16673082998022437
b 0.16754083498381078
b 0.16704767406918108
b 0.16648077592253685


vm2_case_lit

i = 0
@ret = [ "foo", true, false, :sym, 6, nil, 0.1, 0xffffffffffffffff ]
def foo(i)
@ret[i % @ret.size]
end

while i<6_000_000 # while loop 2
case foo(i)
when "foo" then :foo
when true then true
when false then false
when :sym then :sym
when 6 then :fix
when nil then nil
when 0.1 then :float
when 0xffffffffffffffff then :big
end
i += 1
end

a 0.670805990928784
a 0.6559841448906809
a 0.6560367799829692
a 0.655022048857063
a 0.671947613125667
b 0.659776204964146
b 0.6549702121410519
b 0.6788892599288374
b 0.6793588208965957
b 0.6878500080201775


vm2_case_small

i = 0
while i<6_000_000 # while loop 2
case :foo
when :foo
i += 1
else
raise
end
end

a 0.1667630800511688
a 0.16669297800399363
a 0.1665362489875406
a 0.16644068900495768
a 0.16646360605955124
b 0.15790433110669255
b 0.15774213010445237
b 0.15753646893426776
b 0.15781028987839818
b 0.15771835204213858

Elapsed time: 11.069307098 (sec)

benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
loop_whileloop2 0.103 0.103
vm2_case* 0.078 0.064
vm2_case_lit* 0.552 0.552
vm2_case_small* 0.064 0.055


Files

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0