[squeak-dev] SmallInteger someInstance == nil :(

Eliot Miranda eliot.miranda at gmail.com
Tue Jul 21 15:37:53 UTC 2020


Hi Marcel, Hi Fabio,

> On Jul 21, 2020, at 5:24 AM, Marcel Taeumel <marcel.taeumel at hpi.de> wrote:
> 
> 
> Hi all!
> 
> Why is "SmallInteger someInstance == nil"?

Fabio has it right.  Let me expand a little.

Historically the blue book Smalltalk-80 and Squeak have had garbage collectors that preserved allocation order, so that all instances could reliably be obtained by

    aClass firstInstance => first instance or nil
    anInstance nextInstance => next instance or nil

and all objects could reliably be obtained by

    Object someObject => anObject
    anObject nextObject => next object or nil

When Berkeley Smalltalk introduced generation scavenging allocation order was no longer preserved and so an allInstances primitive was introduced. So VisualWorks and the Spur implementation of Squeak and several other Smalltalks depend on allInstances and allObjects primitives.

In VisualWorks we sped up finding instances of a few classes by providing allInstancesOfAny: which takes an array of classes and has better performance because it makes one pass through the heap while the array of classes stays in the processor’s cache.

Like Marcel I thought that Character allInstances SmallInteger allInstances made sense as intervals, and implemented in VisualWorks so that SmallInteger allInstances answered (SmallInteger minVal to: SmallInteger maxVal).  But the cure was worse than the disease.  Tools that determined the memory usage of classes would have to special case the immediate classes because enumerating their instances, though now possible, took a looooong time ;-).  And of course extending the idea so that allObjects includes these is impossible; there’s not enough address space.  And so I would counsel against implementing allInstances for Character SmallFloat64 and SmallInteger.  It sounds great in theory but in practice it’s a pain in the ass.

Maybe someInstance should fail for immediate classes.  But more realistically we should deprecate someInstance, nextInstance, someObject and  nextObject, with helpful but succinct error messages.

Finally, Spur optimises allInstances.  A class’s identity hash is its instances’ class index.  So a class that doesn’t have an identity hash has no instances.  But if eg one creates a set of all classes they all get an identity hash and the optimization is defeated.  We could add a step in the release process to collect all classes with an identity hash but no instances, check if they’re in any hashed collections and void their identity hash if not.  There is a hidden primitive to test and to force setting of identity hashes fir nefarious purposes such as this.  But the primitive is not for every day use so it should remain hidden IMO.

> Best,
> Marcel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20200721/b36b54f8/attachment.html>


More information about the Squeak-dev mailing list