Please use accessors!

Andres Valloud sqrmax at cvtci.com.ar
Sun Aug 1 21:34:59 UTC 1999


Hi.

> In Squeak, what is the cost of using an accessor vs. a direct call to the
> ivar?  In VisualAge, for example, the compiler optimizes a send to an
> accessor and makes it equivalent to a direct access.  At least, I believe it
> is - I'll have to run tests to verify.  I think this is true with other
> dialects as well.

This has a relative penalty in speed. If your sources are already free
of other more significant performance issues (like pointless object
creation, for instance), then you can get an additional speed boost by
replacing all "self accessorToAVariable" with "theVariableItself".

Of course, there's a non trivial balance you should meet. For example,
if the accessors are plain, ie ^theVariable, then that's good (if your
design didn't require accessors in the first place)...

I remember I did this with my compression stuff, because at first I was
unsure of the proper internal structure of my objects, so I used
accessors to avoid rewriting everything all the time. When I found the
(a) proper way to do it, I first attacked the worst performance
bottlenecks (MessageTally). And then accessors started to pop up in the
MessageTally, taking a considerable amount of time to run. The worst was
that some accessors were in superclasses, so each access to a variable
took send, fail, lookup in the superclass, and then send. This was not
acceptable, so I changed the self references to the variable references.
Although I can't quote the figure by heart, the improvement was
significant. Furthermore, I could then delete methods and that was nice
too.

Andres.





More information about the Squeak-dev mailing list