Skip to content

Commit b136f26

Browse files
committed
Do not store the DepNode twice.
1 parent 27732c2 commit b136f26

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4371,6 +4371,7 @@ dependencies = [
43714371
name = "rustc_query_system"
43724372
version = "0.0.0"
43734373
dependencies = [
4374+
"hashbrown",
43744375
"parking_lot 0.11.2",
43754376
"rustc-rayon-core",
43764377
"rustc_arena",

compiler/rustc_query_system/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ doctest = false
88

99
[dependencies]
1010
rustc_arena = { path = "../rustc_arena" }
11+
hashbrown = { version = "0.12.0", features = ["raw"] }
1112
tracing = "0.1"
1213
rustc-rayon-core = { version = "0.4.0", optional = true }
1314
rustc_ast = { path = "../rustc_ast" }

compiler/rustc_query_system/src/dep_graph/serialized.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
1515
use super::query::DepGraphQuery;
1616
use super::{DepKind, DepNode, DepNodeIndex};
17+
use hashbrown::raw::RawTable;
1718
use rustc_data_structures::fingerprint::Fingerprint;
18-
use rustc_data_structures::fx::FxHashMap;
19+
use rustc_data_structures::fx::{FxHashMap, FxHasher};
1920
use rustc_data_structures::memmap::Mmap;
2021
use rustc_data_structures::owning_ref::OwningRef;
2122
use rustc_data_structures::profiling::SelfProfilerRef;
@@ -25,6 +26,7 @@ use rustc_serialize::opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixed
2526
use rustc_serialize::{Decodable, Encodable};
2627
use smallvec::SmallVec;
2728
use std::convert::TryInto;
29+
use std::hash::{Hash, Hasher};
2830

2931
// The maximum value of `SerializedDepNodeIndex` leaves the upper two bits
3032
// unused so that we can store multiple index types in `CompressedHybridIndex`,
@@ -69,7 +71,13 @@ pub struct SerializedDepGraph<K: DepKind> {
6971
/// The set of all DepNodes in the graph and their position in the mmap.
7072
nodes: Option<OwningRef<Mmap, [(DepNode<K>, u32)]>>,
7173
/// Reciprocal map to `nodes`.
72-
index: FxHashMap<DepNode<K>, SerializedDepNodeIndex>,
74+
index: RawTable<u32>,
75+
}
76+
77+
fn hash_node<K: DepKind>(key: &DepNode<K>) -> u64 {
78+
let mut h = FxHasher::default();
79+
key.hash(&mut h);
80+
h.finish()
7381
}
7482

7583
impl<K: DepKind> Default for SerializedDepGraph<K> {
@@ -91,7 +99,10 @@ impl<K: DepKind> SerializedDepGraph<K> {
9199

92100
#[inline]
93101
pub fn node_to_index_opt(&self, dep_node: &DepNode<K>) -> Option<SerializedDepNodeIndex> {
94-
self.index.get(dep_node).cloned()
102+
let nodes = self.nodes.as_ref()?;
103+
let hash = hash_node(dep_node);
104+
let index = self.index.get(hash, |&i| *dep_node == nodes[i as usize].0)?;
105+
Some(SerializedDepNodeIndex::from_u32(*index))
95106
}
96107

97108
#[inline]
@@ -112,7 +123,7 @@ impl<K: DepKind> SerializedDepGraph<K> {
112123

113124
#[inline]
114125
pub fn fingerprint_of(&self, dep_node: &DepNode<K>) -> Option<Fingerprint> {
115-
let index = self.index.get(dep_node).cloned()?;
126+
let index = self.node_to_index_opt(dep_node)?;
116127
Some(self.fingerprint_by_index(index))
117128
}
118129

@@ -131,7 +142,7 @@ impl<K: DepKind> SerializedDepGraph<K> {
131142
}
132143

133144
pub fn node_count(&self) -> usize {
134-
self.index.len()
145+
if let Some(ref nodes) = self.nodes { nodes.len() } else { 0 }
135146
}
136147

137148
#[instrument(level = "debug", skip(mmap))]
@@ -152,11 +163,14 @@ impl<K: DepKind> SerializedDepGraph<K> {
152163
unsafe { d.mmap_slice_at::<(DepNode<K>, u32)>(nodes_position, node_count) }
153164
});
154165

155-
let index: FxHashMap<_, _> = nodes
156-
.iter()
157-
.enumerate()
158-
.map(|(idx, &(dep_node, _))| (dep_node, SerializedDepNodeIndex::from_usize(idx)))
159-
.collect();
166+
let mut index = RawTable::with_capacity(node_count);
167+
let mut known_hashes = Vec::with_capacity(node_count);
168+
for (idx, (dep_node, _)) in nodes.iter().enumerate() {
169+
let idx = idx.try_into().unwrap();
170+
let hash = hash_node(dep_node);
171+
known_hashes.push(hash);
172+
index.insert(hash, idx, |&i| known_hashes[i as usize]);
173+
}
160174

161175
SerializedDepGraph { nodes: Some(nodes), index }
162176
}

0 commit comments

Comments
 (0)