Skip to content

Commit

Permalink
accounts-db: Improve hashing of the read-only cache (#4300)
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 #4276
  • Loading branch information
vadorovsky authored Jan 7, 2025
1 parent dde1685 commit 2415647
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 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},
index_list::{Index, IndexList},
log::*,
Expand Down Expand Up @@ -71,7 +72,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>>,
/// When an item is first entered into the cache, it is added to the end of
/// the queue. Also each time an entry is looked up from the cache it is
/// moved to the end of the queue. As a result, items in the queue are
Expand Down Expand Up @@ -104,7 +105,7 @@ impl ReadOnlyAccountsCache {
ms_to_skip_lru_update: u32,
) -> Self {
assert!(max_data_size_lo <= max_data_size_hi);
let cache = Arc::new(DashMap::default());
let cache = Arc::new(DashMap::with_hasher(AHashRandomState::default()));
let queue = Arc::new(Mutex::<IndexList<ReadOnlyCacheKey>>::default());
let data_size = Arc::new(AtomicUsize::default());
let stats = Arc::new(AtomicReadOnlyCacheStats::default());
Expand Down Expand Up @@ -236,7 +237,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>,
queue: &Mutex<IndexList<ReadOnlyCacheKey>>,
data_size: &AtomicUsize,
) -> Option<AccountSharedData> {
Expand Down Expand Up @@ -292,7 +293,7 @@ impl ReadOnlyAccountsCache {
max_data_size_lo: usize,
max_data_size_hi: usize,
data_size: Arc<AtomicUsize>,
cache: Arc<DashMap<ReadOnlyCacheKey, ReadOnlyAccountCacheEntry>>,
cache: Arc<DashMap<ReadOnlyCacheKey, ReadOnlyAccountCacheEntry, AHashRandomState>>,
queue: Arc<Mutex<IndexList<ReadOnlyCacheKey>>>,
stats: Arc<AtomicReadOnlyCacheStats>,
) -> thread::JoinHandle<()> {
Expand Down Expand Up @@ -336,7 +337,7 @@ impl ReadOnlyAccountsCache {
fn evict(
target_data_size: usize,
data_size: &AtomicUsize,
cache: &DashMap<ReadOnlyCacheKey, ReadOnlyAccountCacheEntry>,
cache: &DashMap<ReadOnlyCacheKey, ReadOnlyAccountCacheEntry, AHashRandomState>,
queue: &Mutex<IndexList<ReadOnlyCacheKey>>,
) -> u64 {
let mut num_evicts = 0;
Expand Down

0 comments on commit 2415647

Please sign in to comment.