Monday, December 27, 2010

Smalltalk is typed

In the last post, I mentioned that I had found some fascinating material for chapter 6 (in Fundamentals' volume 2). Here's a bit that has profound consequences: Smalltalk is typed. Well, of course, receivers have to be of the same type, right? Yes, of course. And that means one of the most fundamental types of objects in the image will cause endless performance issues because each one of them is an instance of a different class. But which objects are singletons and instances of a singleton class?

Classes.

So that means that whenever you send messages like #respondsTo:, or #withAllSubclasses, or #superclass, or #isKindOf:, you will effectively cause message sends that cannot possibly be cached (particularly when you do them in a loop). Consequently, performance stinks. I know, because I did the experiment of replacing VisualWorks' sorted collection method dictionaries with actually hashed method dictionaries. The performance of the enumeration part of this expression

Object withAllSubclasses reverse do: [:each | each yourself]

went up by 60-70%! But there are many other such cases hidden all over the place. For example, put all those classes in a Set and then look them up to see if they are there. It happens --- slowly, but it does. Now do the same with an identity set. Bam!!! It's two times faster!!! But why?

The message #= is implemented in Object as #==. However, when the message #= is sent with a multitude of receiver classes, each send requires a lookup (and an expensive lookup at that, because you have to go all the way up to Object). If you use an identity set, then you send #== directly --- which in most Smalltalks is not even a message send. Thus, there are no lookups when comparing objects, and no wonder the code runs way faster.

Huh... Smalltalk is typed, and there are consequences to that...

Among other things, chapter 6 explores actual production problems in which this kind of stuff makes a huge difference. Hopefully I can finish it off soon so I can move on to chapter 7, On Threading.

No comments: