Skip to content

Commit 5f11e71

Browse files
committed
Reuse memory for process_cycles
1 parent 86e0303 commit 5f11e71

File tree

1 file changed

+8
-8
lines changed
  • compiler/rustc_data_structures/src/obligation_forest

1 file changed

+8
-8
lines changed

compiler/rustc_data_structures/src/obligation_forest/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ pub struct ObligationForest<O: ForestObligation> {
149149
/// comments in `process_obligation` for details.
150150
active_cache: FxHashMap<O::CacheKey, usize>,
151151

152-
/// A vector reused in compress(), to avoid allocating new vectors.
153-
node_rewrites: Vec<usize>,
152+
/// A vector reused in compress() and find_cycles_from_node(), to avoid allocating new vectors.
153+
reused_node_vec: Vec<usize>,
154154

155155
obligation_tree_id_generator: ObligationTreeIdGenerator,
156156

@@ -289,7 +289,7 @@ impl<O: ForestObligation> ObligationForest<O> {
289289
nodes: vec![],
290290
done_cache: Default::default(),
291291
active_cache: Default::default(),
292-
node_rewrites: vec![],
292+
reused_node_vec: vec![],
293293
obligation_tree_id_generator: (0..).map(ObligationTreeId),
294294
error_cache: Default::default(),
295295
}
@@ -544,12 +544,11 @@ impl<O: ForestObligation> ObligationForest<O> {
544544

545545
/// Report cycles between all `Success` nodes, and convert all `Success`
546546
/// nodes to `Done`. This must be called after `mark_successes`.
547-
fn process_cycles<P>(&self, processor: &mut P)
547+
fn process_cycles<P>(&mut self, processor: &mut P)
548548
where
549549
P: ObligationProcessor<Obligation = O>,
550550
{
551-
let mut stack = vec![];
552-
551+
let mut stack = std::mem::take(&mut self.reused_node_vec);
553552
for (index, node) in self.nodes.iter().enumerate() {
554553
// For some benchmarks this state test is extremely hot. It's a win
555554
// to handle the no-op cases immediately to avoid the cost of the
@@ -560,6 +559,7 @@ impl<O: ForestObligation> ObligationForest<O> {
560559
}
561560

562561
debug_assert!(stack.is_empty());
562+
self.reused_node_vec = stack;
563563
}
564564

565565
fn find_cycles_from_node<P>(&self, stack: &mut Vec<usize>, processor: &mut P, index: usize)
@@ -594,7 +594,7 @@ impl<O: ForestObligation> ObligationForest<O> {
594594
#[inline(never)]
595595
fn compress(&mut self, do_completed: DoCompleted) -> Option<Vec<O>> {
596596
let orig_nodes_len = self.nodes.len();
597-
let mut node_rewrites: Vec<_> = std::mem::take(&mut self.node_rewrites);
597+
let mut node_rewrites: Vec<_> = std::mem::take(&mut self.reused_node_vec);
598598
debug_assert!(node_rewrites.is_empty());
599599
node_rewrites.extend(0..orig_nodes_len);
600600
let mut dead_nodes = 0;
@@ -655,7 +655,7 @@ impl<O: ForestObligation> ObligationForest<O> {
655655
}
656656

657657
node_rewrites.truncate(0);
658-
self.node_rewrites = node_rewrites;
658+
self.reused_node_vec = node_rewrites;
659659

660660
if do_completed == DoCompleted::Yes { Some(removed_done_obligations) } else { None }
661661
}

0 commit comments

Comments
 (0)