Skip to content

Commit 4b20a27

Browse files
committed
coverage: Replace FrozenUnionFind with a plain IndexVec
This dedicated type seemed like a good idea at the time, but if we want to store this information in a query result then a plainer data type is more convenient.
1 parent 52c1bfa commit 4b20a27

File tree

2 files changed

+11
-31
lines changed

2 files changed

+11
-31
lines changed

compiler/rustc_mir_transform/src/coverage/counters/node_flow.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_index::{Idx, IndexVec};
1212
use rustc_middle::mir::coverage::Op;
1313

1414
use crate::coverage::counters::iter_nodes::IterNodes;
15-
use crate::coverage::counters::union_find::{FrozenUnionFind, UnionFind};
15+
use crate::coverage::counters::union_find::UnionFind;
1616

1717
#[cfg(test)]
1818
mod tests;
@@ -32,7 +32,7 @@ mod tests;
3232
pub(crate) struct MergedNodeFlowGraph<Node: Idx> {
3333
/// Maps each node to the supernode that contains it, indicated by some
3434
/// arbitrary "root" node that is part of that supernode.
35-
supernodes: FrozenUnionFind<Node>,
35+
supernodes: IndexVec<Node, Node>,
3636
/// For each node, stores the single supernode that all of its successors
3737
/// have been merged into.
3838
///
@@ -66,11 +66,11 @@ impl<Node: Idx> MergedNodeFlowGraph<Node> {
6666
})
6767
.collect::<IndexVec<G::Node, G::Node>>();
6868

69-
// Now that unification is complete, freeze the supernode forest,
69+
// Now that unification is complete, take a snapshot of the supernode forest,
7070
// and resolve each arbitrarily-chosen successor to its canonical root.
7171
// (This avoids having to explicitly resolve them later.)
72-
let supernodes = supernodes.freeze();
73-
let succ_supernodes = successors.into_iter().map(|succ| supernodes.find(succ)).collect();
72+
let supernodes = supernodes.snapshot();
73+
let succ_supernodes = successors.into_iter().map(|succ| supernodes[succ]).collect();
7474

7575
Self { supernodes, succ_supernodes }
7676
}
@@ -80,7 +80,7 @@ impl<Node: Idx> MergedNodeFlowGraph<Node> {
8080
}
8181

8282
fn is_supernode(&self, node: Node) -> bool {
83-
self.supernodes.find(node) == node
83+
self.supernodes[node] == node
8484
}
8585

8686
/// Using the information in this merged graph, together with a given
@@ -225,7 +225,7 @@ impl<'a, Node: Idx> SpantreeBuilder<'a, Node> {
225225

226226
// Get the supernode containing `this`, and make it the root of its
227227
// component of the spantree.
228-
let this_supernode = self.graph.supernodes.find(this);
228+
let this_supernode = self.graph.supernodes[this];
229229
self.yank_to_spantree_root(this_supernode);
230230

231231
// Get the supernode containing all of this's successors.

compiler/rustc_mir_transform/src/coverage/counters/union_find.rs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,9 @@ impl<Key: Idx> UnionFind<Key> {
8888
a
8989
}
9090

91-
/// Creates a snapshot of this disjoint-set forest that can no longer be
92-
/// mutated, but can be queried without mutation.
93-
pub(crate) fn freeze(&mut self) -> FrozenUnionFind<Key> {
94-
// Just resolve each key to its actual root.
95-
let roots = self.table.indices().map(|key| self.find(key)).collect();
96-
FrozenUnionFind { roots }
97-
}
98-
}
99-
100-
/// Snapshot of a disjoint-set forest that can no longer be mutated, but can be
101-
/// queried in O(1) time without mutation.
102-
///
103-
/// This is really just a wrapper around a direct mapping from keys to roots,
104-
/// but with a [`Self::find`] method that resembles [`UnionFind::find`].
105-
#[derive(Debug)]
106-
pub(crate) struct FrozenUnionFind<Key: Idx> {
107-
roots: IndexVec<Key, Key>,
108-
}
109-
110-
impl<Key: Idx> FrozenUnionFind<Key> {
111-
/// Returns the "root" key of the disjoint-set containing the given key.
112-
/// If two keys have the same root, they belong to the same set.
113-
pub(crate) fn find(&self, key: Key) -> Key {
114-
self.roots[key]
91+
/// Takes a "snapshot" of the current state of this disjoint-set forest, in
92+
/// the form of a vector that directly maps each key to its current root.
93+
pub(crate) fn snapshot(&mut self) -> IndexVec<Key, Key> {
94+
self.table.indices().map(|key| self.find(key)).collect()
11595
}
11696
}

0 commit comments

Comments
 (0)