Skip to content

Commit

Permalink
accounts-db: Improve hashing of the read-only cache
Browse files Browse the repository at this point in the history
`DashMap` uses SipHash by default. Change it to AHash, which is more
lightweight.

Ref anza-xyz#4276
  • Loading branch information
vadorovsky committed Jan 6, 2025
1 parent d0a8348 commit 377fbcd
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions accounts-db/src/read_only_accounts_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#[cfg(feature = "dev-context-only-utils")]
use qualifier_attr::qualifiers;
use {
ahash::random_state::RandomState as AHashRandomState,
dashmap::{mapref::entry::Entry, DashMap},
log::*,
rand::{
Expand Down Expand Up @@ -77,7 +78,7 @@ struct AtomicReadOnlyCacheStats {
#[cfg_attr(feature = "dev-context-only-utils", qualifiers(pub))]
#[derive(Debug)]
pub(crate) struct ReadOnlyAccountsCache {
cache: Arc<DashMap<ReadOnlyCacheKey, ReadOnlyAccountCacheEntry>>,
cache: Arc<DashMap<ReadOnlyCacheKey, ReadOnlyAccountCacheEntry, AHashRandomState>>,
_max_data_size_lo: usize,
_max_data_size_hi: usize,
data_size: Arc<AtomicUsize>,
Expand All @@ -103,7 +104,10 @@ impl ReadOnlyAccountsCache {
) -> Self {
assert!(max_data_size_lo <= max_data_size_hi);
assert!(evict_sample_size > 0);
let cache = Arc::new(DashMap::with_shard_amount(SHARDS));
let cache = Arc::new(DashMap::with_hasher_and_shard_amount(
AHashRandomState::default(),
SHARDS,
));
let data_size = Arc::new(AtomicUsize::default());
let stats = Arc::new(AtomicReadOnlyCacheStats::default());
let evictor_exit_flag = Arc::new(AtomicBool::new(false));
Expand Down Expand Up @@ -210,7 +214,7 @@ impl ReadOnlyAccountsCache {
/// Removes `key` from the cache, if present, and returns the removed account
fn do_remove(
key: &ReadOnlyCacheKey,
cache: &DashMap<ReadOnlyCacheKey, ReadOnlyAccountCacheEntry>,
cache: &DashMap<ReadOnlyCacheKey, ReadOnlyAccountCacheEntry, AHashRandomState>,
data_size: &AtomicUsize,
) -> Option<AccountSharedData> {
let (_, entry) = cache.remove(key)?;
Expand Down Expand Up @@ -262,7 +266,7 @@ impl ReadOnlyAccountsCache {
max_data_size_hi: usize,
data_size: Arc<AtomicUsize>,
evict_sample_size: usize,
cache: Arc<DashMap<ReadOnlyCacheKey, ReadOnlyAccountCacheEntry>>,
cache: Arc<DashMap<ReadOnlyCacheKey, ReadOnlyAccountCacheEntry, AHashRandomState>>,
stats: Arc<AtomicReadOnlyCacheStats>,
) -> thread::JoinHandle<()> {
thread::Builder::new()
Expand Down Expand Up @@ -310,7 +314,7 @@ impl ReadOnlyAccountsCache {
target_data_size: usize,
data_size: &AtomicUsize,
evict_sample_size: usize,
cache: &DashMap<ReadOnlyCacheKey, ReadOnlyAccountCacheEntry>,
cache: &DashMap<ReadOnlyCacheKey, ReadOnlyAccountCacheEntry, AHashRandomState>,
) -> u64 {
let mut rng = thread_rng();

Expand Down

0 comments on commit 377fbcd

Please sign in to comment.