@@ -27,7 +27,7 @@ use kvdb::DBTransaction;
2727use kvdb_memorydb;
2828use lru_cache:: LruCache ;
2929use parking_lot:: Mutex ;
30- use primitives:: H256 ;
30+ use primitives:: { Bytes , H256 } ;
3131use util_error:: UtilError ;
3232
3333use super :: {
@@ -40,9 +40,10 @@ const STATE_CACHE_BLOCKS: usize = 12;
4040// The percentage of supplied cache size to go to accounts.
4141const ACCOUNT_CACHE_RATIO : usize = 40 ;
4242const METADATA_CACHE_RATIO : usize = 1 ;
43- const SHARD_CACHE_RATIO : usize = 9 ;
43+ const SHARD_CACHE_RATIO : usize = 8 ;
4444const ASSET_SCHEME_CACHE_RATIO : usize = 10 ;
4545const ASSET_CACHE_RATIO : usize = 40 ;
46+ const ACTION_DATA_CACHE_RATIO : usize = 1 ;
4647
4748/// Shared canonical state cache.
4849struct Cache < Item >
@@ -108,13 +109,15 @@ pub struct StateDB {
108109 shard_cache : Arc < Mutex < Cache < Shard > > > ,
109110 asset_scheme_cache : Arc < Mutex < Cache < AssetScheme > > > ,
110111 asset_cache : Arc < Mutex < Cache < Asset > > > ,
112+ action_data_cache : Arc < Mutex < Cache < Bytes > > > ,
111113
112114 /// Local dirty cache.
113115 local_account_cache : Vec < CacheQueueItem < Account > > ,
114116 local_metadata_cache : Vec < CacheQueueItem < Metadata > > ,
115117 local_shard_cache : Vec < CacheQueueItem < Shard > > ,
116118 local_asset_scheme_cache : Vec < CacheQueueItem < AssetScheme > > ,
117119 local_asset_cache : Vec < CacheQueueItem < Asset > > ,
120+ local_action_data_cache : Vec < CacheQueueItem < Bytes > > ,
118121 /// Hash of the block on top of which this instance was created or
119122 /// `None` if cache is disabled
120123 parent_hash : Option < H256 > ,
@@ -137,6 +140,7 @@ impl StateDB {
137140 + SHARD_CACHE_RATIO
138141 + ASSET_SCHEME_CACHE_RATIO
139142 + ASSET_CACHE_RATIO
143+ + ACTION_DATA_CACHE_RATIO
140144 ) ;
141145
142146 let account_cache_size = cache_size * ACCOUNT_CACHE_RATIO / 100 ;
@@ -154,6 +158,9 @@ impl StateDB {
154158 let asset_cache_size = cache_size * ASSET_CACHE_RATIO / 100 ;
155159 let asset_cache_items = asset_cache_size / :: std:: mem:: size_of :: < Option < Asset > > ( ) ;
156160
161+ let action_data_cache_size = cache_size * ACTION_DATA_CACHE_RATIO / 100 ;
162+ let action_data_cache_items = action_data_cache_size / :: std:: mem:: size_of :: < Option < Bytes > > ( ) ;
163+
157164 StateDB {
158165 db,
159166 account_cache : Arc :: new ( Mutex :: new ( Cache {
@@ -176,12 +183,17 @@ impl StateDB {
176183 cache : LruCache :: new ( asset_cache_items) ,
177184 modifications : VecDeque :: new ( ) ,
178185 } ) ) ,
186+ action_data_cache : Arc :: new ( Mutex :: new ( Cache {
187+ cache : LruCache :: new ( action_data_cache_items) ,
188+ modifications : VecDeque :: new ( ) ,
189+ } ) ) ,
179190
180191 local_account_cache : Vec :: new ( ) ,
181192 local_metadata_cache : Vec :: new ( ) ,
182193 local_shard_cache : Vec :: new ( ) ,
183194 local_asset_scheme_cache : Vec :: new ( ) ,
184195 local_asset_cache : Vec :: new ( ) ,
196+ local_action_data_cache : Vec :: new ( ) ,
185197 parent_hash : None ,
186198 commit_hash : None ,
187199 commit_number : None ,
@@ -271,6 +283,17 @@ impl StateDB {
271283 & self . commit_hash ,
272284 & self . commit_number ,
273285 ) ;
286+
287+ Self :: sync_cache_impl (
288+ enacted,
289+ retracted,
290+ is_best,
291+ & mut self . action_data_cache ,
292+ & mut self . local_action_data_cache ,
293+ & self . parent_hash ,
294+ & self . commit_hash ,
295+ & self . commit_number ,
296+ ) ;
274297 }
275298
276299 fn sync_cache_impl < Item > (
@@ -392,12 +415,14 @@ impl StateDB {
392415 shard_cache : self . shard_cache . clone ( ) ,
393416 asset_scheme_cache : self . asset_scheme_cache . clone ( ) ,
394417 asset_cache : self . asset_cache . clone ( ) ,
418+ action_data_cache : self . action_data_cache . clone ( ) ,
395419
396420 local_account_cache : Vec :: new ( ) ,
397421 local_metadata_cache : Vec :: new ( ) ,
398422 local_shard_cache : Vec :: new ( ) ,
399423 local_asset_scheme_cache : Vec :: new ( ) ,
400424 local_asset_cache : Vec :: new ( ) ,
425+ local_action_data_cache : Vec :: new ( ) ,
401426
402427 parent_hash : Some ( parent. clone ( ) ) ,
403428 commit_hash : None ,
@@ -425,6 +450,7 @@ impl StateDB {
425450 + Self :: mem_used_impl ( & self . shard_cache . lock ( ) )
426451 + Self :: mem_used_impl ( & self . asset_scheme_cache . lock ( ) )
427452 + Self :: mem_used_impl ( & self . asset_cache . lock ( ) )
453+ + Self :: mem_used_impl ( & self . action_data_cache . lock ( ) )
428454 }
429455
430456 /// Returns underlying `JournalDB`.
@@ -503,12 +529,14 @@ impl Clone for StateDB {
503529 shard_cache : self . shard_cache . clone ( ) ,
504530 asset_scheme_cache : self . asset_scheme_cache . clone ( ) ,
505531 asset_cache : self . asset_cache . clone ( ) ,
532+ action_data_cache : self . action_data_cache . clone ( ) ,
506533
507534 local_account_cache : Vec :: new ( ) ,
508535 local_metadata_cache : Vec :: new ( ) ,
509536 local_shard_cache : Vec :: new ( ) ,
510537 local_asset_scheme_cache : Vec :: new ( ) ,
511538 local_asset_cache : Vec :: new ( ) ,
539+ local_action_data_cache : Vec :: new ( ) ,
512540
513541 parent_hash : None ,
514542 commit_hash : None ,
@@ -552,6 +580,14 @@ impl TopBackend for StateDB {
552580 } )
553581 }
554582
583+ fn add_to_action_data_cache ( & mut self , address : H256 , item : Option < Bytes > , modified : bool ) {
584+ self . local_action_data_cache . push ( CacheQueueItem {
585+ address,
586+ item,
587+ modified,
588+ } )
589+ }
590+
555591 fn get_cached_account ( & self , addr : & Address ) -> Option < Option < Account > > {
556592 self . get_cached ( addr, & self . account_cache )
557593 }
@@ -564,6 +600,10 @@ impl TopBackend for StateDB {
564600 self . get_cached ( addr, & self . shard_cache )
565601 }
566602
603+ fn get_cached_action_data ( & self , key : & H256 ) -> Option < Option < Bytes > > {
604+ self . get_cached ( key, & self . action_data_cache )
605+ }
606+
567607 fn get_cached_account_with < F , U > ( & self , a : & Address , f : F ) -> Option < U >
568608 where
569609 F : FnOnce ( Option < & mut Account > ) -> U , {
0 commit comments