Skip to content

Commit 8188bd4

Browse files
committed
avoid marking as immutable what is already immutable
this has been demonstrated to help performance
1 parent 29c95e9 commit 8188bd4

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

compiler/rustc_const_eval/src/const_eval/machine.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -714,11 +714,14 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
714714
_kind: mir::RetagKind,
715715
val: &ImmTy<'tcx, CtfeProvenance>,
716716
) -> InterpResult<'tcx, ImmTy<'tcx, CtfeProvenance>> {
717+
// If it's a frozen shared reference that's not already immutable, make it immutable.
718+
// (Do nothing on `None` provenance, that cannot store immutability anyway.)
717719
if let ty::Ref(_, ty, mutbl) = val.layout.ty.kind()
718720
&& *mutbl == Mutability::Not
721+
&& val.to_scalar_and_meta().0.to_pointer(ecx)?.provenance.is_some_and(|p| !p.immutable())
722+
// That next check is expensive, that's why we have all the guards above.
719723
&& ty.is_freeze(*ecx.tcx, ecx.param_env)
720724
{
721-
// This is a frozen shared reference, mark it immutable.
722725
let place = ecx.ref_to_mplace(val)?;
723726
let new_place = place.map_provenance(|p| p.map(CtfeProvenance::as_immutable));
724727
Ok(ImmTy::from_immediate(new_place.to_ref(ecx), val.layout))

compiler/rustc_const_eval/src/interpret/operand.rs

+11
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ impl<Prov: Provenance> Immediate<Prov> {
9393
Immediate::Uninit => bug!("Got uninit where a scalar pair was expected"),
9494
}
9595
}
96+
97+
/// Returns the scalar from the first component and optionally the 2nd component as metadata.
98+
#[inline]
99+
#[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980)
100+
pub fn to_scalar_and_meta(self) -> (Scalar<Prov>, MemPlaceMeta<Prov>) {
101+
match self {
102+
Immediate::ScalarPair(val1, val2) => (val1, MemPlaceMeta::Meta(val2)),
103+
Immediate::Scalar(val) => (val, MemPlaceMeta::None),
104+
Immediate::Uninit => bug!("Got uninit where a scalar or scalar pair was expected"),
105+
}
106+
}
96107
}
97108

98109
// ScalarPair needs a type to interpret, so we often have an immediate and a type together

compiler/rustc_const_eval/src/interpret/place.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,7 @@ where
406406
let pointee_type =
407407
val.layout.ty.builtin_deref(true).expect("`ref_to_mplace` called on non-ptr type").ty;
408408
let layout = self.layout_of(pointee_type)?;
409-
let (ptr, meta) = match **val {
410-
Immediate::Scalar(ptr) => (ptr, MemPlaceMeta::None),
411-
Immediate::ScalarPair(ptr, meta) => (ptr, MemPlaceMeta::Meta(meta)),
412-
Immediate::Uninit => throw_ub!(InvalidUninitBytes(None)),
413-
};
409+
let (ptr, meta) = val.to_scalar_and_meta();
414410

415411
// `ref_to_mplace` is called on raw pointers even if they don't actually get dereferenced;
416412
// we hence can't call `size_and_align_of` since that asserts more validity than we want.

0 commit comments

Comments
 (0)