about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src/const_eval/machine.rs
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2023-11-28 22:30:55 +0100
committerRalf Jung <post@ralfj.de>2023-12-07 17:46:48 +0100
commit8188bd45489a1dbd7e3f61d33233576e36f9c48c (patch)
tree7a2db1de253674db8a62be010ce61139dcc50211 /compiler/rustc_const_eval/src/const_eval/machine.rs
parent29c95e98e318be711169862c996982c7dffd2372 (diff)
downloadrust-8188bd45489a1dbd7e3f61d33233576e36f9c48c.tar.gz
rust-8188bd45489a1dbd7e3f61d33233576e36f9c48c.zip
avoid marking as immutable what is already immutable
this has been demonstrated to help performance
Diffstat (limited to 'compiler/rustc_const_eval/src/const_eval/machine.rs')
-rw-r--r--compiler/rustc_const_eval/src/const_eval/machine.rs5
1 files changed, 4 insertions, 1 deletions
diff --git a/compiler/rustc_const_eval/src/const_eval/machine.rs b/compiler/rustc_const_eval/src/const_eval/machine.rs
index 761337126ef..9aaf6c510d5 100644
--- a/compiler/rustc_const_eval/src/const_eval/machine.rs
+++ b/compiler/rustc_const_eval/src/const_eval/machine.rs
@@ -714,11 +714,14 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
         _kind: mir::RetagKind,
         val: &ImmTy<'tcx, CtfeProvenance>,
     ) -> InterpResult<'tcx, ImmTy<'tcx, CtfeProvenance>> {
+        // If it's a frozen shared reference that's not already immutable, make it immutable.
+        // (Do nothing on `None` provenance, that cannot store immutability anyway.)
         if let ty::Ref(_, ty, mutbl) = val.layout.ty.kind()
             && *mutbl == Mutability::Not
+            && val.to_scalar_and_meta().0.to_pointer(ecx)?.provenance.is_some_and(|p| !p.immutable())
+            // That next check is expensive, that's why we have all the guards above.
             && ty.is_freeze(*ecx.tcx, ecx.param_env)
         {
-            // This is a frozen shared reference, mark it immutable.
             let place = ecx.ref_to_mplace(val)?;
             let new_place = place.map_provenance(|p| p.map(CtfeProvenance::as_immutable));
             Ok(ImmTy::from_immediate(new_place.to_ref(ecx), val.layout))