1
Fork 0

use Vec for region constraints

This commit is contained in:
Ali MJ Al-Nasrawy 2023-12-11 13:03:35 +00:00
parent 604f185fae
commit 9f7d0e91b5
9 changed files with 50 additions and 33 deletions

View file

@ -137,6 +137,10 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
) -> LexicalRegionResolutions<'tcx> {
let mut var_data = self.construct_var_data();
// Deduplicating constraints is shown to have a positive perf impact.
self.data.constraints.sort_by_key(|(constraint, _)| *constraint);
self.data.constraints.dedup_by_key(|(constraint, _)| *constraint);
if cfg!(debug_assertions) {
self.dump_constraints();
}
@ -183,7 +187,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
let mut constraints = IndexVec::from_elem(Vec::new(), &var_values.values);
// Tracks the changed region vids.
let mut changes = Vec::new();
for constraint in self.data.constraints.keys() {
for (constraint, _) in &self.data.constraints {
match *constraint {
Constraint::RegSubVar(a_region, b_vid) => {
let b_data = var_values.value_mut(b_vid);
@ -678,7 +682,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
let dummy_source = graph.add_node(());
let dummy_sink = graph.add_node(());
for constraint in self.data.constraints.keys() {
for (constraint, _) in &self.data.constraints {
match *constraint {
Constraint::VarSubVar(a_id, b_id) => {
graph.add_edge(NodeIndex(a_id.index()), NodeIndex(b_id.index()), *constraint);
@ -885,9 +889,11 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
}
Constraint::RegSubVar(region, _) | Constraint::VarSubReg(_, region) => {
let constraint_idx =
this.constraints.binary_search_by(|(c, _)| c.cmp(&edge.data)).unwrap();
state.result.push(RegionAndOrigin {
region,
origin: this.constraints.get(&edge.data).unwrap().clone(),
origin: this.constraints[constraint_idx].1.clone(),
});
}

View file

@ -416,8 +416,8 @@ impl<'tcx> MiniGraph<'tcx> {
region_constraints.undo_log.region_constraints_in_snapshot(&snapshot.undo_snapshot)
{
match undo_entry {
AddConstraint(constraint) => {
each_constraint(constraint);
&AddConstraint(i) => {
each_constraint(&region_constraints.data().constraints[i].0);
}
&AddVerify(i) => span_bug!(
region_constraints.data().verifys[i].origin.span(),
@ -430,8 +430,8 @@ impl<'tcx> MiniGraph<'tcx> {
region_constraints
.data()
.constraints
.keys()
.for_each(|constraint| each_constraint(constraint));
.iter()
.for_each(|(constraint, _)| each_constraint(constraint));
}
}

View file

@ -20,7 +20,6 @@ use rustc_middle::ty::{ReBound, ReVar};
use rustc_middle::ty::{Region, RegionVid};
use rustc_span::Span;
use std::collections::BTreeMap;
use std::ops::Range;
use std::{cmp, fmt, mem};
@ -90,7 +89,7 @@ pub type VarInfos = IndexVec<RegionVid, RegionVariableInfo>;
pub struct RegionConstraintData<'tcx> {
/// Constraints of the form `A <= B`, where either `A` or `B` can
/// be a region variable (or neither, as it happens).
pub constraints: BTreeMap<Constraint<'tcx>, SubregionOrigin<'tcx>>,
pub constraints: Vec<(Constraint<'tcx>, SubregionOrigin<'tcx>)>,
/// Constraints of the form `R0 member of [R1, ..., Rn]`, meaning that
/// `R0` must be equal to one of the regions `R1..Rn`. These occur
@ -273,7 +272,7 @@ pub(crate) enum UndoLog<'tcx> {
AddVar(RegionVid),
/// We added the given `constraint`.
AddConstraint(Constraint<'tcx>),
AddConstraint(usize),
/// We added the given `verify`.
AddVerify(usize),
@ -319,8 +318,9 @@ impl<'tcx> RegionConstraintStorage<'tcx> {
self.var_infos.pop().unwrap();
assert_eq!(self.var_infos.len(), vid.index());
}
AddConstraint(ref constraint) => {
self.data.constraints.remove(constraint);
AddConstraint(index) => {
self.data.constraints.pop().unwrap();
assert_eq!(self.data.constraints.len(), index);
}
AddVerify(index) => {
self.data.verifys.pop();
@ -443,14 +443,9 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
// cannot add constraints once regions are resolved
debug!("RegionConstraintCollector: add_constraint({:?})", constraint);
// never overwrite an existing (constraint, origin) - only insert one if it isn't
// present in the map yet. This prevents origins from outside the snapshot being
// replaced with "less informative" origins e.g., during calls to `can_eq`
let undo_log = &mut self.undo_log;
self.storage.data.constraints.entry(constraint).or_insert_with(|| {
undo_log.push(AddConstraint(constraint));
origin
});
let index = self.storage.data.constraints.len();
self.storage.data.constraints.push((constraint, origin));
self.undo_log.push(AddConstraint(index));
}
fn add_verify(&mut self, verify: Verify<'tcx>) {