immutable-tuple
immutable-tuple copied to clipboard
leaks when using primitives; fixable with WeakRef
The description claims "no leaks", but if you do Tuple(primitive)
, that creates a UniversalWeakMap
which can never be garbage collected. If you're creating a lot of tuples with a lot of different primitives, that can be a problem.
Now that we have WeakRef
and FinalizationRegistry
, I'm pretty sure it's possible to do better, so that there's actually no leaks: that is, no matter how many tuples you create and how you create them, once they've all been GC'd you're guaranteed that no additional memory is held.
The necessary changes are:
- When
set(key, value)
is called on aUniversalWeakMap
, store aWeakRef
tovalue
instead of storingvalue
directly, and registervalue
in aFinalizationRegistry
so that when it is collectedkey
is cleared from the map. - Use a global
WeakMap
to store a map from each created tuple to all of its parent nodes.
Then the only strong references to UniversalWeakMap
objects (except the root) are from the tuples stored in those maps. Once all the tuples stored in a given UniversalWeakMap
are GC'd, it can be GC'd as well.
The only caveat with this approach is if the user uses a tuple as a key in a weak collection. In that case the tuple can still get collected even if it was only holding primitives, which might be surprising. But that's unavoidable if avoiding leaks.