Skip to content

Commit c6f1215

Browse files
author
Markus Westerlind
committed
perf(dep_graph): Avoid allocating a set on when the number reads are small
`reserve_and_rehash` takes up 1.4% of the runtime on the `packed-simd` benchmark which I believe is due to the number of reads are very low in many cases (see #50565 for instance). This avoids allocating the set until we start allocating the `reads` `SmallVec` but it is possible that a lower limit might be better (not tested since the improvement will be hard to spot either way).
1 parent 9381e81 commit c6f1215

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/librustc/dep_graph/graph.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -1128,11 +1128,25 @@ impl DepGraphData {
11281128
let icx = if let Some(icx) = icx { icx } else { return };
11291129
if let Some(task_deps) = icx.task_deps {
11301130
let mut task_deps = task_deps.lock();
1131+
let task_deps = &mut *task_deps;
11311132
if cfg!(debug_assertions) {
11321133
self.current.total_read_count.fetch_add(1, Relaxed);
11331134
}
1134-
if task_deps.read_set.insert(source) {
1135+
1136+
// As long as we only have a low number of reads we can avoid doing a hash
1137+
// insert and potentially allocating/reallocating the hashmap
1138+
let new_read = if task_deps.reads.len() < TASK_DEPS_READS_CAP {
1139+
task_deps.reads.iter().all(|other| *other != source)
1140+
} else {
1141+
task_deps.read_set.insert(source)
1142+
};
1143+
if new_read {
11351144
task_deps.reads.push(source);
1145+
if task_deps.reads.len() == TASK_DEPS_READS_CAP {
1146+
// Fill `read_set` with what we have so far so we can use the hashset next
1147+
// time
1148+
task_deps.read_set.extend(task_deps.reads.iter().copied());
1149+
}
11361150

11371151
#[cfg(debug_assertions)]
11381152
{
@@ -1154,10 +1168,11 @@ impl DepGraphData {
11541168
}
11551169
}
11561170

1171+
const TASK_DEPS_READS_CAP: usize = 8;
11571172
pub struct TaskDeps {
11581173
#[cfg(debug_assertions)]
11591174
node: Option<DepNode>,
1160-
reads: SmallVec<[DepNodeIndex; 8]>,
1175+
reads: SmallVec<[DepNodeIndex; TASK_DEPS_READS_CAP]>,
11611176
read_set: FxHashSet<DepNodeIndex>,
11621177
}
11631178

0 commit comments

Comments
 (0)