Skip to content

Commit 385970f

Browse files
committed
Auto merge of rust-lang#137655 - nnethercote:split-edges-iterator, r=nnethercote
Split the `Edges` iterator. Some nice performance wins here, mostly on the `wg-grammar` benchmark. r? `@lcnr`
2 parents ed897d5 + 9a0513a commit 385970f

File tree

2 files changed

+132
-93
lines changed

2 files changed

+132
-93
lines changed

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

+68-60
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
@@ -23,8 +20,8 @@ pub(crate) type ReverseConstraintGraph = ConstraintGraph<Reverse>;
2320
/// Marker trait that controls whether a `R1: R2` constraint
2421
/// represents an edge `R1 -> R2` or `R2 -> R1`.
2522
pub(crate) trait ConstraintGraphDirection: Copy + 'static {
26-
fn start_region(c: &OutlivesConstraint<'_>) -> RegionVid;
27-
fn end_region(c: &OutlivesConstraint<'_>) -> RegionVid;
23+
fn start_region(sup: RegionVid, sub: RegionVid) -> RegionVid;
24+
fn end_region(sup: RegionVid, sub: RegionVid) -> RegionVid;
2825
fn is_normal() -> bool;
2926
}
3027

@@ -36,12 +33,12 @@ pub(crate) trait ConstraintGraphDirection: Copy + 'static {
3633
pub(crate) struct Normal;
3734

3835
impl ConstraintGraphDirection for Normal {
39-
fn start_region(c: &OutlivesConstraint<'_>) -> RegionVid {
40-
c.sup
36+
fn start_region(sup: RegionVid, _sub: RegionVid) -> RegionVid {
37+
sup
4138
}
4239

43-
fn end_region(c: &OutlivesConstraint<'_>) -> RegionVid {
44-
c.sub
40+
fn end_region(_sup: RegionVid, sub: RegionVid) -> RegionVid {
41+
sub
4542
}
4643

4744
fn is_normal() -> bool {
@@ -57,12 +54,12 @@ impl ConstraintGraphDirection for Normal {
5754
pub(crate) struct Reverse;
5855

5956
impl ConstraintGraphDirection for Reverse {
60-
fn start_region(c: &OutlivesConstraint<'_>) -> RegionVid {
61-
c.sub
57+
fn start_region(_sup: RegionVid, sub: RegionVid) -> RegionVid {
58+
sub
6259
}
6360

64-
fn end_region(c: &OutlivesConstraint<'_>) -> RegionVid {
65-
c.sup
61+
fn end_region(sup: RegionVid, _sub: RegionVid) -> RegionVid {
62+
sup
6663
}
6764

6865
fn is_normal() -> bool {
@@ -84,7 +81,7 @@ impl<D: ConstraintGraphDirection> ConstraintGraph<D> {
8481
let mut next_constraints = IndexVec::from_elem(None, &set.outlives);
8582

8683
for (idx, constraint) in set.outlives.iter_enumerated().rev() {
87-
let head = &mut first_constraints[D::start_region(constraint)];
84+
let head = &mut first_constraints[D::start_region(constraint.sup, constraint.sub)];
8885
let next = &mut next_constraints[idx];
8986
debug_assert!(next.is_none());
9087
*next = *head;
@@ -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))
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

+64-33
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;
@@ -311,9 +311,11 @@ enum RegionRelationCheckResult {
311311
}
312312

313313
#[derive(Clone, PartialEq, Eq, Debug)]
314-
enum Trace<'tcx> {
314+
enum Trace<'a, 'tcx> {
315315
StartRegion,
316-
FromOutlivesConstraint(OutlivesConstraint<'tcx>),
316+
FromGraph(&'a OutlivesConstraint<'tcx>),
317+
FromStatic(RegionVid),
318+
FromMember(RegionVid, RegionVid, Span),
317319
NotVisited,
318320
}
319321

@@ -1764,6 +1766,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17641766
let mut context = IndexVec::from_elem(Trace::NotVisited, &self.definitions);
17651767
context[from_region] = Trace::StartRegion;
17661768

1769+
let fr_static = self.universal_regions().fr_static;
1770+
17671771
// Use a deque so that we do a breadth-first search. We will
17681772
// stop at the first match, which ought to be the shortest
17691773
// path (fewest constraints).
@@ -1783,13 +1787,39 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17831787
if target_test(r) {
17841788
let mut result = vec![];
17851789
let mut p = r;
1790+
// This loop is cold and runs at the end, which is why we delay
1791+
// `OutlivesConstraint` construction until now.
17861792
loop {
1787-
match context[p].clone() {
1788-
Trace::NotVisited => {
1789-
bug!("found unvisited region {:?} on path to {:?}", p, r)
1793+
match context[p] {
1794+
Trace::FromGraph(c) => {
1795+
p = c.sup;
1796+
result.push(*c);
17901797
}
17911798

1792-
Trace::FromOutlivesConstraint(c) => {
1799+
Trace::FromStatic(sub) => {
1800+
let c = OutlivesConstraint {
1801+
sup: fr_static,
1802+
sub,
1803+
locations: Locations::All(DUMMY_SP),
1804+
span: DUMMY_SP,
1805+
category: ConstraintCategory::Internal,
1806+
variance_info: ty::VarianceDiagInfo::default(),
1807+
from_closure: false,
1808+
};
1809+
p = c.sup;
1810+
result.push(c);
1811+
}
1812+
1813+
Trace::FromMember(sup, sub, span) => {
1814+
let c = OutlivesConstraint {
1815+
sup,
1816+
sub,
1817+
locations: Locations::All(span),
1818+
span,
1819+
category: ConstraintCategory::OpaqueType,
1820+
variance_info: ty::VarianceDiagInfo::default(),
1821+
from_closure: false,
1822+
};
17931823
p = c.sup;
17941824
result.push(c);
17951825
}
@@ -1798,6 +1828,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17981828
result.reverse();
17991829
return Some((result, r));
18001830
}
1831+
1832+
Trace::NotVisited => {
1833+
bug!("found unvisited region {:?} on path to {:?}", p, r)
1834+
}
18011835
}
18021836
}
18031837
}
@@ -1808,45 +1842,42 @@ impl<'tcx> RegionInferenceContext<'tcx> {
18081842

18091843
// A constraint like `'r: 'x` can come from our constraint
18101844
// graph.
1811-
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);
18141845

18151846
// Always inline this closure because it can be hot.
1816-
let mut handle_constraint = #[inline(always)]
1817-
|constraint: OutlivesConstraint<'tcx>| {
1818-
debug_assert_eq!(constraint.sup, r);
1819-
let sub_region = constraint.sub;
1820-
if let Trace::NotVisited = context[sub_region] {
1821-
context[sub_region] = Trace::FromOutlivesConstraint(constraint);
1822-
deque.push_back(sub_region);
1847+
let mut handle_trace = #[inline(always)]
1848+
|sub, trace| {
1849+
if let Trace::NotVisited = context[sub] {
1850+
context[sub] = trace;
1851+
deque.push_back(sub);
18231852
}
18241853
};
18251854

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;
1855+
// If this is the `'static` region and the graph's direction is normal, then set up the
1856+
// Edges iterator to return all regions (#53178).
1857+
if r == fr_static && self.constraint_graph.is_normal() {
1858+
for sub in self.constraint_graph.outgoing_edges_from_static() {
1859+
handle_trace(sub, Trace::FromStatic(sub));
1860+
}
1861+
} else {
1862+
let edges = self.constraint_graph.outgoing_edges_from_graph(r, &self.constraints);
1863+
// This loop can be hot.
1864+
for constraint in edges {
1865+
if matches!(constraint.category, ConstraintCategory::IllegalUniverse) {
1866+
debug!("Ignoring illegal universe constraint: {constraint:?}");
1867+
continue;
1868+
}
1869+
debug_assert_eq!(constraint.sup, r);
1870+
handle_trace(constraint.sub, Trace::FromGraph(constraint));
18311871
}
1832-
handle_constraint(constraint);
18331872
}
18341873

18351874
// Member constraints can also give rise to `'r: 'x` edges that
18361875
// were not part of the graph initially, so watch out for those.
18371876
// (But they are extremely rare; this loop is very cold.)
18381877
for constraint in self.applied_member_constraints(self.constraint_sccs.scc(r)) {
1878+
let sub = constraint.min_choice;
18391879
let p_c = &self.member_constraints[constraint.member_constraint_index];
1840-
let constraint = OutlivesConstraint {
1841-
sup: r,
1842-
sub: constraint.min_choice,
1843-
locations: Locations::All(p_c.definition_span),
1844-
span: p_c.definition_span,
1845-
category: ConstraintCategory::OpaqueType,
1846-
variance_info: ty::VarianceDiagInfo::default(),
1847-
from_closure: false,
1848-
};
1849-
handle_constraint(constraint);
1880+
handle_trace(sub, Trace::FromMember(r, sub, p_c.definition_span));
18501881
}
18511882
}
18521883

0 commit comments

Comments
 (0)