Skip to content

Commit b0dbd60

Browse files
committed
optimization round 2
- moved work from `find_local` to `gather_statement` - created custom iterator for `iter_projections` - reverted change from `IndexVec` to `FxIndexMap`
1 parent d1c9696 commit b0dbd60

File tree

3 files changed

+96
-34
lines changed

3 files changed

+96
-34
lines changed

compiler/rustc_mir_dataflow/src/move_paths/builder.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
3939
locals: body
4040
.local_decls
4141
.iter_enumerated()
42-
.filter(|(_, l)| !l.is_deref_temp())
43-
.map(|(i, _)| {
44-
(
45-
i,
42+
.map(|(i, l)| {
43+
if l.is_deref_temp() {
44+
MovePathIndex::MAX
45+
} else {
4646
Self::new_move_path(
4747
&mut move_paths,
4848
&mut path_map,
4949
&mut init_path_map,
5050
None,
5151
Place::from(i),
52-
),
53-
)
52+
)
53+
}
5454
})
5555
.collect(),
5656
projections: Default::default(),
@@ -285,8 +285,14 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
285285
fn gather_statement(&mut self, stmt: &Statement<'tcx>) {
286286
match &stmt.kind {
287287
StatementKind::Assign(box (place, Rvalue::CopyForDeref(reffed))) => {
288-
assert!(self.builder.body.local_decls[place.local].is_deref_temp());
289-
self.builder.data.rev_lookup.un_derefer.insert(place.as_local().unwrap(), *reffed);
288+
let local = place.as_local().unwrap();
289+
assert!(self.builder.body.local_decls[local].is_deref_temp());
290+
291+
let rev_lookup = &mut self.builder.data.rev_lookup;
292+
293+
rev_lookup.un_derefer.insert(local, reffed.as_ref());
294+
let base_local = rev_lookup.un_derefer.deref_chain(local).first().unwrap().local;
295+
rev_lookup.locals[local] = rev_lookup.locals[base_local];
290296
}
291297
StatementKind::Assign(box (place, rval)) => {
292298
self.create_move_path(*place);

compiler/rustc_mir_dataflow/src/move_paths/mod.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::move_paths::builder::MoveDat;
22
use crate::un_derefer::UnDerefer;
3-
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
3+
use rustc_data_structures::fx::FxHashMap;
44
use rustc_index::{IndexSlice, IndexVec};
55
use rustc_middle::mir::*;
66
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
@@ -291,7 +291,7 @@ impl Init {
291291
/// Tables mapping from a place to its MovePathIndex.
292292
#[derive(Debug)]
293293
pub struct MovePathLookup<'tcx> {
294-
locals: FxIndexMap<Local, MovePathIndex>,
294+
locals: IndexVec<Local, MovePathIndex>,
295295

296296
/// projections are made from a base-place and a projection
297297
/// elem. The base-place will have a unique MovePathIndex; we use
@@ -331,25 +331,17 @@ impl<'tcx> MovePathLookup<'tcx> {
331331
LookupResult::Exact(result)
332332
}
333333

334+
#[inline]
334335
pub fn find_local(&self, local: Local) -> MovePathIndex {
335-
let deref_chain = self.un_derefer.deref_chain(local);
336-
337-
let local = match deref_chain.first() {
338-
Some(place) => place.local,
339-
None => local,
340-
};
341-
342-
*self.locals.get(&local).unwrap_or_else(|| {
343-
bug!("base local ({local:?}) of deref_chain should not be a deref temp")
344-
})
336+
self.locals[local]
345337
}
346338

347339
/// An enumerated iterator of `local`s and their associated
348340
/// `MovePathIndex`es.
349341
pub fn iter_locals_enumerated(
350342
&self,
351343
) -> impl DoubleEndedIterator<Item = (Local, MovePathIndex)> + ExactSizeIterator + '_ {
352-
self.locals.iter().map(|(&l, &idx)| (l, idx))
344+
self.locals.iter_enumerated().map(|(l, &idx)| (l, idx))
353345
}
354346
}
355347

+77-13
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,100 @@
11
use rustc_data_structures::fx::FxHashMap;
22
use rustc_middle::mir::*;
33

4-
use std::iter::once;
5-
64
/// Used for reverting changes made by `DerefSeparator`
75
#[derive(Default, Debug)]
86
pub struct UnDerefer<'tcx> {
9-
deref_chains: FxHashMap<Local, Vec<Place<'tcx>>>,
7+
deref_chains: FxHashMap<Local, Vec<PlaceRef<'tcx>>>,
108
}
119

1210
impl<'tcx> UnDerefer<'tcx> {
13-
pub fn insert(&mut self, local: Local, reffed: Place<'tcx>) {
11+
#[inline]
12+
pub fn insert(&mut self, local: Local, reffed: PlaceRef<'tcx>) {
1413
let mut chain = self.deref_chains.remove(&reffed.local).unwrap_or_default();
1514
chain.push(reffed);
1615
self.deref_chains.insert(local, chain);
1716
}
1817

1918
/// Returns the chain of places behind `DerefTemp` locals
20-
pub fn deref_chain(&self, local: Local) -> &[Place<'tcx>] {
19+
#[inline]
20+
pub fn deref_chain(&self, local: Local) -> &[PlaceRef<'tcx>] {
2121
self.deref_chains.get(&local).map(Vec::as_slice).unwrap_or_default()
2222
}
2323

24+
/// Iterates over the projections of a place and its deref chain.
25+
///
26+
/// See [`PlaceRef::iter_projections`]
27+
#[inline]
2428
pub fn iter_projections(
2529
&self,
2630
place: PlaceRef<'tcx>,
27-
) -> impl Iterator<Item = (PlaceRef<'tcx>, PlaceElem<'tcx>)> + DoubleEndedIterator + '_ {
28-
let deref_chain = self.deref_chain(place.local);
29-
30-
deref_chain
31-
.iter()
32-
.map(Place::as_ref)
33-
.chain(once(place))
34-
.flat_map(|place| place.iter_projections())
31+
) -> impl Iterator<Item = (PlaceRef<'tcx>, PlaceElem<'tcx>)> + '_ {
32+
ProjectionIter::new(self.deref_chain(place.local), place)
33+
}
34+
}
35+
36+
/// The iterator returned by [`UnDerefer::iter_projections`].
37+
struct ProjectionIter<'a, 'tcx> {
38+
places: SlicePlusOne<'a, PlaceRef<'tcx>>,
39+
proj_idx: usize,
40+
}
41+
42+
impl<'a, 'tcx> ProjectionIter<'a, 'tcx> {
43+
#[inline]
44+
fn new(deref_chain: &'a [PlaceRef<'tcx>], place: PlaceRef<'tcx>) -> Self {
45+
// just return an empty iterator for a bare local
46+
let last = if place.as_local().is_none() {
47+
Some(place)
48+
} else {
49+
debug_assert!(deref_chain.is_empty());
50+
None
51+
};
52+
53+
ProjectionIter { places: SlicePlusOne { slice: deref_chain, last }, proj_idx: 0 }
54+
}
55+
}
56+
57+
impl<'tcx> Iterator for ProjectionIter<'_, 'tcx> {
58+
type Item = (PlaceRef<'tcx>, PlaceElem<'tcx>);
59+
60+
#[inline]
61+
fn next(&mut self) -> Option<(PlaceRef<'tcx>, PlaceElem<'tcx>)> {
62+
let place = self.places.read()?;
63+
64+
// the projection should never be empty except for a bare local which is handled in new
65+
let partial_place =
66+
PlaceRef { local: place.local, projection: &place.projection[..self.proj_idx] };
67+
let elem = place.projection[self.proj_idx];
68+
69+
if self.proj_idx == place.projection.len() - 1 {
70+
self.proj_idx = 0;
71+
self.places.advance();
72+
} else {
73+
self.proj_idx += 1;
74+
}
75+
76+
Some((partial_place, elem))
77+
}
78+
}
79+
80+
struct SlicePlusOne<'a, T> {
81+
slice: &'a [T],
82+
last: Option<T>,
83+
}
84+
85+
impl<T: Copy> SlicePlusOne<'_, T> {
86+
#[inline]
87+
fn read(&self) -> Option<T> {
88+
self.slice.first().copied().or(self.last)
89+
}
90+
91+
#[inline]
92+
fn advance(&mut self) {
93+
match self.slice {
94+
[_, ref remainder @ ..] => {
95+
self.slice = remainder;
96+
}
97+
[] => self.last = None,
98+
}
3599
}
36100
}

0 commit comments

Comments
 (0)