Skip to content

Commit

Permalink
Avoid cloning LocalDecls.
Browse files Browse the repository at this point in the history
`DerefChecker` can just hold a reference instead. This avoids quite a
lot of allocations for some benchmarks.
  • Loading branch information
nnethercote committed Jun 29, 2023
1 parent 8d7084d commit 7e786e8
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions compiler/rustc_mir_transform/src/deref_separator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use rustc_middle::ty::TyCtxt;

pub struct Derefer;

pub struct DerefChecker<'tcx> {
pub struct DerefChecker<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
patcher: MirPatch<'tcx>,
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
local_decls: &'a IndexVec<Local, LocalDecl<'tcx>>,
}

impl<'tcx> MutVisitor<'tcx> for DerefChecker<'tcx> {
impl<'a, 'tcx> MutVisitor<'tcx> for DerefChecker<'a, 'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
Expand All @@ -36,7 +36,7 @@ impl<'tcx> MutVisitor<'tcx> for DerefChecker<'tcx> {

for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
if !p_ref.projection.is_empty() && p_elem == ProjectionElem::Deref {
let ty = p_ref.ty(&self.local_decls, self.tcx).ty;
let ty = p_ref.ty(self.local_decls, self.tcx).ty;
let temp = self.patcher.new_internal_with_info(
ty,
self.local_decls[p_ref.local].source_info.span,
Expand Down Expand Up @@ -70,7 +70,7 @@ impl<'tcx> MutVisitor<'tcx> for DerefChecker<'tcx> {

pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let patch = MirPatch::new(body);
let mut checker = DerefChecker { tcx, patcher: patch, local_decls: body.local_decls.clone() };
let mut checker = DerefChecker { tcx, patcher: patch, local_decls: &body.local_decls };

for (bb, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
checker.visit_basic_block_data(bb, data);
Expand Down

0 comments on commit 7e786e8

Please sign in to comment.