Skip to content

Commit afc5e1f

Browse files
committed
Split the Edges iterator.
The `Edges` iterator returns `OutlivesConstraint` elements, which are 72 bytes. This is big enough to affect performance. Return `&OutlivesConstraint` would be better. However, each `Edges` iterator is really one of two different iterators. The "from graph" case does a graph traversal and could return `&OutlivesConstraint`. But the "from static" case just does a `0..n` iteration and constructs a new `OutlivesConstraint` from that, so it can't return a reference. This commit splits `Edges into `EdgesFromGraph` and `EdgesFromStatic`, which allows them to have different return types. This is a perf win for the `wg-grammar` benchmark.
1 parent 60bf07a commit afc5e1f

File tree

2 files changed

+85
-61
lines changed

2 files changed

+85
-61
lines changed

Diff for: compiler/rustc_borrowck/src/constraints/graph.rs

+57-49
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
use rustc_data_structures::graph;
22
use rustc_index::IndexVec;
3-
use rustc_middle::mir::ConstraintCategory;
4-
use rustc_middle::ty::{RegionVid, VarianceDiagInfo};
5-
use rustc_span::DUMMY_SP;
3+
use rustc_middle::ty::RegionVid;
64

75
use crate::constraints::{OutlivesConstraint, OutlivesConstraintIndex, OutlivesConstraintSet};
8-
use crate::type_check::Locations;
96

107
/// The construct graph organizes the constraints by their end-points.
118
/// It can be used to view a `R1: R2` constraint as either an edge `R1
@@ -105,63 +102,57 @@ impl<D: ConstraintGraphDirection> ConstraintGraph<D> {
105102
RegionGraph::new(set, self, static_region)
106103
}
107104

105+
pub(crate) fn is_normal(&self) -> bool {
106+
D::is_normal()
107+
}
108+
108109
/// Given a region `R`, iterate over all constraints `R: R1`.
109-
pub(crate) fn outgoing_edges<'a, 'tcx>(
110+
pub(crate) fn outgoing_edges_from_graph<'a, 'tcx>(
110111
&'a self,
111112
region_sup: RegionVid,
112113
constraints: &'a OutlivesConstraintSet<'tcx>,
113-
static_region: RegionVid,
114-
) -> Edges<'a, 'tcx, D> {
115-
//if this is the `'static` region and the graph's direction is normal,
116-
//then setup the Edges iterator to return all regions #53178
117-
if region_sup == static_region && D::is_normal() {
118-
Edges {
119-
graph: self,
120-
constraints,
121-
pointer: None,
122-
next_static_idx: Some(0),
123-
static_region,
124-
}
125-
} else {
126-
//otherwise, just setup the iterator as normal
127-
let first = self.first_constraints[region_sup];
128-
Edges { graph: self, constraints, pointer: first, next_static_idx: None, static_region }
129-
}
114+
) -> EdgesFromGraph<'a, 'tcx, D> {
115+
EdgesFromGraph { graph: self, constraints, pointer: self.first_constraints[region_sup] }
116+
}
117+
118+
/// Returns all regions (#53178).
119+
pub(crate) fn outgoing_edges_from_static(&self) -> EdgesFromStatic {
120+
EdgesFromStatic { next_static_idx: 0, end_static_idx: self.first_constraints.len() }
130121
}
131122
}
132123

133-
pub(crate) struct Edges<'a, 'tcx, D: ConstraintGraphDirection> {
124+
pub(crate) struct EdgesFromGraph<'a, 'tcx, D: ConstraintGraphDirection> {
134125
graph: &'a ConstraintGraph<D>,
135126
constraints: &'a OutlivesConstraintSet<'tcx>,
136127
pointer: Option<OutlivesConstraintIndex>,
137-
next_static_idx: Option<usize>,
138-
static_region: RegionVid,
139128
}
140129

141-
impl<'a, 'tcx, D: ConstraintGraphDirection> Iterator for Edges<'a, 'tcx, D> {
142-
type Item = OutlivesConstraint<'tcx>;
130+
impl<'a, 'tcx, D: ConstraintGraphDirection> Iterator for EdgesFromGraph<'a, 'tcx, D> {
131+
type Item = &'a OutlivesConstraint<'tcx>;
143132

144133
fn next(&mut self) -> Option<Self::Item> {
145134
if let Some(p) = self.pointer {
146135
self.pointer = self.graph.next_constraints[p];
136+
Some(&self.constraints[p])
137+
} else {
138+
None
139+
}
140+
}
141+
}
142+
143+
pub(crate) struct EdgesFromStatic {
144+
next_static_idx: usize,
145+
end_static_idx: usize,
146+
}
147+
148+
impl Iterator for EdgesFromStatic {
149+
type Item = RegionVid;
147150

148-
Some(self.constraints[p])
149-
} else if let Some(next_static_idx) = self.next_static_idx {
150-
self.next_static_idx = if next_static_idx == (self.graph.first_constraints.len() - 1) {
151-
None
152-
} else {
153-
Some(next_static_idx + 1)
154-
};
155-
156-
Some(OutlivesConstraint {
157-
sup: self.static_region,
158-
sub: next_static_idx.into(),
159-
locations: Locations::All(DUMMY_SP),
160-
span: DUMMY_SP,
161-
category: ConstraintCategory::Internal,
162-
variance_info: VarianceDiagInfo::default(),
163-
from_closure: false,
164-
})
151+
fn next(&mut self) -> Option<Self::Item> {
152+
if self.next_static_idx < self.end_static_idx {
153+
let ret = RegionVid::from_usize(self.next_static_idx);
154+
self.next_static_idx += 1;
155+
Some(ret)
165156
} else {
166157
None
167158
}
@@ -193,21 +184,38 @@ impl<'a, 'tcx, D: ConstraintGraphDirection> RegionGraph<'a, 'tcx, D> {
193184
/// Given a region `R`, iterate over all regions `R1` such that
194185
/// there exists a constraint `R: R1`.
195186
pub(crate) fn outgoing_regions(&self, region_sup: RegionVid) -> Successors<'a, 'tcx, D> {
196-
Successors {
197-
edges: self.constraint_graph.outgoing_edges(region_sup, self.set, self.static_region),
187+
// If this is the `'static` region and the graph's direction is normal,
188+
// then setup the Edges iterator to return all regions (#53178).
189+
if region_sup == self.static_region && D::is_normal() {
190+
Successors::FromStatic(self.constraint_graph.outgoing_edges_from_static())
191+
} else {
192+
// Otherwise, just setup the iterator as normal.
193+
Successors::FromGraph(
194+
self.constraint_graph.outgoing_edges_from_graph(region_sup, self.set),
195+
)
198196
}
199197
}
200198
}
201199

202-
pub(crate) struct Successors<'a, 'tcx, D: ConstraintGraphDirection> {
203-
edges: Edges<'a, 'tcx, D>,
200+
pub(crate) enum Successors<'a, 'tcx, D: ConstraintGraphDirection> {
201+
FromStatic(EdgesFromStatic),
202+
FromGraph(EdgesFromGraph<'a, 'tcx, D>),
204203
}
205204

206205
impl<'a, 'tcx, D: ConstraintGraphDirection> Iterator for Successors<'a, 'tcx, D> {
207206
type Item = RegionVid;
208207

209208
fn next(&mut self) -> Option<Self::Item> {
210-
self.edges.next().map(|c| D::end_region(c.sup, c.sub))
209+
match self {
210+
Successors::FromStatic(edges) => {
211+
// No `D::end_region` call needed here: static successors are only possible when
212+
// the direction is `Normal`, so we can directly use what would be the `sub` value.
213+
edges.next()
214+
}
215+
Successors::FromGraph(edges) => {
216+
edges.next().map(|constraint| D::end_region(constraint.sup, constraint.sub))
217+
}
218+
}
211219
}
212220
}
213221

Diff for: compiler/rustc_borrowck/src/region_infer/mod.rs

+28-12
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
2121
use rustc_middle::ty::fold::fold_regions;
2222
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt, TypeFoldable, UniverseIndex};
2323
use rustc_mir_dataflow::points::DenseLocationMap;
24-
use rustc_span::Span;
2524
use rustc_span::hygiene::DesugaringKind;
25+
use rustc_span::{DUMMY_SP, Span};
2626
use tracing::{debug, instrument, trace};
2727

2828
use crate::BorrowckInferCtxt;
@@ -1809,27 +1809,43 @@ impl<'tcx> RegionInferenceContext<'tcx> {
18091809
// A constraint like `'r: 'x` can come from our constraint
18101810
// graph.
18111811
let fr_static = self.universal_regions().fr_static;
1812-
let outgoing_edges_from_graph =
1813-
self.constraint_graph.outgoing_edges(r, &self.constraints, fr_static);
18141812

18151813
// Always inline this closure because it can be hot.
18161814
let mut handle_constraint = #[inline(always)]
1817-
|constraint: OutlivesConstraint<'tcx>| {
1815+
|constraint: &OutlivesConstraint<'tcx>| {
18181816
debug_assert_eq!(constraint.sup, r);
18191817
let sub_region = constraint.sub;
18201818
if let Trace::NotVisited = context[sub_region] {
1821-
context[sub_region] = Trace::FromOutlivesConstraint(constraint);
1819+
context[sub_region] = Trace::FromOutlivesConstraint(*constraint);
18221820
deque.push_back(sub_region);
18231821
}
18241822
};
18251823

1826-
// This loop can be hot.
1827-
for constraint in outgoing_edges_from_graph {
1828-
if matches!(constraint.category, ConstraintCategory::IllegalUniverse) {
1829-
debug!("Ignoring illegal universe constraint: {constraint:?}");
1830-
continue;
1824+
// If this is the `'static` region and the graph's direction is normal, then set up the
1825+
// Edges iterator to return all regions (#53178).
1826+
if r == fr_static && self.constraint_graph.is_normal() {
1827+
for next_static_idx in self.constraint_graph.outgoing_edges_from_static() {
1828+
let constraint = OutlivesConstraint {
1829+
sup: fr_static,
1830+
sub: next_static_idx,
1831+
locations: Locations::All(DUMMY_SP),
1832+
span: DUMMY_SP,
1833+
category: ConstraintCategory::Internal,
1834+
variance_info: ty::VarianceDiagInfo::default(),
1835+
from_closure: false,
1836+
};
1837+
handle_constraint(&constraint);
1838+
}
1839+
} else {
1840+
let edges = self.constraint_graph.outgoing_edges_from_graph(r, &self.constraints);
1841+
// This loop can be hot.
1842+
for constraint in edges {
1843+
if matches!(constraint.category, ConstraintCategory::IllegalUniverse) {
1844+
debug!("Ignoring illegal universe constraint: {constraint:?}");
1845+
continue;
1846+
}
1847+
handle_constraint(constraint);
18311848
}
1832-
handle_constraint(constraint);
18331849
}
18341850

18351851
// Member constraints can also give rise to `'r: 'x` edges that
@@ -1846,7 +1862,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
18461862
variance_info: ty::VarianceDiagInfo::default(),
18471863
from_closure: false,
18481864
};
1849-
handle_constraint(constraint);
1865+
handle_constraint(&constraint);
18501866
}
18511867
}
18521868

0 commit comments

Comments
 (0)