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

Implement WeakRef #148

Merged
merged 2 commits into from
Nov 28, 2023
Merged

Implement WeakRef #148

merged 2 commits into from
Nov 28, 2023

Conversation

saghul
Copy link
Contributor

@saghul saghul commented Nov 27, 2023

Ref: #54

@saghul saghul force-pushed the weakref branch 3 times, most recently from 78f00b6 to 49f6783 Compare November 27, 2023 23:26
@saghul saghul marked this pull request as ready for review November 27, 2023 23:35
@saghul saghul requested a review from bnoordhuis November 27, 2023 23:36
@saghul saghul marked this pull request as draft November 27, 2023 23:36
@saghul saghul force-pushed the weakref branch 2 times, most recently from 936f51b to f2487ce Compare November 28, 2023 00:20
@saghul saghul marked this pull request as ready for review November 28, 2023 00:21
@saghul
Copy link
Contributor Author

saghul commented Nov 28, 2023

@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.

Copy link
Contributor

@bnoordhuis bnoordhuis left a 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));
Copy link
Contributor

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. :)

Copy link
Contributor Author

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

@saghul
Copy link
Contributor Author

saghul commented Nov 28, 2023

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 :-)

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

Successfully merging this pull request may close these issues.

2 participants