Skip to content

Commit 01c8446

Browse files
committed
Refactor and speed up ActiveGuardDrop.
To fix a small perf regression from #153471.
1 parent 3b1b0ef commit 01c8446

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

compiler/rustc_query_impl/src/execution.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::hash::Hash;
2-
use std::mem::ManuallyDrop;
32

43
use rustc_data_structures::hash_table::{Entry, HashTable};
54
use rustc_data_structures::stack::ensure_sufficient_stack;
@@ -135,6 +134,7 @@ where
135134
state: &'tcx QueryState<'tcx, K>,
136135
key: K,
137136
key_hash: u64,
137+
poison_on_drop: bool,
138138
}
139139

140140
impl<'tcx, K> ActiveJobGuard<'tcx, K>
@@ -143,21 +143,27 @@ where
143143
{
144144
/// Completes the query by updating the query cache with the `result`,
145145
/// signals the waiter, and forgets the guard so it won't poison the query.
146-
fn complete<C>(self, cache: &C, value: C::Value, dep_node_index: DepNodeIndex)
146+
#[inline]
147+
fn complete<C>(mut self, cache: &C, value: C::Value, dep_node_index: DepNodeIndex)
147148
where
148149
C: QueryCache<Key = K>,
149150
{
150151
// Mark as complete before we remove the job from the active state
151152
// so no other thread can re-execute this query.
152153
cache.complete(self.key, value, dep_node_index);
153154

154-
let mut this = ManuallyDrop::new(self);
155-
156155
// Drop everything without poisoning the query.
157-
this.drop_and_maybe_poison(/* poison */ false);
156+
self.poison_on_drop = false;
157+
158+
// `drop` is now called because this method consumes `self`.
158159
}
160+
}
159161

160-
fn drop_and_maybe_poison(&mut self, poison: bool) {
162+
impl<'tcx, K> Drop for ActiveJobGuard<'tcx, K>
163+
where
164+
K: Eq + Hash + Copy,
165+
{
166+
fn drop(&mut self) {
161167
let status = {
162168
let mut shard = self.state.active.lock_shard_by_hash(self.key_hash);
163169
match shard.find_entry(self.key_hash, equivalent_key(self.key)) {
@@ -169,7 +175,7 @@ where
169175
}
170176
Ok(occupied) => {
171177
let ((key, status), vacant) = occupied.remove();
172-
if poison {
178+
if self.poison_on_drop {
173179
vacant.insert((key, ActiveKeyStatus::Poisoned));
174180
}
175181
status
@@ -185,18 +191,6 @@ where
185191
}
186192
}
187193

188-
impl<'tcx, K> Drop for ActiveJobGuard<'tcx, K>
189-
where
190-
K: Eq + Hash + Copy,
191-
{
192-
#[inline(never)]
193-
#[cold]
194-
fn drop(&mut self) {
195-
// Poison the query so jobs waiting on it panic.
196-
self.drop_and_maybe_poison(/* poison */ true);
197-
}
198-
}
199-
200194
#[cold]
201195
#[inline(never)]
202196
fn cycle_error<'tcx, C: QueryCache>(
@@ -340,7 +334,7 @@ fn execute_job<'tcx, C: QueryCache, const INCR: bool>(
340334
) -> (C::Value, Option<DepNodeIndex>) {
341335
// Set up a guard object that will automatically poison the query if a
342336
// panic occurs while executing the query (or any intermediate plumbing).
343-
let job_guard = ActiveJobGuard { state: &query.state, key, key_hash };
337+
let job_guard = ActiveJobGuard { state: &query.state, key, key_hash, poison_on_drop: true };
344338

345339
debug_assert_eq!(tcx.dep_graph.is_fully_enabled(), INCR);
346340

0 commit comments

Comments
 (0)