Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hashbrown from trie cache. #2632

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions substrate/primitives/trie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ harness = false
[dependencies]
ahash = { version = "0.8.2", optional = true }
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false }
hashbrown = { version = "0.13.2", optional = true }
hash-db = { version = "0.16.0", default-features = false }
lazy_static = { version = "1.4.0", optional = true }
memory-db = { version = "0.32.0", default-features = false }
Expand Down Expand Up @@ -50,7 +49,6 @@ std = [
"ahash",
"codec/std",
"hash-db/std",
"hashbrown",
"lazy_static",
"memory-db/std",
"nohash-hasher",
Expand Down
24 changes: 15 additions & 9 deletions substrate/primitives/trie/src/cache/shared_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
///! that combines both caches and is exported to the outside.
use super::{CacheSize, NodeCached};
use hash_db::Hasher;
use hashbrown::{hash_set::Entry as SetEntry, HashSet};
use nohash_hasher::BuildNoHashHasher;
use parking_lot::{Mutex, RwLock, RwLockWriteGuard};
use schnellru::LruMap;
use std::{
collections::{hash_map::Entry as SetEntry, HashMap},
hash::{BuildHasher, Hasher as _},
sync::Arc,
};
Expand Down Expand Up @@ -148,7 +148,7 @@ pub struct SharedValueCacheLimiter {
heap_size: usize,

/// A set with all of the keys deduplicated to save on memory.
known_storage_keys: HashSet<Arc<[u8]>>,
known_storage_keys: HashMap<Arc<[u8]>, ()>,

/// A counter with the number of elements that got evicted from the cache.
///
Expand Down Expand Up @@ -189,10 +189,10 @@ where
}

self.heap_size += new_item_heap_size;
entry.insert();
entry.insert(());
},
SetEntry::Occupied(entry) => {
key.storage_key = entry.get().clone();
key.storage_key = entry.key().clone();
},
}

Expand Down Expand Up @@ -778,7 +778,9 @@ mod tests {
assert_eq!(1, cache.lru.limiter_mut().known_storage_keys.len());
assert_eq!(
3, // Two instances inside the cache + one extra in `known_storage_keys`.
Arc::strong_count(cache.lru.limiter_mut().known_storage_keys.get(&key[..]).unwrap())
Arc::strong_count(
cache.lru.limiter_mut().known_storage_keys.get_key_value(&key[..]).unwrap().0
)
);
assert_eq!(key.len(), cache.lru.limiter().heap_size);
assert_eq!(cache.lru.len(), 2);
Expand All @@ -792,7 +794,9 @@ mod tests {
assert_eq!(1, cache.lru.limiter_mut().known_storage_keys.len());
assert_eq!(
3,
Arc::strong_count(cache.lru.limiter_mut().known_storage_keys.get(&key[..]).unwrap())
Arc::strong_count(
cache.lru.limiter_mut().known_storage_keys.get_key_value(&key[..]).unwrap().0
)
);
assert_eq!(key.len(), cache.lru.limiter().heap_size);
assert_eq!(cache.lru.len(), 2);
Expand All @@ -812,7 +816,9 @@ mod tests {
assert_eq!(1, cache.lru.limiter_mut().known_storage_keys.len());
assert_eq!(
3,
Arc::strong_count(cache.lru.limiter_mut().known_storage_keys.get(&key[..]).unwrap())
Arc::strong_count(
cache.lru.limiter_mut().known_storage_keys.get_key_value(&key[..]).unwrap().0
)
);
assert_eq!(key.len(), cache.lru.limiter().heap_size);
assert_eq!(cache.lru.len(), 2);
Expand All @@ -833,7 +839,7 @@ mod tests {
assert_eq!(cache.lru.limiter().items_evicted, 2);
assert_eq!(10, cache.lru.len());
assert_eq!(10, cache.lru.limiter_mut().known_storage_keys.len());
assert!(cache.lru.limiter_mut().known_storage_keys.get(&key[..]).is_none());
assert!(cache.lru.limiter_mut().known_storage_keys.get_key_value(&key[..]).is_none());
assert_eq!(key.len() * 10, cache.lru.limiter().heap_size);
assert_eq!(cache.lru.len(), 10);
assert!(cache.lru.limiter().heap_size <= cache.lru.limiter().max_heap_size);
Expand All @@ -854,6 +860,6 @@ mod tests {
vec![],
);

assert!(cache.lru.limiter_mut().known_storage_keys.get(&key[..]).is_none());
assert!(cache.lru.limiter_mut().known_storage_keys.get_key_value(&key[..]).is_none());
}
}
Loading