Skip to content
New issue

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

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

Already on GitHub? # to your account

WeakMap entries with tuple keys might get lost after GC #3

Open
0f-0b opened this issue Aug 30, 2024 · 2 comments
Open

WeakMap entries with tuple keys might get lost after GC #3

0f-0b opened this issue Aug 30, 2024 · 2 comments

Comments

@0f-0b
Copy link

0f-0b commented Aug 30, 2024

Run the code below with node --expose-gc. One might expect the output to be value, but it actually prints undefined.

import { Tuple } from "libtuple";
import { setTimeout } from "node:timers/promises";

const data = new WeakMap();
data.set(Tuple("key"), "value");
await setTimeout();
gc();
console.log(data.get(Tuple("key")));

I think this should be documented under the “Limitations” section of the readme, because I don't see a way to fix this without causing memory leaks.

@seanmorris
Copy link
Owner

hmmm, I'll need to really think about this one.

@seanmorris
Copy link
Owner

I wonder if a comparison to registered symbols would be helpful in deciding on the correct behavior would be helpful here.

If I recall correctly, registered symbols can't participate in WeakMaps or WeakSets, but I wonder how the memory is handled under the hood for those.

If you call Symbol.for('foo'), and then the resulting value is gc'ed, and then you call Symbol.for('foo') again, do you get back the "same" value, or is it just the "same" for "all" intents and purposes?

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

No branches or pull requests

2 participants