Skip to content

Commit da55e55

Browse files
committed
graphql: Move cache weight calculation outside the lock
1 parent 289fc85 commit da55e55

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

graphql/src/execution/execution.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ impl CacheByBlock {
4545
}
4646

4747
/// Returns `true` if the insert was successful or `false` if the cache was full.
48-
fn insert(&mut self, key: QueryHash, value: Arc<QueryResult>) -> bool {
48+
fn insert(&mut self, key: QueryHash, value: Arc<QueryResult>, weight: usize) -> bool {
4949
// We never try to insert errors into this cache, and always resolve some value.
5050
assert!(value.errors.is_none());
51-
let weight = value.data.as_ref().unwrap().weight();
5251
let fits_in_cache = self.weight + weight <= self.max_weight;
5352
if fits_in_cache {
5453
self.weight += weight;
@@ -378,11 +377,13 @@ pub fn execute_root_selection_set<R: Resolver>(
378377
// It would be redundant to insert herd cache hits.
379378
let no_cache = herd_hit || result.has_errors();
380379
if let (false, Some(key), Some(block_ptr)) = (no_cache, key, block_ptr) {
380+
// Calculate the weight outside the lock.
381+
let weight = result.data.as_ref().unwrap().weight();
381382
let mut cache = QUERY_CACHE.write().unwrap();
382383

383384
// If there is already a cache by the block of this query, just add it there.
384385
if let Some(cache_by_block) = cache.iter_mut().find(|c| c.block == block_ptr) {
385-
let cache_insert = cache_by_block.insert(key, result.cheap_clone());
386+
let cache_insert = cache_by_block.insert(key, result.cheap_clone(), weight);
386387
if cache_insert {
387388
ctx.cache_status.store(CacheStatus::Insert);
388389
}
@@ -406,7 +407,7 @@ pub fn execute_root_selection_set<R: Resolver>(
406407
// Create a new cache by block, insert this entry, and add it to the QUERY_CACHE.
407408
let max_weight = *QUERY_CACHE_MAX_MEM / *QUERY_CACHE_BLOCKS;
408409
let mut cache_by_block = CacheByBlock::new(block_ptr, max_weight);
409-
let cache_insert = cache_by_block.insert(key, result.cheap_clone());
410+
let cache_insert = cache_by_block.insert(key, result.cheap_clone(), weight);
410411
if cache_insert {
411412
ctx.cache_status.store(CacheStatus::Insert);
412413
}

0 commit comments

Comments
 (0)