Skip to content

Commit 04b3cd9

Browse files
committed
use a loop rather than try_fold
1 parent ab225ad commit 04b3cd9

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -524,19 +524,18 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
524524
/// avoid allocations.
525525
pub fn eval_place_to_op(
526526
&self,
527-
place: mir::Place<'tcx>,
527+
mir_place: mir::Place<'tcx>,
528528
layout: Option<TyAndLayout<'tcx>>,
529529
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
530530
// Do not use the layout passed in as argument if the base we are looking at
531531
// here is not the entire place.
532-
let layout = if place.projection.is_empty() { layout } else { None };
532+
let layout = if mir_place.projection.is_empty() { layout } else { None };
533533

534-
let base_op = self.local_to_op(self.frame(), place.local, layout)?;
535-
536-
let op = place
537-
.projection
538-
.iter()
539-
.try_fold(base_op, |op, elem| self.operand_projection(&op, elem))?;
534+
let mut op = self.local_to_op(self.frame(), mir_place.local, layout)?;
535+
// Using `try_fold` turned out to be bad for performance, hence the loop.
536+
for elem in mir_place.projection.iter() {
537+
op = self.operand_projection(&op, elem)?
538+
}
540539

541540
trace!("eval_place_to_op: got {:?}", *op);
542541
// Sanity-check the type we ended up with.
@@ -545,12 +544,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
545544
*self.tcx,
546545
self.param_env,
547546
self.layout_of(self.subst_from_current_frame_and_normalize_erasing_regions(
548-
place.ty(&self.frame().body.local_decls, *self.tcx).ty
547+
mir_place.ty(&self.frame().body.local_decls, *self.tcx).ty
549548
)?)?,
550549
op.layout,
551550
),
552551
"eval_place of a MIR place with type {:?} produced an interpreter operand with type {:?}",
553-
place.ty(&self.frame().body.local_decls, *self.tcx).ty,
552+
mir_place.ty(&self.frame().body.local_decls, *self.tcx).ty,
554553
op.layout.ty,
555554
);
556555
Ok(op)

compiler/rustc_const_eval/src/interpret/place.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -432,31 +432,30 @@ where
432432
#[instrument(skip(self), level = "debug")]
433433
pub fn eval_place(
434434
&mut self,
435-
place: mir::Place<'tcx>,
435+
mir_place: mir::Place<'tcx>,
436436
) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
437-
let base_place = self.local_to_place(self.frame_idx(), place.local)?;
438-
439-
let final_place = place
440-
.projection
441-
.iter()
442-
.try_fold(base_place, |op, elem| self.place_projection(&op, elem))?;
437+
let mut place = self.local_to_place(self.frame_idx(), mir_place.local)?;
438+
// Using `try_fold` turned out to be bad for performance, hence the loop.
439+
for elem in mir_place.projection.iter() {
440+
place = self.place_projection(&place, elem)?
441+
}
443442

444-
trace!("{:?}", self.dump_place(final_place.place));
443+
trace!("{:?}", self.dump_place(place.place));
445444
// Sanity-check the type we ended up with.
446445
debug_assert!(
447446
mir_assign_valid_types(
448447
*self.tcx,
449448
self.param_env,
450449
self.layout_of(self.subst_from_current_frame_and_normalize_erasing_regions(
451-
place.ty(&self.frame().body.local_decls, *self.tcx).ty
450+
mir_place.ty(&self.frame().body.local_decls, *self.tcx).ty
452451
)?)?,
453-
final_place.layout,
452+
place.layout,
454453
),
455454
"eval_place of a MIR place with type {:?} produced an interpreter place with type {:?}",
456-
place.ty(&self.frame().body.local_decls, *self.tcx).ty,
457-
final_place.layout.ty,
455+
mir_place.ty(&self.frame().body.local_decls, *self.tcx).ty,
456+
place.layout.ty,
458457
);
459-
Ok(final_place)
458+
Ok(place)
460459
}
461460

462461
/// Write an immediate to a place

0 commit comments

Comments
 (0)