Skip to content

Commit

Permalink
Remove hashbrown from trie cache. (paritytech#2632)
Browse files Browse the repository at this point in the history
Using hashmap instead (hashset do not expose entry), to get the default
random hasher her.
  • Loading branch information
cheme authored Dec 8, 2023
1 parent b190e18 commit 3078eca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
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
26 changes: 16 additions & 10 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]>, (), ahash::RandomState>,

/// 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 @@ -491,7 +491,7 @@ impl<H: Eq + std::hash::Hash + Clone + Copy + AsRef<[u8]>> SharedValueCache<H> {
max_inline_size,
max_heap_size,
heap_size: 0,
known_storage_keys: Default::default(),
known_storage_keys: HashMap::with_hasher(RANDOM_STATE.clone()),
items_evicted: 0,
max_items_evicted: 0, // Will be set during `update`.
},
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());
}
}

0 comments on commit 3078eca

Please sign in to comment.