Skip to content

Commit 89e84c0

Browse files
committed
Use Rc less in MirBorrowckCtxt.
The `regioncx` and `borrow_set` fields can be references instead of `Rc`. They use the existing `'a` lifetime. This avoids some heap allocations and is a bit simpler.
1 parent 9ff5fc4 commit 89e84c0

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

compiler/rustc_borrowck/src/diagnostics/find_use.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::collections::VecDeque;
2-
use std::rc::Rc;
32

43
use rustc_data_structures::fx::FxIndexSet;
54
use rustc_middle::mir::visit::{MirVisitable, PlaceContext, Visitor};
@@ -11,7 +10,7 @@ use crate::region_infer::{Cause, RegionInferenceContext};
1110

1211
pub(crate) fn find<'tcx>(
1312
body: &Body<'tcx>,
14-
regioncx: &Rc<RegionInferenceContext<'tcx>>,
13+
regioncx: &RegionInferenceContext<'tcx>,
1514
tcx: TyCtxt<'tcx>,
1615
region_vid: RegionVid,
1716
start_point: Location,
@@ -23,7 +22,7 @@ pub(crate) fn find<'tcx>(
2322

2423
struct UseFinder<'a, 'tcx> {
2524
body: &'a Body<'tcx>,
26-
regioncx: &'a Rc<RegionInferenceContext<'tcx>>,
25+
regioncx: &'a RegionInferenceContext<'tcx>,
2726
tcx: TyCtxt<'tcx>,
2827
region_vid: RegionVid,
2928
start_point: Location,

compiler/rustc_borrowck/src/lib.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ fn do_mir_borrowck<'tcx>(
201201
.into_results_cursor(body);
202202

203203
let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(def).is_fn_or_closure();
204-
let borrow_set =
205-
Rc::new(BorrowSet::build(tcx, body, locals_are_invalidated_at_exit, &move_data));
204+
let borrow_set = BorrowSet::build(tcx, body, locals_are_invalidated_at_exit, &move_data);
206205

207206
// Compute non-lexical lifetimes.
208207
let nll::NllOutput {
@@ -246,8 +245,6 @@ fn do_mir_borrowck<'tcx>(
246245
// usage significantly on some benchmarks.
247246
drop(flow_inits);
248247

249-
let regioncx = Rc::new(regioncx);
250-
251248
let flow_borrows = Borrows::new(tcx, body, &regioncx, &borrow_set)
252249
.into_engine(tcx, body)
253250
.pass_name("borrowck")
@@ -289,10 +286,10 @@ fn do_mir_borrowck<'tcx>(
289286
access_place_error_reported: Default::default(),
290287
reservation_error_reported: Default::default(),
291288
uninitialized_error_reported: Default::default(),
292-
regioncx: regioncx.clone(),
289+
regioncx: &regioncx,
293290
used_mut: Default::default(),
294291
used_mut_upvars: SmallVec::new(),
295-
borrow_set: Rc::clone(&borrow_set),
292+
borrow_set: &borrow_set,
296293
upvars: &[],
297294
local_names: IndexVec::from_elem(None, &promoted_body.local_decls),
298295
region_names: RefCell::default(),
@@ -330,10 +327,10 @@ fn do_mir_borrowck<'tcx>(
330327
access_place_error_reported: Default::default(),
331328
reservation_error_reported: Default::default(),
332329
uninitialized_error_reported: Default::default(),
333-
regioncx: Rc::clone(&regioncx),
330+
regioncx: &regioncx,
334331
used_mut: Default::default(),
335332
used_mut_upvars: SmallVec::new(),
336-
borrow_set: Rc::clone(&borrow_set),
333+
borrow_set: &borrow_set,
337334
upvars: tcx.closure_captures(def),
338335
local_names,
339336
region_names: RefCell::default(),
@@ -423,8 +420,8 @@ fn do_mir_borrowck<'tcx>(
423420
Some(Box::new(BodyWithBorrowckFacts {
424421
body: body_owned,
425422
promoted,
426-
borrow_set,
427-
region_inference_context: regioncx,
423+
borrow_set: Rc::new(borrow_set),
424+
region_inference_context: Rc::new(regioncx),
428425
location_table: polonius_input.as_ref().map(|_| location_table),
429426
input_facts: polonius_input,
430427
output_facts,
@@ -570,10 +567,10 @@ struct MirBorrowckCtxt<'a, 'infcx, 'tcx> {
570567
used_mut_upvars: SmallVec<[FieldIdx; 8]>,
571568
/// Region inference context. This contains the results from region inference and lets us e.g.
572569
/// find out which CFG points are contained in each borrow region.
573-
regioncx: Rc<RegionInferenceContext<'tcx>>,
570+
regioncx: &'a RegionInferenceContext<'tcx>,
574571

575572
/// The set of borrows extracted from the MIR
576-
borrow_set: Rc<BorrowSet<'tcx>>,
573+
borrow_set: &'a BorrowSet<'tcx>,
577574

578575
/// Information about upvars not necessarily preserved in types or MIR
579576
upvars: &'tcx [&'tcx ty::CapturedPlace<'tcx>],
@@ -800,9 +797,8 @@ impl<'a, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'a, 'tcx, R>
800797
TerminatorKind::Yield { value: _, resume: _, resume_arg: _, drop: _ } => {
801798
if self.movable_coroutine {
802799
// Look for any active borrows to locals
803-
let borrow_set = self.borrow_set.clone();
804800
for i in state.borrows.iter() {
805-
let borrow = &borrow_set[i];
801+
let borrow = &self.borrow_set[i];
806802
self.check_for_local_borrow(borrow, span);
807803
}
808804
}
@@ -816,9 +812,8 @@ impl<'a, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'a, 'tcx, R>
816812
// Often, the storage will already have been killed by an explicit
817813
// StorageDead, but we don't always emit those (notably on unwind paths),
818814
// so this "extra check" serves as a kind of backup.
819-
let borrow_set = self.borrow_set.clone();
820815
for i in state.borrows.iter() {
821-
let borrow = &borrow_set[i];
816+
let borrow = &self.borrow_set[i];
822817
self.check_for_invalidation_at_exit(loc, borrow, span);
823818
}
824819
}
@@ -1037,13 +1032,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
10371032
state: &BorrowckDomain<'a, 'tcx>,
10381033
) -> bool {
10391034
let mut error_reported = false;
1040-
let borrow_set = Rc::clone(&self.borrow_set);
10411035

10421036
// Use polonius output if it has been enabled.
10431037
let mut polonius_output;
10441038
let borrows_in_scope = if let Some(polonius) = &self.polonius_output {
10451039
let location = self.location_table.start_index(location);
1046-
polonius_output = BitSet::new_empty(borrow_set.len());
1040+
polonius_output = BitSet::new_empty(self.borrow_set.len());
10471041
for &idx in polonius.errors_at(location) {
10481042
polonius_output.insert(idx);
10491043
}
@@ -1057,7 +1051,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
10571051
self.infcx.tcx,
10581052
self.body,
10591053
(sd, place_span.0),
1060-
&borrow_set,
1054+
self.borrow_set,
10611055
|borrow_index| borrows_in_scope.contains(borrow_index),
10621056
|this, borrow_index, borrow| match (rw, borrow.kind) {
10631057
// Obviously an activation is compatible with its own
@@ -1580,9 +1574,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
15801574
// Two-phase borrow support: For each activation that is newly
15811575
// generated at this statement, check if it interferes with
15821576
// another borrow.
1583-
let borrow_set = self.borrow_set.clone();
1584-
for &borrow_index in borrow_set.activations_at_location(location) {
1585-
let borrow = &borrow_set[borrow_index];
1577+
for &borrow_index in self.borrow_set.activations_at_location(location) {
1578+
let borrow = &self.borrow_set[borrow_index];
15861579

15871580
// only mutable borrows should be 2-phase
15881581
assert!(match borrow.kind {

0 commit comments

Comments
 (0)