-
Notifications
You must be signed in to change notification settings - Fork 142
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
Implement WeakRef #148
Implement WeakRef #148
Conversation
78f00b6
to
49f6783
Compare
936f51b
to
f2487ce
Compare
@bnoordhuis LMK what you think. I went this route since it seems the simplest, given the use of weak references is discouraged. I'll add FinalizationRegistry after, once we agree on the approach. If this one is taken, it would be a new "kind" of weak reference I reckon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a suggestion.
I went this route
What other routes are there? A WeakRef implementation completely detached from the WeakMap machinery?
@@ -43787,10 +43809,18 @@ static JSMapRecord *map_add_record(JSContext *ctx, JSMapState *s, | |||
mr->map = s; | |||
mr->empty = FALSE; | |||
if (s->is_weak) { | |||
pmr = get_first_weak_ref(key); | |||
JSWeakRefRecord *wr, **pwr; | |||
wr = js_malloc(ctx, sizeof(*wr)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to embed the JSWeakRefRecord inside JSMapRecord so you don't have to do this allocation? That's one fewer failure state.
If you're worried about embiggening JSMapRecord with three words, you could drop the kind
field and use the LSB of the map_record/weak_ref_data pointer to distinguish between them.
I suppose the counterargument to my suggestion is that WeakMap and WeakRef aren't very common and therefore not worth optimizing to death. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm taking the way out on this one :-P
Yes maybe a hash table on the context with the object as a key. But that might add an extra pointer too... I'm happy with how it turned out for now, happy to revisit if / when needed :-) |
Ref: #54