Skip to content

Commit b71b6a1

Browse files
committed
Small clean ups after evaluating element segments only once
Addressing bytecodealliance#12985 (review)
1 parent d06fd9e commit b71b6a1

3 files changed

Lines changed: 25 additions & 12 deletions

File tree

crates/wasmtime/src/runtime/store.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2269,7 +2269,10 @@ impl StoreOpaque {
22692269
// SAFETY: the instance's GC roots will remain valid for the
22702270
// duration of this GC cycle.
22712271
unsafe {
2272-
instance.handle.get_mut().trace_roots(gc_roots_list);
2272+
instance
2273+
.handle
2274+
.get_mut()
2275+
.trace_element_segment_roots(gc_roots_list);
22732276
}
22742277
}
22752278
log::trace!("End trace GC roots :: instance");

crates/wasmtime/src/runtime/vm/instance.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,24 +214,23 @@ impl Instance {
214214
Ok(ret)
215215
}
216216

217-
/// Trace GC roots inside this `Instance`.
218-
///
219-
/// NB: This instance's `vmctx` roots are traced separately in
220-
/// `Store::trace_vmctx_roots`.
217+
/// Trace element segment GC roots inside this `Instance`.
221218
///
222219
/// # Safety
223220
///
224221
/// This instance must live for the duration of the associated GC cycle.
225222
#[cfg(feature = "gc")]
226-
pub(crate) unsafe fn trace_roots(self: Pin<&mut Self>, gc_roots: &mut crate::vm::GcRootsList) {
227-
// SAFETY: not moving data out of `self`.
228-
let passive_elements = &mut unsafe { self.get_unchecked_mut() }.passive_elements;
229-
230-
for segment in passive_elements {
223+
pub(crate) unsafe fn trace_element_segment_roots(
224+
self: Pin<&mut Self>,
225+
gc_roots: &mut crate::vm::GcRootsList,
226+
) {
227+
for segment in self.passive_elements_mut().iter_mut() {
231228
if let Some((wasmtime_environ::NeedsGcRooting::Yes, elems)) = segment {
232229
for e in elems {
233-
let root: SendSyncPtr<ValRaw> = SendSyncPtr::from(e);
234-
let root: SendSyncPtr<super::VMGcRef> = root.cast();
230+
let Some(root) = e.as_vmgc_ref_ptr() else {
231+
continue;
232+
};
233+
let root: SendSyncPtr<super::VMGcRef> = root.into();
235234

236235
// Safety: We know this is a type that needs GC rooting and
237236
// the lifetime is implied by our safety contract.

crates/wasmtime/src/runtime/vm/vmcontext.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,17 @@ impl ValRaw {
17381738
assert!(cfg!(feature = "gc") || exnref == 0);
17391739
exnref
17401740
}
1741+
1742+
/// Convert this `&ValRaw` into a pointer to its inner `VMGcRef`.
1743+
#[cfg(feature = "gc")]
1744+
pub(crate) fn as_vmgc_ref_ptr(&self) -> Option<NonNull<crate::vm::VMGcRef>> {
1745+
if self.get_anyref() == 0 {
1746+
return None;
1747+
}
1748+
let ptr = &raw const self.anyref;
1749+
let ptr = NonNull::new(ptr.cast_mut()).unwrap();
1750+
Some(ptr.cast())
1751+
}
17411752
}
17421753

17431754
/// An "opaque" version of `VMContext` which must be explicitly casted to a

0 commit comments

Comments
 (0)