@@ -45,10 +45,9 @@ impl CacheByBlock {
45
45
}
46
46
47
47
/// 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 {
49
49
// We never try to insert errors into this cache, and always resolve some value.
50
50
assert ! ( value. errors. is_none( ) ) ;
51
- let weight = value. data . as_ref ( ) . unwrap ( ) . weight ( ) ;
52
51
let fits_in_cache = self . weight + weight <= self . max_weight ;
53
52
if fits_in_cache {
54
53
self . weight += weight;
@@ -378,11 +377,13 @@ pub fn execute_root_selection_set<R: Resolver>(
378
377
// It would be redundant to insert herd cache hits.
379
378
let no_cache = herd_hit || result. has_errors ( ) ;
380
379
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 ( ) ;
381
382
let mut cache = QUERY_CACHE . write ( ) . unwrap ( ) ;
382
383
383
384
// If there is already a cache by the block of this query, just add it there.
384
385
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 ) ;
386
387
if cache_insert {
387
388
ctx. cache_status . store ( CacheStatus :: Insert ) ;
388
389
}
@@ -406,7 +407,7 @@ pub fn execute_root_selection_set<R: Resolver>(
406
407
// Create a new cache by block, insert this entry, and add it to the QUERY_CACHE.
407
408
let max_weight = * QUERY_CACHE_MAX_MEM / * QUERY_CACHE_BLOCKS ;
408
409
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 ) ;
410
411
if cache_insert {
411
412
ctx. cache_status . store ( CacheStatus :: Insert ) ;
412
413
}
0 commit comments