optimization round 2
- moved work from `find_local` to `gather_statement` - created custom iterator for `iter_projections` - reverted change from `IndexVec` to `FxIndexMap`
This commit is contained in:
parent
d1c9696b7d
commit
b0dbd60040
3 changed files with 96 additions and 34 deletions
|
@ -39,18 +39,18 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
|
||||||
locals: body
|
locals: body
|
||||||
.local_decls
|
.local_decls
|
||||||
.iter_enumerated()
|
.iter_enumerated()
|
||||||
.filter(|(_, l)| !l.is_deref_temp())
|
.map(|(i, l)| {
|
||||||
.map(|(i, _)| {
|
if l.is_deref_temp() {
|
||||||
(
|
MovePathIndex::MAX
|
||||||
i,
|
} else {
|
||||||
Self::new_move_path(
|
Self::new_move_path(
|
||||||
&mut move_paths,
|
&mut move_paths,
|
||||||
&mut path_map,
|
&mut path_map,
|
||||||
&mut init_path_map,
|
&mut init_path_map,
|
||||||
None,
|
None,
|
||||||
Place::from(i),
|
Place::from(i),
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
projections: Default::default(),
|
projections: Default::default(),
|
||||||
|
@ -285,8 +285,14 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
|
||||||
fn gather_statement(&mut self, stmt: &Statement<'tcx>) {
|
fn gather_statement(&mut self, stmt: &Statement<'tcx>) {
|
||||||
match &stmt.kind {
|
match &stmt.kind {
|
||||||
StatementKind::Assign(box (place, Rvalue::CopyForDeref(reffed))) => {
|
StatementKind::Assign(box (place, Rvalue::CopyForDeref(reffed))) => {
|
||||||
assert!(self.builder.body.local_decls[place.local].is_deref_temp());
|
let local = place.as_local().unwrap();
|
||||||
self.builder.data.rev_lookup.un_derefer.insert(place.as_local().unwrap(), *reffed);
|
assert!(self.builder.body.local_decls[local].is_deref_temp());
|
||||||
|
|
||||||
|
let rev_lookup = &mut self.builder.data.rev_lookup;
|
||||||
|
|
||||||
|
rev_lookup.un_derefer.insert(local, reffed.as_ref());
|
||||||
|
let base_local = rev_lookup.un_derefer.deref_chain(local).first().unwrap().local;
|
||||||
|
rev_lookup.locals[local] = rev_lookup.locals[base_local];
|
||||||
}
|
}
|
||||||
StatementKind::Assign(box (place, rval)) => {
|
StatementKind::Assign(box (place, rval)) => {
|
||||||
self.create_move_path(*place);
|
self.create_move_path(*place);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::move_paths::builder::MoveDat;
|
use crate::move_paths::builder::MoveDat;
|
||||||
use crate::un_derefer::UnDerefer;
|
use crate::un_derefer::UnDerefer;
|
||||||
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
use rustc_index::{IndexSlice, IndexVec};
|
use rustc_index::{IndexSlice, IndexVec};
|
||||||
use rustc_middle::mir::*;
|
use rustc_middle::mir::*;
|
||||||
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
|
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
|
||||||
|
@ -291,7 +291,7 @@ impl Init {
|
||||||
/// Tables mapping from a place to its MovePathIndex.
|
/// Tables mapping from a place to its MovePathIndex.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct MovePathLookup<'tcx> {
|
pub struct MovePathLookup<'tcx> {
|
||||||
locals: FxIndexMap<Local, MovePathIndex>,
|
locals: IndexVec<Local, MovePathIndex>,
|
||||||
|
|
||||||
/// projections are made from a base-place and a projection
|
/// projections are made from a base-place and a projection
|
||||||
/// elem. The base-place will have a unique MovePathIndex; we use
|
/// elem. The base-place will have a unique MovePathIndex; we use
|
||||||
|
@ -331,17 +331,9 @@ impl<'tcx> MovePathLookup<'tcx> {
|
||||||
LookupResult::Exact(result)
|
LookupResult::Exact(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn find_local(&self, local: Local) -> MovePathIndex {
|
pub fn find_local(&self, local: Local) -> MovePathIndex {
|
||||||
let deref_chain = self.un_derefer.deref_chain(local);
|
self.locals[local]
|
||||||
|
|
||||||
let local = match deref_chain.first() {
|
|
||||||
Some(place) => place.local,
|
|
||||||
None => local,
|
|
||||||
};
|
|
||||||
|
|
||||||
*self.locals.get(&local).unwrap_or_else(|| {
|
|
||||||
bug!("base local ({local:?}) of deref_chain should not be a deref temp")
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An enumerated iterator of `local`s and their associated
|
/// An enumerated iterator of `local`s and their associated
|
||||||
|
@ -349,7 +341,7 @@ impl<'tcx> MovePathLookup<'tcx> {
|
||||||
pub fn iter_locals_enumerated(
|
pub fn iter_locals_enumerated(
|
||||||
&self,
|
&self,
|
||||||
) -> impl DoubleEndedIterator<Item = (Local, MovePathIndex)> + ExactSizeIterator + '_ {
|
) -> impl DoubleEndedIterator<Item = (Local, MovePathIndex)> + ExactSizeIterator + '_ {
|
||||||
self.locals.iter().map(|(&l, &idx)| (l, idx))
|
self.locals.iter_enumerated().map(|(l, &idx)| (l, idx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,36 +1,100 @@
|
||||||
use rustc_data_structures::fx::FxHashMap;
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
use rustc_middle::mir::*;
|
use rustc_middle::mir::*;
|
||||||
|
|
||||||
use std::iter::once;
|
|
||||||
|
|
||||||
/// Used for reverting changes made by `DerefSeparator`
|
/// Used for reverting changes made by `DerefSeparator`
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
pub struct UnDerefer<'tcx> {
|
pub struct UnDerefer<'tcx> {
|
||||||
deref_chains: FxHashMap<Local, Vec<Place<'tcx>>>,
|
deref_chains: FxHashMap<Local, Vec<PlaceRef<'tcx>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> UnDerefer<'tcx> {
|
impl<'tcx> UnDerefer<'tcx> {
|
||||||
pub fn insert(&mut self, local: Local, reffed: Place<'tcx>) {
|
#[inline]
|
||||||
|
pub fn insert(&mut self, local: Local, reffed: PlaceRef<'tcx>) {
|
||||||
let mut chain = self.deref_chains.remove(&reffed.local).unwrap_or_default();
|
let mut chain = self.deref_chains.remove(&reffed.local).unwrap_or_default();
|
||||||
chain.push(reffed);
|
chain.push(reffed);
|
||||||
self.deref_chains.insert(local, chain);
|
self.deref_chains.insert(local, chain);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the chain of places behind `DerefTemp` locals
|
/// Returns the chain of places behind `DerefTemp` locals
|
||||||
pub fn deref_chain(&self, local: Local) -> &[Place<'tcx>] {
|
#[inline]
|
||||||
|
pub fn deref_chain(&self, local: Local) -> &[PlaceRef<'tcx>] {
|
||||||
self.deref_chains.get(&local).map(Vec::as_slice).unwrap_or_default()
|
self.deref_chains.get(&local).map(Vec::as_slice).unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Iterates over the projections of a place and its deref chain.
|
||||||
|
///
|
||||||
|
/// See [`PlaceRef::iter_projections`]
|
||||||
|
#[inline]
|
||||||
pub fn iter_projections(
|
pub fn iter_projections(
|
||||||
&self,
|
&self,
|
||||||
place: PlaceRef<'tcx>,
|
place: PlaceRef<'tcx>,
|
||||||
) -> impl Iterator<Item = (PlaceRef<'tcx>, PlaceElem<'tcx>)> + DoubleEndedIterator + '_ {
|
) -> impl Iterator<Item = (PlaceRef<'tcx>, PlaceElem<'tcx>)> + '_ {
|
||||||
let deref_chain = self.deref_chain(place.local);
|
ProjectionIter::new(self.deref_chain(place.local), place)
|
||||||
|
}
|
||||||
deref_chain
|
}
|
||||||
.iter()
|
|
||||||
.map(Place::as_ref)
|
/// The iterator returned by [`UnDerefer::iter_projections`].
|
||||||
.chain(once(place))
|
struct ProjectionIter<'a, 'tcx> {
|
||||||
.flat_map(|place| place.iter_projections())
|
places: SlicePlusOne<'a, PlaceRef<'tcx>>,
|
||||||
|
proj_idx: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'tcx> ProjectionIter<'a, 'tcx> {
|
||||||
|
#[inline]
|
||||||
|
fn new(deref_chain: &'a [PlaceRef<'tcx>], place: PlaceRef<'tcx>) -> Self {
|
||||||
|
// just return an empty iterator for a bare local
|
||||||
|
let last = if place.as_local().is_none() {
|
||||||
|
Some(place)
|
||||||
|
} else {
|
||||||
|
debug_assert!(deref_chain.is_empty());
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
ProjectionIter { places: SlicePlusOne { slice: deref_chain, last }, proj_idx: 0 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'tcx> Iterator for ProjectionIter<'_, 'tcx> {
|
||||||
|
type Item = (PlaceRef<'tcx>, PlaceElem<'tcx>);
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn next(&mut self) -> Option<(PlaceRef<'tcx>, PlaceElem<'tcx>)> {
|
||||||
|
let place = self.places.read()?;
|
||||||
|
|
||||||
|
// the projection should never be empty except for a bare local which is handled in new
|
||||||
|
let partial_place =
|
||||||
|
PlaceRef { local: place.local, projection: &place.projection[..self.proj_idx] };
|
||||||
|
let elem = place.projection[self.proj_idx];
|
||||||
|
|
||||||
|
if self.proj_idx == place.projection.len() - 1 {
|
||||||
|
self.proj_idx = 0;
|
||||||
|
self.places.advance();
|
||||||
|
} else {
|
||||||
|
self.proj_idx += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some((partial_place, elem))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SlicePlusOne<'a, T> {
|
||||||
|
slice: &'a [T],
|
||||||
|
last: Option<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Copy> SlicePlusOne<'_, T> {
|
||||||
|
#[inline]
|
||||||
|
fn read(&self) -> Option<T> {
|
||||||
|
self.slice.first().copied().or(self.last)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn advance(&mut self) {
|
||||||
|
match self.slice {
|
||||||
|
[_, ref remainder @ ..] => {
|
||||||
|
self.slice = remainder;
|
||||||
|
}
|
||||||
|
[] => self.last = None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue