Skip to content

Commit

Permalink
Prune garbage collected SharedShapes
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed May 19, 2023
1 parent 4a368a2 commit 8ef2583
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
20 changes: 20 additions & 0 deletions boa_engine/src/object/shape/shared_shape/forward_transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,24 @@ impl ForwardTransition {
};
transitions.get(key).cloned()
}

/// Prunes the [`WeakGc`]s that have been garbage collected.
pub(super) fn prune_property_transitions(&self) {
let mut this = self.inner.borrow_mut();
let Some(transitions) = this.properties.as_deref_mut() else {
return;
};

transitions.retain(|_, v| v.is_upgradable());
}

/// Prunes the [`WeakGc`]s that have been garbage collected.
pub(super) fn prune_prototype_transitions(&self) {
let mut this = self.inner.borrow_mut();
let Some(transitions) = this.prototypes.as_deref_mut() else {
return;
};

transitions.retain(|_, v| v.is_upgradable());
}
}
6 changes: 6 additions & 0 deletions boa_engine/src/object/shape/shared_shape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ impl SharedShape {
if let Some(inner) = shape.upgrade() {
return Self { inner };
}

self.forward_transitions().prune_prototype_transitions();
}
let new_inner_shape = Inner {
forward_transitions: ForwardTransition::default(),
Expand All @@ -217,6 +219,8 @@ impl SharedShape {
if let Some(inner) = shape.upgrade() {
return Self { inner };
}

self.forward_transitions().prune_property_transitions();
}

let property_table = self.property_table().add_property_deep_clone_if_needed(
Expand Down Expand Up @@ -266,6 +270,8 @@ impl SharedShape {
action,
};
}

self.forward_transitions().prune_property_transitions();
}

// The attribute change transitions, didn't change from accessor to data property or vice-versa.
Expand Down
7 changes: 7 additions & 0 deletions boa_gc/src/pointers/ephemeron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ impl<K: Trace + ?Sized, V: Trace + Clone> Ephemeron<K, V> {
// `inner_ptr`.
unsafe { self.inner_ptr.get().as_ref().value().cloned() }
}

/// Checks if the [`Ephemeron`] has a value.
pub fn has_value(&self) -> bool {
// SAFETY: this is safe because `Ephemeron` is tracked to always point to a valid pointer
// `inner_ptr`.
unsafe { self.inner_ptr.get().as_ref().value().is_some() }
}
}

impl<K: Trace + ?Sized, V: Trace> Ephemeron<K, V> {
Expand Down
5 changes: 5 additions & 0 deletions boa_gc/src/pointers/weak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ impl<T: Trace> WeakGc<T> {
pub fn upgrade(&self) -> Option<Gc<T>> {
self.inner.value()
}

/// Check if the [`WeakGc`] can be upgraded.
pub fn is_upgradable(&self) -> bool {
self.inner.has_value()
}
}

impl<T: Trace> Clone for WeakGc<T> {
Expand Down

0 comments on commit 8ef2583

Please sign in to comment.