Repository navigation
BSOA Garbage Collection
In BSOA, object model objects are all really just 'references' to rows within tables. The .NET Garbage Collector will clean up the objects, but BSOA itself must identify rows that are not reachable from the root object and remove them. However, users may create objects which aren't reachable and hold onto them, so BSOA must keep this data somewhere else to keep those objects consistent.
- Logs must not write with unreachable rows in them
- Object model objects must be kept valid and pointing to the same data
- Relatively low performance impact on BSOA save (which must call Collect())
- Very low impact for read-only usage (which shouldn't trigger Collect() itself)
Garbage Collection should be primarily triggered on Write, when unreachable rows must be removed to avoid being written. Collection may also be needed if many objects have been created, taking a lot of memory. Garbage Collection should not be triggered on read only use - after loading a log and when enumerating existing items. Triggering Garbage collection on Write and sometimes when new rows are added cleanly distinguishes read only use from other scenarios.
BSOA Collection must identify non-reachable rows and remove them. Keeping track of which rows are reachable dynamically would be complex and likely impact performance, so BSOA instead walks all objects from the root during collections to identify reachable rows at a point in time. This is done on the base Database and Table classes themselves, which avoids creating any object model instances for the process.
"Reachable" rows refer to all rows which can be reached by traversing properties from the root object. All of these rows must be kept by Garbage Collection.
All rows not in the reachable set are "Non-Reachable", and must be removed by Garbage Collection so that they aren't serialized out.
"Unreachable" rows are a slightly different set: this is all rows reachable by the non-reachable rows. This includes all non-reachable rows, AND all reachable rows which some non-reachable row references.
- Walk from the root object, identifying every reachable row (recursing through all Ref and RefList column references).
- Walk every non-reachable object to find the 'unreachable' set (see above).
- Assign a row index in a temporary database for every unreachable row, and copy them all there.
- Collect all non-reachable rows by swapping them to the end of the table and removing them.
- Ensure object model instances which moved will be updated with the equivalent current table and row index.
Users should not have to care about BSOA's storage model. Users will expect to create objects and have them remember their data, whether or not they are added to a root object collection. If BSOA must not write unreachable rows, but object model objects could still be around and pointing to them, then unreachable rows must be moved into another database and object model instances, must be remapped to the new copy of the data. Also, references should be set up so that if none of the unreachable row objects are still in memory, the .NET garbage collector should know it can clean up this other database.
Updating object model instances could be done by keeping WeakReferences to them, hooking up events, or perhaps by reference counting them and adding finalizers. However, these mechanisms allocate additional per-object data and cause a significant performance penalty for read only scenarios. For example, just creating a WeakReference for each object model object causes enumeration speed to become 13x slower.
Instead, we use a trick - object model instances ask the table they reference to update themselves before use. The update call is very low cost if the table is already up-to-date.
In Garbage Collection, we find all reachable rows. These are left in the original database. Any unreachable rows, before being deleted, are copied to a temporary database instance. This way, if any object model instances refer to them, those object model instances will still have the data they had before. Unreachable rows are then swapped to the end of the table and removed, deleting them from the "main" database.
Next, Garbage Collection creates a new "successor" Table instance. This table copy replaces the current table in the Database and takes over ownership of the columns.
The old Table instance (which existing object model instances will be referencing) has a 'RowUpdater' set on it, which instructs it to redirect any object model objects to the new row index in either the successor or temp table. The Table exposes an EnsureCurrent(IRow) call which object model instances must call before getting or setting any property.
With this design, we don't have to keep WeakReferences of every object model instance, and don't have to create constructor and finalizer logic to reference count, both of which add significant overhead to all usage scenarios.
In addition, the normal .NET garbage collector will automatically clean up the "temp" database when no object model instances are left pointing to it. After Garbage Collection, the only objects referencing the non-successor table are object model instances which haven't been updated. Once every instance is updated or goes out of scope, the .NET garbage collector will recognize the temp table itself as unreferenced and clean it up also.