Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leaks when using primitives; fixable with WeakRef #8

Open
bakkot opened this issue Jan 28, 2024 · 0 comments
Open

leaks when using primitives; fixable with WeakRef #8

bakkot opened this issue Jan 28, 2024 · 0 comments

Comments

@bakkot
Copy link

bakkot commented Jan 28, 2024

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:

  1. When set(key, value) is called on a UniversalWeakMap, store a WeakRef to value instead of storing value directly, and register value in a FinalizationRegistry so that when it is collected key is cleared from the map.
  2. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant