23
23
use std:: collections:: { VecDeque , HashSet , HashMap } ;
24
24
use std:: sync:: Arc ;
25
25
use std:: hash:: Hash as StdHash ;
26
- use parking_lot:: { Mutex , RwLock , RwLockUpgradableReadGuard } ;
26
+ use parking_lot:: { RwLock , RwLockUpgradableReadGuard } ;
27
27
use linked_hash_map:: { LinkedHashMap , Entry } ;
28
28
use hash_db:: Hasher ;
29
29
use sp_runtime:: traits:: { Block as BlockT , Header , HashFor , NumberFor } ;
@@ -222,7 +222,7 @@ impl<B: BlockT> Cache<B> {
222
222
}
223
223
}
224
224
225
- pub type SharedCache < B > = Arc < Mutex < Cache < B > > > ;
225
+ pub type SharedCache < B > = Arc < RwLock < Cache < B > > > ;
226
226
227
227
/// Fix lru storage size for hash (small 64ko).
228
228
const FIX_LRU_HASH_SIZE : usize = 65_536 ;
@@ -234,7 +234,7 @@ pub fn new_shared_cache<B: BlockT>(
234
234
) -> SharedCache < B > {
235
235
let top = child_ratio. 1 . saturating_sub ( child_ratio. 0 ) ;
236
236
Arc :: new (
237
- Mutex :: new (
237
+ RwLock :: new (
238
238
Cache {
239
239
lru_storage : LRUMap (
240
240
LinkedHashMap :: new ( ) , 0 , shared_cache_size * top / child_ratio. 1
@@ -337,7 +337,7 @@ impl<B: BlockT> CacheChanges<B> {
337
337
commit_number : Option < NumberFor < B > > ,
338
338
is_best : bool ,
339
339
) {
340
- let mut cache = self . shared_cache . lock ( ) ;
340
+ let mut cache = self . shared_cache . write ( ) ;
341
341
trace ! (
342
342
"Syncing cache, id = (#{:?}, {:?}), parent={:?}, best={}" ,
343
343
commit_number,
@@ -527,12 +527,15 @@ impl<S: StateBackend<HashFor<B>>, B: BlockT> StateBackend<HashFor<B>> for Cachin
527
527
528
528
return Ok ( entry)
529
529
}
530
- let mut cache = self . cache . shared_cache . lock ( ) ;
531
- if Self :: is_allowed ( Some ( key) , None , & self . cache . parent_hash , & cache. modifications ) {
532
- if let Some ( entry) = cache. lru_storage . get ( key) . map ( |a| a. clone ( ) ) {
533
- trace ! ( "Found in shared cache: {:?}" , HexDisplay :: from( & key) ) ;
534
- self . usage . tally_key_read ( key, entry. as_ref ( ) , true ) ;
535
- return Ok ( entry)
530
+ {
531
+ let cache = self . cache . shared_cache . upgradable_read ( ) ;
532
+ if Self :: is_allowed ( Some ( key) , None , & self . cache . parent_hash , & cache. modifications ) {
533
+ let mut cache = RwLockUpgradableReadGuard :: upgrade ( cache) ;
534
+ if let Some ( entry) = cache. lru_storage . get ( key) . map ( |a| a. clone ( ) ) {
535
+ trace ! ( "Found in shared cache: {:?}" , HexDisplay :: from( & key) ) ;
536
+ self . usage . tally_key_read ( key, entry. as_ref ( ) , true ) ;
537
+ return Ok ( entry)
538
+ }
536
539
}
537
540
}
538
541
trace ! ( "Cache miss: {:?}" , HexDisplay :: from( & key) ) ;
@@ -548,11 +551,14 @@ impl<S: StateBackend<HashFor<B>>, B: BlockT> StateBackend<HashFor<B>> for Cachin
548
551
trace ! ( "Found hash in local cache: {:?}" , HexDisplay :: from( & key) ) ;
549
552
return Ok ( entry)
550
553
}
551
- let mut cache = self . cache . shared_cache . lock ( ) ;
552
- if Self :: is_allowed ( Some ( key) , None , & self . cache . parent_hash , & cache. modifications ) {
553
- if let Some ( entry) = cache. lru_hashes . get ( key) . map ( |a| a. 0 . clone ( ) ) {
554
- trace ! ( "Found hash in shared cache: {:?}" , HexDisplay :: from( & key) ) ;
555
- return Ok ( entry)
554
+ {
555
+ let cache = self . cache . shared_cache . upgradable_read ( ) ;
556
+ if Self :: is_allowed ( Some ( key) , None , & self . cache . parent_hash , & cache. modifications ) {
557
+ let mut cache = RwLockUpgradableReadGuard :: upgrade ( cache) ;
558
+ if let Some ( entry) = cache. lru_hashes . get ( key) . map ( |a| a. 0 . clone ( ) ) {
559
+ trace ! ( "Found hash in shared cache: {:?}" , HexDisplay :: from( & key) ) ;
560
+ return Ok ( entry)
561
+ }
556
562
}
557
563
}
558
564
trace ! ( "Cache hash miss: {:?}" , HexDisplay :: from( & key) ) ;
@@ -574,13 +580,16 @@ impl<S: StateBackend<HashFor<B>>, B: BlockT> StateBackend<HashFor<B>> for Cachin
574
580
self . usage . tally_child_key_read ( & key, entry, true )
575
581
)
576
582
}
577
- let mut cache = self . cache . shared_cache . lock ( ) ;
578
- if Self :: is_allowed ( None , Some ( & key) , & self . cache . parent_hash , & cache. modifications ) {
579
- if let Some ( entry) = cache. lru_child_storage . get ( & key) . map ( |a| a. clone ( ) ) {
580
- trace ! ( "Found in shared cache: {:?}" , key) ;
581
- return Ok (
582
- self . usage . tally_child_key_read ( & key, entry, true )
583
- )
583
+ {
584
+ let cache = self . cache . shared_cache . upgradable_read ( ) ;
585
+ if Self :: is_allowed ( None , Some ( & key) , & self . cache . parent_hash , & cache. modifications ) {
586
+ let mut cache = RwLockUpgradableReadGuard :: upgrade ( cache) ;
587
+ if let Some ( entry) = cache. lru_child_storage . get ( & key) . map ( |a| a. clone ( ) ) {
588
+ trace ! ( "Found in shared cache: {:?}" , key) ;
589
+ return Ok (
590
+ self . usage . tally_child_key_read ( & key, entry, true )
591
+ )
592
+ }
584
593
}
585
594
}
586
595
trace ! ( "Cache miss: {:?}" , key) ;
@@ -1274,7 +1283,7 @@ mod tests {
1274
1283
true ,
1275
1284
) ;
1276
1285
// 32 key, 3 byte size
1277
- assert_eq ! ( shared. lock ( ) . used_storage_cache_size( ) , 35 /* bytes */ ) ;
1286
+ assert_eq ! ( shared. read ( ) . used_storage_cache_size( ) , 35 /* bytes */ ) ;
1278
1287
1279
1288
let key = H256 :: random ( ) [ ..] . to_vec ( ) ;
1280
1289
s. cache . sync_cache (
@@ -1287,7 +1296,7 @@ mod tests {
1287
1296
true ,
1288
1297
) ;
1289
1298
// 35 + (2 * 32) key, 2 byte size
1290
- assert_eq ! ( shared. lock ( ) . used_storage_cache_size( ) , 101 /* bytes */ ) ;
1299
+ assert_eq ! ( shared. read ( ) . used_storage_cache_size( ) , 101 /* bytes */ ) ;
1291
1300
}
1292
1301
1293
1302
#[ test]
@@ -1313,7 +1322,7 @@ mod tests {
1313
1322
true ,
1314
1323
) ;
1315
1324
// 32 key, 4 byte size
1316
- assert_eq ! ( shared. lock ( ) . used_storage_cache_size( ) , 36 /* bytes */ ) ;
1325
+ assert_eq ! ( shared. read ( ) . used_storage_cache_size( ) , 36 /* bytes */ ) ;
1317
1326
1318
1327
let key = H256 :: random ( ) [ ..] . to_vec ( ) ;
1319
1328
s. cache . sync_cache (
@@ -1326,7 +1335,7 @@ mod tests {
1326
1335
true ,
1327
1336
) ;
1328
1337
// 32 key, 2 byte size
1329
- assert_eq ! ( shared. lock ( ) . used_storage_cache_size( ) , 34 /* bytes */ ) ;
1338
+ assert_eq ! ( shared. read ( ) . used_storage_cache_size( ) , 34 /* bytes */ ) ;
1330
1339
}
1331
1340
1332
1341
#[ test]
@@ -1379,7 +1388,7 @@ mod tests {
1379
1388
1380
1389
// Restart (or unknown block?), clear caches.
1381
1390
{
1382
- let mut cache = s. cache . shared_cache . lock ( ) ;
1391
+ let mut cache = s. cache . shared_cache . write ( ) ;
1383
1392
let cache = & mut * cache;
1384
1393
cache. lru_storage . clear ( ) ;
1385
1394
cache. lru_hashes . clear ( ) ;
@@ -1426,7 +1435,7 @@ mod tests {
1426
1435
Some ( 1 ) ,
1427
1436
true ,
1428
1437
) ;
1429
- assert_eq ! ( shared. lock ( ) . lru_storage. get( & key) . unwrap( ) , & Some ( vec![ 1 ] ) ) ;
1438
+ assert_eq ! ( shared. write ( ) . lru_storage. get( & key) . unwrap( ) , & Some ( vec![ 1 ] ) ) ;
1430
1439
1431
1440
let mut s = CachingState :: new (
1432
1441
InMemoryBackend :: < BlakeTwo256 > :: default ( ) ,
@@ -1445,7 +1454,7 @@ mod tests {
1445
1454
false ,
1446
1455
) ;
1447
1456
1448
- assert_eq ! ( shared. lock ( ) . lru_storage. get( & key) . unwrap( ) , & Some ( vec![ 1 ] ) ) ;
1457
+ assert_eq ! ( shared. write ( ) . lru_storage. get( & key) . unwrap( ) , & Some ( vec![ 1 ] ) ) ;
1449
1458
1450
1459
let mut s = CachingState :: new (
1451
1460
InMemoryBackend :: < BlakeTwo256 > :: default ( ) ,
@@ -1800,7 +1809,7 @@ mod qc {
1800
1809
1801
1810
std:: mem:: swap ( fork_chain, & mut new_fork) ;
1802
1811
1803
- self . shared . lock ( ) . sync ( & retracted, & enacted) ;
1812
+ self . shared . write ( ) . sync ( & retracted, & enacted) ;
1804
1813
1805
1814
self . head_state (
1806
1815
self . canon . last ( )
0 commit comments