Remove hashbrown from trie cache. (#2632)

Using hashmap instead (hashset do not expose entry), to get the default
random hasher her.
This commit is contained in:
cheme
2023-12-08 06:28:04 +01:00
committed by GitHub
parent fde5d8fe6c
commit 34c991e2cf
3 changed files with 16 additions and 13 deletions
Generated
-1
View File
@@ -18073,7 +18073,6 @@ dependencies = [
"array-bytes 6.1.0",
"criterion 0.4.0",
"hash-db",
"hashbrown 0.13.2",
"lazy_static",
"memory-db",
"nohash-hasher",
-2
View File
@@ -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 }
@@ -50,7 +49,6 @@ std = [
"ahash",
"codec/std",
"hash-db/std",
"hashbrown",
"lazy_static",
"memory-db/std",
"nohash-hasher",
+16 -10
View File
@@ -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,
};
@@ -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.
///
@@ -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();
},
}
@@ -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`.
},
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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());
}
}