Skip to content

Commit 4b8f431

Browse files
committed
Auto merge of rust-lang#103214 - Nilstrieb:set-theory, r=petrochenkov
Use Set instead of Vec in transitive_relation Helps with rust-lang#103195. It doesn't fix the underlying quadraticness but it makes it _a lot_ faster to an extent where even doubling the amount of nested references still takes less than two seconds (50s on nightly). I want to see whether this causes regressions (because the vec was usually quite small) or improvements (as lookup for bigger sets is now much faster) in real code.
2 parents d7dd01f + d45f025 commit 4b8f431

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

compiler/rustc_data_structures/src/transitive_relation.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::frozen::Frozen;
2-
use crate::fx::FxIndexSet;
2+
use crate::fx::{FxHashSet, FxIndexSet};
33
use rustc_index::bit_set::BitMatrix;
44
use std::fmt::Debug;
55
use std::hash::Hash;
@@ -16,7 +16,7 @@ pub struct TransitiveRelationBuilder<T> {
1616

1717
// List of base edges in the graph. Require to compute transitive
1818
// closure.
19-
edges: Vec<Edge>,
19+
edges: FxHashSet<Edge>,
2020
}
2121

2222
#[derive(Debug)]
@@ -52,10 +52,10 @@ impl<T: Eq + Hash> Default for TransitiveRelationBuilder<T> {
5252
}
5353
}
5454

55-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Debug)]
55+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Debug, Hash)]
5656
struct Index(usize);
5757

58-
#[derive(Clone, PartialEq, Eq, Debug)]
58+
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
5959
struct Edge {
6060
source: Index,
6161
target: Index,
@@ -99,9 +99,7 @@ impl<T: Eq + Hash + Copy> TransitiveRelationBuilder<T> {
9999
let a = self.add_index(a);
100100
let b = self.add_index(b);
101101
let edge = Edge { source: a, target: b };
102-
if !self.edges.contains(&edge) {
103-
self.edges.push(edge);
104-
}
102+
self.edges.insert(edge);
105103
}
106104

107105
/// Compute the transitive closure derived from the edges, and converted to

0 commit comments

Comments
 (0)