-
-
Notifications
You must be signed in to change notification settings - Fork 411
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
[Merged by Bors] - Implement WeakSet
#2586
Conversation
Test262 conformance changes
Fixed tests (180):
|
Codecov Report
@@ Coverage Diff @@
## main #2586 +/- ##
==========================================
- Coverage 49.38% 48.86% -0.52%
==========================================
Files 381 393 +12
Lines 37906 39061 +1155
==========================================
+ Hits 18720 19089 +369
- Misses 19186 19972 +786
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
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.
Thanks for the preliminar work! Though, this implementation has the problem of not freeing the inner EphemeronBox
when the inner value invalidates. I think this could only be solved by creating a WeakMap
structure with direct support from our GC. The GC would then track every WeakMap
to free the invalidated boxes from the map.
Anyways, I think we could technically merge this as it is and improve the design later, but let me know what you think first :)
/// Insert the value into the set. | ||
pub fn add(&mut self, value: &JsObject) -> bool { | ||
self.inner.insert(WeakSetObject(WeakGc::new(value.inner()))) | ||
} | ||
|
||
/// Remove the value from the set, and return `true` if it was present. | ||
pub fn delete(&mut self, value: &JsObject) -> bool { | ||
self.inner | ||
.shift_remove(&WeakSetObject(WeakGc::new(value.inner()))) | ||
} | ||
|
||
/// Return `true` if an equivalent to value exists in the set. | ||
pub fn contains(&self, value: &JsObject) -> bool { | ||
self.inner | ||
.contains(&WeakSetObject(WeakGc::new(value.inner()))) | ||
} |
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 think implementing a special HashMap
using ephemerons directly in our GC would avoid having to create a WeakSetObject
on each access.
Yes I was thinking the same thing, but I'm not sure where exactly that would fit in our gc process and how much we would have to adjust it. My first instinct is that we would want to check for any collected weak refs in |
Probably by doing something similar as the strong pointers: a queue of |
@jedel1043 I implemented a |
/// A map that holds weak references to its keys and is traced by the garbage collector. | ||
#[derive(Clone, Debug, Default, Trace, Finalize)] | ||
pub struct WeakMap<K: Trace + Sized + 'static, V: Trace + Sized + 'static> { | ||
pub(crate) inner: Gc<GcRefCell<HashMap<WeakGc<K>, V>>>, |
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.
While this works, I think it would be better to directly use a HashSet<Ephemeron<K, V>>
to optimize this, since right now it is using an Ephemeron<K, ()>
and a V
.
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.
Hm, realized that to do this we'll probably have to implement our own HashMap
...
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.
Yeah, this should work for now. Maybe in the future we'll have to inline HashMap
to optimize the memory of this.
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.
Looks nice! We can improve this in the future by implementing our own HashMap
using Ephemeron
s, so maybe we should open an issue for it?
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.
This is looking pretty good. I just had a question on one small thing.
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.
Looks good to me. Nice work on this!
bors r+ |
Pull request successfully merged into main. Build succeeded: |
This Pull Request changes the following:
WeakSet
buildin object.WeakSet
object #2009