codefrau / SqueakJS

A Squeak Smalltalk VM in Javascript
https://squeak.js.org
MIT License
365 stars 75 forks source link

Add null as next object pointer for old objects #107

Closed ErikOnBike closed 4 years ago

ErikOnBike commented 4 years ago

This is to allow easier detecting of old/GC'd objects as discussed on Slack.

codefrau commented 4 years ago

@ErikOnBike I merged it because it doesn't hurt, and possibly helps. In reviewing the code, however, I noticed that the nextObject property is not only used in old space, but also in what I call "young space". Young space is a temporary linked list of reachable new objects (oop < 0) created in partialGC: Primitives like allInstances need to be able to enumerate al instances. Our old objects are a linked list so they are trivial to enumerate. Our new objects only live in the JS heap, there is no simple way to enumerate them. A fullGC would find them (by tenuring all new objects, appending them to the old-space linked list) but this is rather expensive. Instead, SqueakJS's partialGC does a tree walk starting from old objects that have been written to (using the dirty property), but following only new references. This visits many fewer objects, so is much faster. The result is a linked list using the nextObject references that is disconnected from the old space list. Then nextInstance simply continues with that list and thus enumerates all objects, even new ones. We don't bother removing those nextObject references.

I don't remember exactly what your use case was, just be aware that if there is an object that has a nextObject property it does not necessarily mean that it is old. (However, I think an oop >= 0 does mean it is currently in old space. The oop is made negative again when it is GC'ed).