comparing Collections. Can we do better!?

Joel Shellman joel at ikestrel.com
Mon Dec 15 17:01:14 UTC 2003


Ralph Boland wrote:

> Finally, a question.
> Would it not be a good idea for all (or at least most) Smalltalk
>  Collections (Sequenceable or otherwise) to store the hash value?
>
> I realize that this means that each at: or at:put: or similar operations
> then have to do additional work and perhaps since these operations are
> very common it might not actually be a good idea. 


Except that the hash method for an object should be very fast. And if 
you can't do it very fast, generally you should cache  the hash code in 
that object.

So then the at: and at:put operations might not be affected all that much.

-joel

For another example of hash problems:

It used to be that in the standard String class in Java, the hashCode 
was calculated every time it was called. This led to people recommending 
never to use String's as keys in Hashtables, but rather create their own 
Key class.

Fortunately, they did change String's implementation so it now caches 
the hash code.

That's another handy thing about immutable classes. You can cache the 
hash code without worrying about it changing.




More information about the Squeak-dev mailing list