Skip to content

Commit 280a286

Browse files
committed
Drop the cache lock earlier.
1 parent 15b0bc6 commit 280a286

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

compiler/rustc_query_system/src/query/caches.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub trait QueryCache: QueryStorage {
3737
key: &Self::Key,
3838
// `on_hit` can be called while holding a lock to the query state shard.
3939
on_hit: OnHit,
40-
) -> Result<R, QueryLookup<'s, Self::Sharded>>
40+
) -> Result<R, QueryLookup>
4141
where
4242
OnHit: FnOnce(&Self::Stored, DepNodeIndex) -> R;
4343

@@ -98,12 +98,12 @@ where
9898
state: &'s QueryCacheStore<Self>,
9999
key: &K,
100100
on_hit: OnHit,
101-
) -> Result<R, QueryLookup<'s, Self::Sharded>>
101+
) -> Result<R, QueryLookup>
102102
where
103103
OnHit: FnOnce(&V, DepNodeIndex) -> R,
104104
{
105-
let lookup = state.get_lookup(key);
106-
let result = lookup.lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key);
105+
let (lookup, lock) = state.get_lookup(key);
106+
let result = lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key);
107107

108108
if let Some((_, value)) = result {
109109
let hit_result = on_hit(&value.0, value.1);
@@ -181,12 +181,12 @@ where
181181
state: &'s QueryCacheStore<Self>,
182182
key: &K,
183183
on_hit: OnHit,
184-
) -> Result<R, QueryLookup<'s, Self::Sharded>>
184+
) -> Result<R, QueryLookup>
185185
where
186186
OnHit: FnOnce(&&'tcx V, DepNodeIndex) -> R,
187187
{
188-
let lookup = state.get_lookup(key);
189-
let result = lookup.lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key);
188+
let (lookup, lock) = state.get_lookup(key);
189+
let result = lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key);
190190

191191
if let Some((_, value)) = result {
192192
let hit_result = on_hit(&&value.0, value.1);

compiler/rustc_query_system/src/query/plumbing.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ impl<C: QueryCache> Default for QueryCacheStore<C> {
4646
}
4747

4848
/// Values used when checking a query cache which can be reused on a cache-miss to execute the query.
49-
pub struct QueryLookup<'tcx, C> {
49+
pub struct QueryLookup {
5050
pub(super) key_hash: u64,
5151
shard: usize,
52-
pub(super) lock: LockGuard<'tcx, C>,
5352
}
5453

5554
// We compute the key's hash once and then use it for both the
@@ -62,11 +61,14 @@ fn hash_for_shard<K: Hash>(key: &K) -> u64 {
6261
}
6362

6463
impl<C: QueryCache> QueryCacheStore<C> {
65-
pub(super) fn get_lookup<'tcx>(&'tcx self, key: &C::Key) -> QueryLookup<'tcx, C::Sharded> {
64+
pub(super) fn get_lookup<'tcx>(
65+
&'tcx self,
66+
key: &C::Key,
67+
) -> (QueryLookup, LockGuard<'tcx, C::Sharded>) {
6668
let key_hash = hash_for_shard(key);
6769
let shard = get_shard_index_by_hash(key_hash);
6870
let lock = self.shards.get_shard_by_index(shard).lock();
69-
QueryLookup { key_hash, shard, lock }
71+
(QueryLookup { key_hash, shard }, lock)
7072
}
7173

7274
pub fn iter_results<R>(
@@ -178,19 +180,18 @@ where
178180
/// This function is inlined because that results in a noticeable speed-up
179181
/// for some compile-time benchmarks.
180182
#[inline(always)]
181-
fn try_start<'a, 'b, CTX>(
183+
fn try_start<'b, CTX>(
182184
tcx: CTX,
183185
state: &'b QueryState<CTX::DepKind, CTX::Query, C::Key>,
184186
cache: &'b QueryCacheStore<C>,
185187
span: Span,
186188
key: &C::Key,
187-
lookup: QueryLookup<'a, C::Sharded>,
189+
lookup: QueryLookup,
188190
query: &QueryVtable<CTX, C::Key, C::Value>,
189191
) -> TryGetJob<'b, CTX::DepKind, CTX::Query, C>
190192
where
191193
CTX: QueryContext,
192194
{
193-
mem::drop(lookup.lock);
194195
let shard = lookup.shard;
195196
let mut state_lock = state.shards.get_shard_by_index(shard).lock();
196197
let lock = &mut *state_lock;
@@ -379,7 +380,7 @@ fn try_get_cached<'a, CTX, C, R, OnHit>(
379380
key: &C::Key,
380381
// `on_hit` can be called while holding a lock to the query cache
381382
on_hit: OnHit,
382-
) -> Result<R, QueryLookup<'a, C::Sharded>>
383+
) -> Result<R, QueryLookup>
383384
where
384385
C: QueryCache,
385386
CTX: QueryContext,
@@ -403,7 +404,7 @@ fn try_execute_query<CTX, C>(
403404
cache: &QueryCacheStore<C>,
404405
span: Span,
405406
key: C::Key,
406-
lookup: QueryLookup<'_, C::Sharded>,
407+
lookup: QueryLookup,
407408
query: &QueryVtable<CTX, C::Key, C::Value>,
408409
) -> C::Stored
409410
where

0 commit comments

Comments
 (0)