[Vm-dev] A BlockClosure optimization

Igor Stasenko siguctua at gmail.com
Sat May 15 05:14:31 UTC 2010


Hello,

i just thought, that we could optimize a closure-copy mechanism
to reuse a closure (BlockClosure instance), which were created before
for same context.

A mechanism of optimization can illustrated by following code.

Suppose , you having a method, which using a loop:

myMethod

 1 to: 100 do: [:i |
   dict at: i ifAbsent: [ foo bar ] ]

The idea is to copy a closure from method's literal frame just once,
and store it into temp,
and then reuse it like following:

myMethod
| closure |
 1 to: 100 do: [:i |
   dict at: i ifAbsent: (closure ifNil: [ closure := [ foo bar ] ] ) ]

----------

A simple benchmark shows that we could gain from it:

[ 1000000 timesRepeat: [ [ 1+1] value ] ] timeToRun
670

[
| closure |  closure := nil.
1000000 timesRepeat: [
	(closure ifNil: [ closure := [ 1+1] ]) value ]
] timeToRun
518

As you can see, even implemented in smalltalk (without any changes to
VM) it shows
a significant performance boost.

Of course, if we put something, which loads processor by real work,
instead of just [1+1],
the difference will be less significant.

But apart from this, lying the fact, that copying closure object each
time means memory allocation,
and hence leads to more frequent GC.




-- 
Best regards,
Igor Stasenko AKA sig.


More information about the Vm-dev mailing list