Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 9db957c

Browse files
authored
Rework the trie cache (#12982)
* Rework the trie cache * Align `state-machine` tests * Bump `schnellru` to 0.1.1 * Fix off-by-one * Align to review comments * Bump `ahash` to 0.8.2 * Bump `schnellru` to 0.2.0 * Bump `schnellru` to 0.2.1 * Remove unnecessary bound * Remove unnecessary loop when calculating maximum memory usage * Remove unnecessary `mut`s
1 parent 8b4331c commit 9db957c

File tree

9 files changed

+1016
-550
lines changed

9 files changed

+1016
-550
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/db/src/bench.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<B: BlockT> BenchmarkingState<B> {
110110
proof_recorder_root: Cell::new(root),
111111
enable_tracking,
112112
// Enable the cache, but do not sync anything to the shared state.
113-
shared_trie_cache: SharedTrieCache::new(CacheSize::Maximum(0)),
113+
shared_trie_cache: SharedTrieCache::new(CacheSize::new(0)),
114114
};
115115

116116
state.add_whitelist_to_tracker();

client/db/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ impl<Block: BlockT> Backend<Block> {
12431243
blocks_pruning: config.blocks_pruning,
12441244
genesis_state: RwLock::new(None),
12451245
shared_trie_cache: config.trie_cache_maximum_size.map(|maximum_size| {
1246-
SharedTrieCache::new(sp_trie::cache::CacheSize::Maximum(maximum_size))
1246+
SharedTrieCache::new(sp_trie::cache::CacheSize::new(maximum_size))
12471247
}),
12481248
};
12491249

client/finality-grandpa/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ readme = "README.md"
1414
targets = ["x86_64-unknown-linux-gnu"]
1515

1616
[dependencies]
17-
ahash = "0.7.6"
17+
ahash = "0.8.2"
1818
array-bytes = "4.1"
1919
async-trait = "0.1.57"
2020
dyn-clone = "1.0"

client/network-gossip/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ readme = "README.md"
1414
targets = ["x86_64-unknown-linux-gnu"]
1515

1616
[dependencies]
17-
ahash = "0.7.6"
17+
ahash = "0.8.2"
1818
futures = "0.3.21"
1919
futures-timer = "3.0.1"
2020
libp2p = "0.50.0"

primitives/state-machine/src/trie_backend.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -412,19 +412,19 @@ pub mod tests {
412412
fn $name() {
413413
let parameters = vec![
414414
(StateVersion::V0, None, None),
415-
(StateVersion::V0, Some(SharedCache::new(CacheSize::Unlimited)), None),
415+
(StateVersion::V0, Some(SharedCache::new(CacheSize::unlimited())), None),
416416
(StateVersion::V0, None, Some(Recorder::default())),
417417
(
418418
StateVersion::V0,
419-
Some(SharedCache::new(CacheSize::Unlimited)),
419+
Some(SharedCache::new(CacheSize::unlimited())),
420420
Some(Recorder::default()),
421421
),
422422
(StateVersion::V1, None, None),
423-
(StateVersion::V1, Some(SharedCache::new(CacheSize::Unlimited)), None),
423+
(StateVersion::V1, Some(SharedCache::new(CacheSize::unlimited())), None),
424424
(StateVersion::V1, None, Some(Recorder::default())),
425425
(
426426
StateVersion::V1,
427-
Some(SharedCache::new(CacheSize::Unlimited)),
427+
Some(SharedCache::new(CacheSize::unlimited())),
428428
Some(Recorder::default()),
429429
),
430430
];
@@ -760,7 +760,7 @@ pub mod tests {
760760
.clone()
761761
.for_each(|i| assert_eq!(trie.storage(&[i]).unwrap().unwrap(), vec![i; size_content]));
762762

763-
for cache in [Some(SharedTrieCache::new(CacheSize::Unlimited)), None] {
763+
for cache in [Some(SharedTrieCache::new(CacheSize::unlimited())), None] {
764764
// Run multiple times to have a different cache conditions.
765765
for i in 0..5 {
766766
if let Some(cache) = &cache {
@@ -793,7 +793,7 @@ pub mod tests {
793793
proof_record_works_with_iter_inner(StateVersion::V1);
794794
}
795795
fn proof_record_works_with_iter_inner(state_version: StateVersion) {
796-
for cache in [Some(SharedTrieCache::new(CacheSize::Unlimited)), None] {
796+
for cache in [Some(SharedTrieCache::new(CacheSize::unlimited())), None] {
797797
// Run multiple times to have a different cache conditions.
798798
for i in 0..5 {
799799
if let Some(cache) = &cache {
@@ -870,7 +870,7 @@ pub mod tests {
870870
assert_eq!(in_memory.child_storage(child_info_2, &[i]).unwrap().unwrap(), vec![i])
871871
});
872872

873-
for cache in [Some(SharedTrieCache::new(CacheSize::Unlimited)), None] {
873+
for cache in [Some(SharedTrieCache::new(CacheSize::unlimited())), None] {
874874
// Run multiple times to have a different cache conditions.
875875
for i in 0..5 {
876876
eprintln!("Running with cache {}, iteration {}", cache.is_some(), i);
@@ -1002,7 +1002,7 @@ pub mod tests {
10021002
nodes
10031003
};
10041004

1005-
let cache = SharedTrieCache::<BlakeTwo256>::new(CacheSize::Unlimited);
1005+
let cache = SharedTrieCache::<BlakeTwo256>::new(CacheSize::unlimited());
10061006
{
10071007
let local_cache = cache.local_cache();
10081008
let mut trie_cache = local_cache.as_trie_db_cache(child_1_root);
@@ -1093,7 +1093,7 @@ pub mod tests {
10931093

10941094
#[test]
10951095
fn new_data_is_added_to_the_cache() {
1096-
let shared_cache = SharedTrieCache::new(CacheSize::Unlimited);
1096+
let shared_cache = SharedTrieCache::new(CacheSize::unlimited());
10971097
let new_data = vec![
10981098
(&b"new_data0"[..], Some(&b"0"[..])),
10991099
(&b"new_data1"[..], Some(&b"1"[..])),
@@ -1159,7 +1159,7 @@ pub mod tests {
11591159
assert_eq!(in_memory.child_storage(child_info_1, &key).unwrap().unwrap(), child_trie_1_val);
11601160
assert_eq!(in_memory.child_storage(child_info_2, &key).unwrap().unwrap(), child_trie_2_val);
11611161

1162-
for cache in [Some(SharedTrieCache::new(CacheSize::Unlimited)), None] {
1162+
for cache in [Some(SharedTrieCache::new(CacheSize::unlimited())), None] {
11631163
// Run multiple times to have a different cache conditions.
11641164
for i in 0..5 {
11651165
eprintln!("Running with cache {}, iteration {}", cache.is_some(), i);

primitives/trie/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ name = "bench"
1818
harness = false
1919

2020
[dependencies]
21-
ahash = { version = "0.7.6", optional = true }
21+
ahash = { version = "0.8.2", optional = true }
2222
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false }
2323
hashbrown = { version = "0.12.3", optional = true }
2424
hash-db = { version = "0.15.2", default-features = false }
2525
lazy_static = { version = "1.4.0", optional = true }
26-
lru = { version = "0.8.1", optional = true }
2726
memory-db = { version = "0.31.0", default-features = false }
2827
nohash-hasher = { version = "0.2.0", optional = true }
2928
parking_lot = { version = "0.12.1", optional = true }
@@ -34,6 +33,7 @@ trie-db = { version = "0.24.0", default-features = false }
3433
trie-root = { version = "0.17.0", default-features = false }
3534
sp-core = { version = "7.0.0", default-features = false, path = "../core" }
3635
sp-std = { version = "5.0.0", default-features = false, path = "../std" }
36+
schnellru = { version = "0.2.1", optional = true }
3737

3838
[dev-dependencies]
3939
array-bytes = "4.1"
@@ -50,7 +50,7 @@ std = [
5050
"hashbrown",
5151
"hash-db/std",
5252
"lazy_static",
53-
"lru",
53+
"schnellru",
5454
"memory-db/std",
5555
"nohash-hasher",
5656
"parking_lot",

0 commit comments

Comments
 (0)