Skip to content

Commit 49bf306

Browse files
committed
Inline the dep-graph reverse-index lookup fast path
node_to_index_opt is the reverse-index lookup used while marking dep nodes green, so it runs many times per incremental rebuild. The per-kind map build sat inside the get_or_init closure, which kept the lookup from inlining, so every lookup paid an out-of-line call even after the map was built. Move the build into build_fingerprint_map and call get_or_init directly in node_to_index_opt, so the lookup fast path is a plain OnceLock load. No data structures change.
1 parent d2f2412 commit 49bf306

1 file changed

Lines changed: 31 additions & 33 deletions

File tree

compiler/rustc_middle/src/dep_graph/serialized.rs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -168,39 +168,35 @@ struct LazyKindIndex {
168168

169169
impl LazyKindIndex {
170170
/// Returns this kind's `key_fingerprint -> node index` map.
171-
fn fingerprint_map(
171+
fn build_fingerprint_map(
172172
&self,
173173
kind: DepKind,
174174
nodes: &IndexSlice<SerializedDepNodeIndex, DepNode>,
175175
nodes_by_kind: &[Option<SerializedDepNodeIndex>],
176176
profiler: &Option<SelfProfilerRef>,
177-
) -> &UnhashMap<PackedFingerprint, SerializedDepNodeIndex> {
178-
self.map.get_or_init(|| {
179-
let _prof_timer = profiler
180-
.as_ref()
181-
.map(|p| p.generic_activity("incr_comp_load_dep_graph_reverse_index"));
182-
let range = (self.start as usize)..(self.start as usize + self.len as usize);
183-
let mut map =
184-
UnhashMap::with_capacity_and_hasher(self.len as usize, Default::default());
185-
for &idx in &nodes_by_kind[range] {
186-
let idx = idx.expect("counting sort fills every slot of a kind's range");
187-
let node = nodes[idx];
188-
debug_assert_eq!(node.kind, kind);
189-
if map.insert(node.key_fingerprint, idx).is_some()
190-
// Side effect nodes can legitimately share a fingerprint.
191-
&& node.kind != DepKind::SideEffect
192-
{
193-
panic!(
194-
"Error: A dep graph node ({kind:?}) does not have an unique index. \
195-
Running a clean build on a nightly compiler with \
196-
`-Z incremental-verify-ich` can help narrow down the issue for reporting. \
197-
A clean build may also work around the issue.\n
198-
DepNode: {node:?}"
199-
)
200-
}
177+
) -> UnhashMap<PackedFingerprint, SerializedDepNodeIndex> {
178+
let _prof_timer =
179+
profiler.as_ref().map(|p| p.generic_activity("incr_comp_load_dep_graph_reverse_index"));
180+
let range = (self.start as usize)..(self.start as usize + self.len as usize);
181+
let mut map = UnhashMap::with_capacity_and_hasher(self.len as usize, Default::default());
182+
for &idx in &nodes_by_kind[range] {
183+
let idx = idx.expect("counting sort fills every slot of a kind's range");
184+
let node = nodes[idx];
185+
debug_assert_eq!(node.kind, kind);
186+
if map.insert(node.key_fingerprint, idx).is_some()
187+
// Side effect nodes can legitimately share a fingerprint.
188+
&& node.kind != DepKind::SideEffect
189+
{
190+
panic!(
191+
"Error: A dep graph node ({kind:?}) does not have an unique index. \
192+
Running a clean build on a nightly compiler with \
193+
`-Z incremental-verify-ich` can help narrow down the issue for reporting. \
194+
A clean build may also work around the issue.\n
195+
DepNode: {node:?}"
196+
)
201197
}
202-
map
203-
})
198+
}
199+
map
204200
}
205201
}
206202

@@ -235,12 +231,14 @@ impl SerializedDepGraph {
235231
#[inline]
236232
pub fn node_to_index_opt(&self, dep_node: &DepNode) -> Option<SerializedDepNodeIndex> {
237233
let kind = self.reverse_index.kinds.get(dep_node.kind.as_usize())?;
238-
let map = kind.fingerprint_map(
239-
dep_node.kind,
240-
&self.nodes,
241-
&self.reverse_index.nodes_by_kind,
242-
&self.profiler,
243-
);
234+
let map = kind.map.get_or_init(|| {
235+
kind.build_fingerprint_map(
236+
dep_node.kind,
237+
&self.nodes,
238+
&self.reverse_index.nodes_by_kind,
239+
&self.profiler,
240+
)
241+
});
244242
map.get(&dep_node.key_fingerprint).copied()
245243
}
246244

0 commit comments

Comments
 (0)