ctfe: clarify skipping the interning walk
Reorganizes the previous commits to have a single exit-point to avoid doing the potentially costly walk. Also moves the relocations tests before the interior mutability test: only references are important when checking for `UnsafeCell`s and we're checking if there are any to decide to avoid the walk anyways.
This commit is contained in:
parent
266bab2ab0
commit
18cbc19de2
@ -173,18 +173,42 @@ fn visit_aggregate(
|
||||
//
|
||||
// As an optimization, however, if the allocation does not contain any pointers: we don't
|
||||
// need to do the walk. It can be costly for big arrays for example (e.g. issue #93215).
|
||||
let is_walk_needed = |mplace: &MPlaceTy<'tcx>| -> InterpResult<'tcx, bool> {
|
||||
// ZSTs cannot contain pointers, we can avoid the interning walk.
|
||||
if mplace.layout.is_zst() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
// Now, check whether this alloc contains reference types (as relocations).
|
||||
|
||||
// FIXME(lqd): checking the size and alignment could be expensive here, only do the
|
||||
// following for the potentially bigger aggregates like arrays and slices.
|
||||
let Some((size, align)) = self.ecx.size_and_align_of_mplace(&mplace)? else {
|
||||
// We could be dealing with an extern type here in the future, so we do the regular
|
||||
// walk.
|
||||
return self.walk_aggregate(mplace, fields);
|
||||
// We do the walk if we can't determine the size of the mplace: we may be dealing
|
||||
// with extern types here in the future.
|
||||
return Ok(true);
|
||||
};
|
||||
|
||||
let Some(alloc) = self.ecx.get_ptr_alloc(mplace.ptr, size, align)? else {
|
||||
// ZSTs cannot contain pointers, so we can skip them.
|
||||
return Ok(());
|
||||
// If there are no refs or relocations in this allocation, we can avoid the interning
|
||||
// walk.
|
||||
if let Some(alloc) = self.ecx.get_ptr_alloc(mplace.ptr, size, align)?
|
||||
&& !alloc.has_relocations() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
// In the general case, we do the walk.
|
||||
Ok(true)
|
||||
};
|
||||
|
||||
// If this allocation contains no references to intern, we avoid the potentially costly
|
||||
// walk.
|
||||
//
|
||||
// We can do this before the checks for interior mutability below, because only references
|
||||
// are relevant in that situation, and we're checking if there are any here.
|
||||
if !is_walk_needed(mplace)? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(def) = mplace.layout.ty.ty_adt_def() {
|
||||
if Some(def.did()) == self.ecx.tcx.lang_items().unsafe_cell_type() {
|
||||
// We are crossing over an `UnsafeCell`, we can mutate again. This means that
|
||||
@ -198,11 +222,6 @@ fn visit_aggregate(
|
||||
}
|
||||
}
|
||||
|
||||
if !alloc.has_relocations() {
|
||||
// There are no refs or relocations in this allocation, we can skip the interning walk.
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
self.walk_aggregate(mplace, fields)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user