From 04b3cd9f7c8366490590d839ab814d03406eed4a Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 11 Jul 2022 13:42:08 -0400 Subject: [PATCH] use a loop rather than try_fold --- .../rustc_const_eval/src/interpret/operand.rs | 19 +++++++------- .../rustc_const_eval/src/interpret/place.rs | 25 +++++++++---------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index 4f8dbdeabae..1465b986293 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -524,19 +524,18 @@ pub fn place_to_op( /// avoid allocations. pub fn eval_place_to_op( &self, - place: mir::Place<'tcx>, + mir_place: mir::Place<'tcx>, layout: Option>, ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> { // Do not use the layout passed in as argument if the base we are looking at // here is not the entire place. - let layout = if place.projection.is_empty() { layout } else { None }; + let layout = if mir_place.projection.is_empty() { layout } else { None }; - let base_op = self.local_to_op(self.frame(), place.local, layout)?; - - let op = place - .projection - .iter() - .try_fold(base_op, |op, elem| self.operand_projection(&op, elem))?; + let mut op = self.local_to_op(self.frame(), mir_place.local, layout)?; + // Using `try_fold` turned out to be bad for performance, hence the loop. + for elem in mir_place.projection.iter() { + op = self.operand_projection(&op, elem)? + } trace!("eval_place_to_op: got {:?}", *op); // Sanity-check the type we ended up with. @@ -545,12 +544,12 @@ pub fn eval_place_to_op( *self.tcx, self.param_env, self.layout_of(self.subst_from_current_frame_and_normalize_erasing_regions( - place.ty(&self.frame().body.local_decls, *self.tcx).ty + mir_place.ty(&self.frame().body.local_decls, *self.tcx).ty )?)?, op.layout, ), "eval_place of a MIR place with type {:?} produced an interpreter operand with type {:?}", - place.ty(&self.frame().body.local_decls, *self.tcx).ty, + mir_place.ty(&self.frame().body.local_decls, *self.tcx).ty, op.layout.ty, ); Ok(op) diff --git a/compiler/rustc_const_eval/src/interpret/place.rs b/compiler/rustc_const_eval/src/interpret/place.rs index 39a737f4aca..2001359d199 100644 --- a/compiler/rustc_const_eval/src/interpret/place.rs +++ b/compiler/rustc_const_eval/src/interpret/place.rs @@ -432,31 +432,30 @@ pub fn local_to_place( #[instrument(skip(self), level = "debug")] pub fn eval_place( &mut self, - place: mir::Place<'tcx>, + mir_place: mir::Place<'tcx>, ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> { - let base_place = self.local_to_place(self.frame_idx(), place.local)?; + let mut place = self.local_to_place(self.frame_idx(), mir_place.local)?; + // Using `try_fold` turned out to be bad for performance, hence the loop. + for elem in mir_place.projection.iter() { + place = self.place_projection(&place, elem)? + } - let final_place = place - .projection - .iter() - .try_fold(base_place, |op, elem| self.place_projection(&op, elem))?; - - trace!("{:?}", self.dump_place(final_place.place)); + trace!("{:?}", self.dump_place(place.place)); // Sanity-check the type we ended up with. debug_assert!( mir_assign_valid_types( *self.tcx, self.param_env, self.layout_of(self.subst_from_current_frame_and_normalize_erasing_regions( - place.ty(&self.frame().body.local_decls, *self.tcx).ty + mir_place.ty(&self.frame().body.local_decls, *self.tcx).ty )?)?, - final_place.layout, + place.layout, ), "eval_place of a MIR place with type {:?} produced an interpreter place with type {:?}", - place.ty(&self.frame().body.local_decls, *self.tcx).ty, - final_place.layout.ty, + mir_place.ty(&self.frame().body.local_decls, *self.tcx).ty, + place.layout.ty, ); - Ok(final_place) + Ok(place) } /// Write an immediate to a place