-
-
Notifications
You must be signed in to change notification settings - Fork 670
Implement FinalizationRegistry and WeakRef #2593
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
Conversation
Regarding making FRs GC-able, would something like this work? let ptrToRegistry = new Map<usize,FinalizationRegistry[]>();
function finalizeImpl(ptr: usize): void {
if (!ptrToRegistry.has(ptr)) return;
let registries = ptrToRegistry.get(ptr);
for (let i = 0, k = registries.length; i < k; ++i) {
registries[i].finalize(ptr);
}
ptrToRegistry.delete(ptr);
} so when |
Yes, I think that's better. |
I guess my problem with this is that GC pressure is kind of amplified. It might be worth pooling those arrays. |
Fwiw finalization was implemented using as-disposable already and it had to deal with quite a lot of problems |
The current implementation already uses a 2 step lookup so it does not hurt to just support token.
@dcodeIO There are still a lot of tests to write but this change will be huge due to the code change to GCs to support I'd argue it's a better GC hook API than However, it will break as-disposable or anything that relies on
After the |
I'm glad this is at least being talked about. Can we not break something that lunatic devs rely on please? It would be nice to rely on __finalize as a feature. |
This PR has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions! |
This PR has been automatically closed due to lack of recent activity, but feel free to reopen it as long as you merge in the main branch afterwards. |
Resolve #1299
All tests fail right now because fixtures are not recreated.
This is created to gather feedbacks.
One debug output is attached to see the impact of this change.
AFAIK, there is no impact on release builds when the feature is not used.
Current quirks of
FinalizationRegistry
:register
only takes 2 arguments and the 3rd argument is assumed to be the object.register
always take 3 arguments because undefined is not representable.Each object can only be registered once per registry.Simplify the implementation for what I believe is the most typical use case (see below).Each
token
can only be registered once.Repeated
register
andunregister
will consume extra memory until the target object is collected.This is because new book keeping entries are created in
register
but not removed inunregister
.The reason is again, to keep the implementation simple and a typical use case should only
unregister
once, typically in an explicit "close" method (see below).Even in the case of duplicated entries,
finalizer
is still guaranteed to be called once per token.