Skip to content

Commit fb4745b

Browse files
Seulgi Kimsgkim126
authored andcommitted
Make StateDB cache World
1 parent 1955025 commit fb4745b

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

state/src/backend.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use primitives::{Bytes, H256};
4545

4646
use super::{
4747
Account, ActionHandler, Asset, AssetAddress, AssetScheme, AssetSchemeAddress, Metadata, MetadataAddress,
48-
RegularAccount, RegularAccountAddress, Shard, ShardAddress,
48+
RegularAccount, RegularAccountAddress, Shard, ShardAddress, World, WorldAddress,
4949
};
5050

5151

@@ -95,11 +95,15 @@ pub trait TopBackend: Send {
9595
}
9696

9797
pub trait ShardBackend: Send {
98+
fn add_to_world_cache(&mut self, address: WorldAddress, item: Option<World>, modified: bool);
99+
98100
/// Add an asset entry to the cache.
99101
fn add_to_asset_scheme_cache(&mut self, addr: AssetSchemeAddress, asset: Option<AssetScheme>, modified: bool);
100102
/// Add an asset entry to the cache.
101103
fn add_to_asset_cache(&mut self, addr: AssetAddress, asset: Option<Asset>, modified: bool);
102104

105+
fn get_cached_world(&self, hash: &WorldAddress) -> Option<Option<World>>;
106+
103107
/// Get basic copy of the cached account. Not required to include storage.
104108
/// Returns 'None' if cache is disabled or if the the asset/asset scheme is not cached.
105109
fn get_cached_asset_scheme(&self, hash: &AssetSchemeAddress) -> Option<Option<AssetScheme>>;

state/src/db.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ use util_error::UtilError;
3232

3333
use super::{
3434
Account, ActionHandler, Asset, AssetAddress, AssetScheme, AssetSchemeAddress, Backend, CacheableItem, Metadata,
35-
MetadataAddress, RegularAccount, RegularAccountAddress, Shard, ShardAddress, ShardBackend, TopBackend,
35+
MetadataAddress, RegularAccount, RegularAccountAddress, Shard, ShardAddress, ShardBackend, TopBackend, World,
36+
WorldAddress,
3637
};
3738

3839
const STATE_CACHE_BLOCKS: usize = 12;
@@ -41,7 +42,8 @@ const STATE_CACHE_BLOCKS: usize = 12;
4142
const ACCOUNT_CACHE_RATIO: usize = 35;
4243
const REGULAR_ACCOUNT_CACHE_RATIO: usize = 5;
4344
const METADATA_CACHE_RATIO: usize = 1;
44-
const SHARD_CACHE_RATIO: usize = 8;
45+
const SHARD_CACHE_RATIO: usize = 7;
46+
const WORLD_CACHE_RATIO: usize = 1;
4547
const ASSET_SCHEME_CACHE_RATIO: usize = 10;
4648
const ASSET_CACHE_RATIO: usize = 40;
4749
const ACTION_DATA_CACHE_RATIO: usize = 1;
@@ -109,6 +111,7 @@ pub struct StateDB {
109111
regular_account_cache: Arc<Mutex<Cache<RegularAccount>>>,
110112
metadata_cache: Arc<Mutex<Cache<Metadata>>>,
111113
shard_cache: Arc<Mutex<Cache<Shard>>>,
114+
world_cache: Arc<Mutex<Cache<World>>>,
112115
asset_scheme_cache: Arc<Mutex<Cache<AssetScheme>>>,
113116
asset_cache: Arc<Mutex<Cache<Asset>>>,
114117
action_data_cache: Arc<Mutex<Cache<Bytes>>>,
@@ -118,6 +121,7 @@ pub struct StateDB {
118121
local_regular_account_cache: Vec<CacheQueueItem<RegularAccount>>,
119122
local_metadata_cache: Vec<CacheQueueItem<Metadata>>,
120123
local_shard_cache: Vec<CacheQueueItem<Shard>>,
124+
local_world_cache: Vec<CacheQueueItem<World>>,
121125
local_asset_scheme_cache: Vec<CacheQueueItem<AssetScheme>>,
122126
local_asset_cache: Vec<CacheQueueItem<Asset>>,
123127
local_action_data_cache: Vec<CacheQueueItem<Bytes>>,
@@ -143,6 +147,7 @@ impl StateDB {
143147
ACCOUNT_CACHE_RATIO
144148
+ METADATA_CACHE_RATIO
145149
+ SHARD_CACHE_RATIO
150+
+ WORLD_CACHE_RATIO
146151
+ ASSET_SCHEME_CACHE_RATIO
147152
+ ASSET_CACHE_RATIO
148153
+ ACTION_DATA_CACHE_RATIO
@@ -161,6 +166,9 @@ impl StateDB {
161166
let shard_cache_size = cache_size * SHARD_CACHE_RATIO / 100;
162167
let shard_cache_items = shard_cache_size / ::std::mem::size_of::<Option<Shard>>();
163168

169+
let world_cache_size = cache_size * WORLD_CACHE_RATIO / 100;
170+
let world_cache_items = world_cache_size / ::std::mem::size_of::<Option<World>>();
171+
164172
let asset_scheme_cache_size = cache_size * ASSET_SCHEME_CACHE_RATIO / 100;
165173
let asset_scheme_cache_items = asset_scheme_cache_size / ::std::mem::size_of::<Option<AssetScheme>>();
166174

@@ -188,6 +196,10 @@ impl StateDB {
188196
cache: LruCache::new(shard_cache_items),
189197
modifications: VecDeque::new(),
190198
})),
199+
world_cache: Arc::new(Mutex::new(Cache {
200+
cache: LruCache::new(world_cache_items),
201+
modifications: VecDeque::new(),
202+
})),
191203
asset_scheme_cache: Arc::new(Mutex::new(Cache {
192204
cache: LruCache::new(asset_scheme_cache_items),
193205
modifications: VecDeque::new(),
@@ -205,6 +217,7 @@ impl StateDB {
205217
local_regular_account_cache: Vec::new(),
206218
local_metadata_cache: Vec::new(),
207219
local_shard_cache: Vec::new(),
220+
local_world_cache: Vec::new(),
208221
local_asset_scheme_cache: Vec::new(),
209222
local_asset_cache: Vec::new(),
210223
local_action_data_cache: Vec::new(),
@@ -288,6 +301,17 @@ impl StateDB {
288301
&self.commit_number,
289302
);
290303

304+
Self::sync_cache_impl(
305+
enacted,
306+
retracted,
307+
is_best,
308+
&mut self.world_cache,
309+
&mut self.local_world_cache,
310+
&self.parent_hash,
311+
&self.commit_hash,
312+
&self.commit_number,
313+
);
314+
291315
Self::sync_cache_impl(
292316
enacted,
293317
retracted,
@@ -440,6 +464,7 @@ impl StateDB {
440464
regular_account_cache: self.regular_account_cache.clone(),
441465
metadata_cache: self.metadata_cache.clone(),
442466
shard_cache: self.shard_cache.clone(),
467+
world_cache: self.world_cache.clone(),
443468
asset_scheme_cache: self.asset_scheme_cache.clone(),
444469
asset_cache: self.asset_cache.clone(),
445470
action_data_cache: self.action_data_cache.clone(),
@@ -448,6 +473,7 @@ impl StateDB {
448473
local_regular_account_cache: Vec::new(),
449474
local_metadata_cache: Vec::new(),
450475
local_shard_cache: Vec::new(),
476+
local_world_cache: Vec::new(),
451477
local_asset_scheme_cache: Vec::new(),
452478
local_asset_cache: Vec::new(),
453479
local_action_data_cache: Vec::new(),
@@ -558,6 +584,7 @@ impl Clone for StateDB {
558584
regular_account_cache: self.regular_account_cache.clone(),
559585
metadata_cache: self.metadata_cache.clone(),
560586
shard_cache: self.shard_cache.clone(),
587+
world_cache: self.world_cache.clone(),
561588
asset_scheme_cache: self.asset_scheme_cache.clone(),
562589
asset_cache: self.asset_cache.clone(),
563590
action_data_cache: self.action_data_cache.clone(),
@@ -566,6 +593,7 @@ impl Clone for StateDB {
566593
local_regular_account_cache: Vec::new(),
567594
local_metadata_cache: Vec::new(),
568595
local_shard_cache: Vec::new(),
596+
local_world_cache: Vec::new(),
569597
local_asset_scheme_cache: Vec::new(),
570598
local_asset_cache: Vec::new(),
571599
local_action_data_cache: Vec::new(),
@@ -673,6 +701,14 @@ impl TopBackend for StateDB {
673701
}
674702

675703
impl ShardBackend for StateDB {
704+
fn add_to_world_cache(&mut self, address: WorldAddress, item: Option<World>, modified: bool) {
705+
self.local_world_cache.push(CacheQueueItem {
706+
address,
707+
item,
708+
modified,
709+
})
710+
}
711+
676712
fn add_to_asset_scheme_cache(&mut self, addr: AssetSchemeAddress, item: Option<AssetScheme>, modified: bool) {
677713
self.local_asset_scheme_cache.push(CacheQueueItem {
678714
address: addr,
@@ -689,6 +725,10 @@ impl ShardBackend for StateDB {
689725
})
690726
}
691727

728+
fn get_cached_world(&self, hash: &WorldAddress) -> Option<Option<World>> {
729+
self.get_cached(hash, &self.world_cache)
730+
}
731+
692732
fn get_cached_asset_scheme(&self, hash: &AssetSchemeAddress) -> Option<Option<AssetScheme>> {
693733
self.get_cached(hash, &self.asset_scheme_cache)
694734
}

0 commit comments

Comments
 (0)