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

Mark header of rooted ephemerons when tracing #3049

Merged
merged 5 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion boa_gc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl Collector {
let unreachables = Self::mark_heap(&gc.strong_start, &gc.weak_start, &gc.weak_map_start);

// Only finalize if there are any unreachable nodes.
if !unreachables.strong.is_empty() || unreachables.weak.is_empty() {
if !unreachables.strong.is_empty() || !unreachables.weak.is_empty() {
// Finalize all the unreachable nodes.
// SAFETY: All passed pointers are valid, since we won't deallocate until `Self::sweep`.
unsafe { Self::finalize(unreachables) };
Expand Down Expand Up @@ -367,12 +367,17 @@ impl Collector {

// === Weak mark phase ===
//
//
// 1. Get the naive list of ephemerons that are supposedly dead or their key is dead and
// trace all the ephemerons that have roots and their keys are live. Also remove from
// this list the ephemerons that are marked but their value is dead.
while let Some(eph) = weak.get() {
// SAFETY: node must be valid as this phase cannot drop any node.
let eph_ref = unsafe { eph.as_ref() };
let header = eph_ref.header();
if header.roots() > 0 {
jedel1043 marked this conversation as resolved.
Show resolved Hide resolved
header.mark();
}
// SAFETY: the garbage collector ensures `eph_ref` always points to valid data.
if unsafe { !eph_ref.trace() } {
pending_ephemerons.push(eph);
Expand Down
12 changes: 8 additions & 4 deletions boa_gc/src/test/weak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,22 @@ fn eph_allocation_chains() {
let weak = WeakGc::new(&cloned_gc);
let wrap = Gc::new(weak);

assert_eq!(*wrap.upgrade().expect("weak is live"), "foo");
assert_eq!(wrap.upgrade().as_deref().map(String::as_str), Some("foo"));

let eph = Ephemeron::new(&wrap, 3);

drop(cloned_gc);
force_collect();

assert_eq!(eph.value().expect("weak is still live"), 3);
assert_eq!(wrap.upgrade().as_deref().map(String::as_str), Some("foo"));
assert_eq!(eph.value(), Some(3));

drop(gc_value);
force_collect();
assert!(wrap.upgrade().is_none());
assert_eq!(eph.value(), Some(3));

drop(wrap);
force_collect();
assert!(eph.value().is_none());
}
});
Expand All @@ -91,7 +95,7 @@ fn eph_basic_alloc_dump_test() {
let eph = Ephemeron::new(&gc_value, 4);
let _fourth = Gc::new("tail");

assert_eq!(eph.value().expect("must be live"), 4);
assert_eq!(eph.value(), Some(4));
});
}

Expand Down