[Yarv-devel] case/when optimization
SASADA Koichi
ko1 at atdot.net
Sat Jun 4 02:54:27 EDT 2005
Hi,
I implemented optimization for case/when statement.
* Order of case/when statement is O(n) (n: number of when conditons)
* If when condtions are all special literals (Numeric, Symbol, String),
when dispatch can be done with hash.
example:
case x
when :a
Abody
when :b
Bbody
when :c
Cbody
else
ElseBody
end
In this situation, we don't need compare with each conditions.
Following pseudo code is more fast.
pos = hash.fetch(x, elsePos)
goto pos
posA:
Abody
goto endPos
posB:
Bbody
goto endPos
posC:
Cbody
goto endPos
elsePos:
Elsebody
endPos:
* Note that if '===' method of these classes is redefined, we can't
use above method. In current, YARV doesn't check it (must check
it).
Benchmark:
i=0
while i<1000000
i+=1
case :a
when :x
when :y
when :z
end
end
ruby 3.815000 0.010000 3.825000 ( 3.893000)
yarv 1.231000 0.000000 1.231000 ( 1.247000) old
yarv 0.370000 0.010000 0.380000 ( 0.385000) new
Regards,
SASADA Koichi
More information about the Yarv-devel
mailing list