Skip to content

Commit 7ad0c55

Browse files
authored
Unrolled build for #141823
Rollup merge of #141823 - amandasystems:reverse_scc_graph_once_cell, r=jieyouxu Drive-by refactor: use `OnceCell` for the reverse region SCC graph During region inference, the reverse SCC region graph is sometimes computed lazily. This changes the implementation for that from using an `Option` to a `OnceCell` which clearly communicates the intention and simplifies the code somewhat. There shouldn't be any performance impact, except that this pulls the computation of the reverse SCC graph slightly later than before, and so may avoid computing it in some instances. Note that this changes a mutable reference into an immutable (interior mutable) one.
2 parents a88fc0e + 95115b9 commit 7ad0c55

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cell::OnceCell;
12
use std::collections::VecDeque;
23
use std::rc::Rc;
34

@@ -197,8 +198,8 @@ pub struct RegionInferenceContext<'tcx> {
197198

198199
/// Reverse of the SCC constraint graph -- i.e., an edge `A -> B` exists if
199200
/// `B: A`. This is used to compute the universal regions that are required
200-
/// to outlive a given SCC. Computed lazily.
201-
rev_scc_graph: Option<ReverseSccGraph>,
201+
/// to outlive a given SCC.
202+
rev_scc_graph: OnceCell<ReverseSccGraph>,
202203

203204
/// The "R0 member of [R1..Rn]" constraints, indexed by SCC.
204205
member_constraints: Rc<MemberConstraintSet<'tcx, ConstraintSccIndex>>,
@@ -502,7 +503,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
502503
constraint_graph,
503504
constraint_sccs,
504505
scc_annotations,
505-
rev_scc_graph: None,
506+
rev_scc_graph: OnceCell::new(),
506507
member_constraints,
507508
member_constraints_applied: Vec::new(),
508509
universe_causes,
@@ -809,9 +810,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
809810
member_constraint_index: NllMemberConstraintIndex,
810811
choice_regions: &[ty::RegionVid],
811812
) {
812-
// Lazily compute the reverse graph, we'll need it later.
813-
self.compute_reverse_scc_graph();
814-
815813
// Create a mutable vector of the options. We'll try to winnow
816814
// them down.
817815
let mut choice_regions: Vec<ty::RegionVid> = choice_regions.to_vec();
@@ -849,7 +847,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
849847
// R0`). Therefore, we need only keep an option `O` if `UB: O`
850848
// for all UB.
851849
let universal_region_relations = &self.universal_region_relations;
852-
for ub in self.rev_scc_graph.as_ref().unwrap().upper_bounds(scc) {
850+
for ub in self.reverse_scc_graph().upper_bounds(scc) {
853851
debug!(?ub);
854852
choice_regions.retain(|&o_r| universal_region_relations.outlives(ub, o_r));
855853
}

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
215215
// FIXME: We could probably compute the LUB if there is one.
216216
let scc = self.constraint_sccs.scc(vid);
217217
let upper_bounds: Vec<_> = self
218-
.rev_scc_graph
219-
.as_ref()
220-
.unwrap()
218+
.reverse_scc_graph()
221219
.upper_bounds(scc)
222220
.filter_map(|vid| self.definitions[vid].external_name)
223221
.filter(|r| !r.is_static())

compiler/rustc_borrowck/src/region_infer/reverse_sccs.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,10 @@ impl ReverseSccGraph {
5959
}
6060

6161
impl RegionInferenceContext<'_> {
62-
/// Compute the reverse SCC-based constraint graph (lazily).
63-
pub(super) fn compute_reverse_scc_graph(&mut self) {
64-
if self.rev_scc_graph.is_some() {
65-
return;
66-
}
67-
68-
self.rev_scc_graph =
69-
Some(ReverseSccGraph::compute(&self.constraint_sccs, self.universal_regions()));
62+
/// Return the reverse graph of the region SCCs, initialising it if needed.
63+
pub(super) fn reverse_scc_graph(&self) -> &ReverseSccGraph {
64+
self.rev_scc_graph.get_or_init(|| {
65+
ReverseSccGraph::compute(&self.constraint_sccs, self.universal_regions())
66+
})
7067
}
7168
}

0 commit comments

Comments
 (0)