Skip to content

Commit

Permalink
accounts-db: Use NoHashHasher in AccountStorageMap
Browse files Browse the repository at this point in the history
`DashMap` uses SipHash by default. `AccountStorageMap` uses `Slot` as a
key and therefore it doesn't have any chance of collisions. We can skip
hashing the key entirely.
  • Loading branch information
vadorovsky committed Feb 6, 2025
1 parent 2974f02 commit ba05292
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
7 changes: 4 additions & 3 deletions accounts-db/src/account_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use {
crate::accounts_db::{AccountStorageEntry, AccountsFileId},
dashmap::DashMap,
solana_clock::Slot,
solana_nohash_hasher::BuildNoHashHasher,
std::sync::Arc,
};

Expand All @@ -18,7 +19,7 @@ pub struct AccountStorageReference {
pub id: AccountsFileId,
}

pub type AccountStorageMap = DashMap<Slot, AccountStorageReference>;
pub type AccountStorageMap = DashMap<Slot, AccountStorageReference, BuildNoHashHasher<Slot>>;

#[derive(Default, Debug)]
pub struct AccountStorage {
Expand All @@ -27,7 +28,7 @@ pub struct AccountStorage {
/// while shrink is operating on a slot, there can be 2 append vecs active for that slot
/// Once the index has been updated to only refer to the new append vec, the single entry for the slot in 'map' can be updated.
/// Entries in 'shrink_in_progress_map' can be found by 'get_account_storage_entry'
shrink_in_progress_map: DashMap<Slot, Arc<AccountStorageEntry>>,
shrink_in_progress_map: DashMap<Slot, Arc<AccountStorageEntry>, BuildNoHashHasher<Slot>>,
}

impl AccountStorage {
Expand Down Expand Up @@ -227,7 +228,7 @@ impl AccountStorage {

/// iterate contents of AccountStorage without exposing internals
pub struct AccountStorageIter<'a> {
iter: dashmap::iter::Iter<'a, Slot, AccountStorageReference>,
iter: dashmap::iter::Iter<'a, Slot, AccountStorageReference, BuildNoHashHasher<Slot>>,
}

impl<'a> AccountStorageIter<'a> {
Expand Down
6 changes: 5 additions & 1 deletion runtime/src/bank/serde_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod tests {
},
stakes::{SerdeStakesToStakeFormat, Stakes, StakesEnum},
},
ahash::RandomState as AHashRandomState,
solana_accounts_db::{
account_storage::{AccountStorageMap, AccountStorageReference},
accounts_db::{
Expand Down Expand Up @@ -53,7 +54,10 @@ mod tests {
storage_access: StorageAccess,
) -> Result<StorageAndNextAccountsFileId, AccountsFileError> {
let storage_entries = accounts_db.get_storages(RangeFull).0;
let storage: AccountStorageMap = AccountStorageMap::with_capacity(storage_entries.len());
let storage: AccountStorageMap = AccountStorageMap::with_capacity_and_hasher(
storage_entries.len(),
AHashRandomState::new(),
);
let mut next_append_vec_id = 0;
for storage_entry in storage_entries.into_iter() {
// Copy file to new directory
Expand Down
6 changes: 5 additions & 1 deletion runtime/src/serde_snapshot/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod serde_snapshot_tests {
},
snapshot_utils::{get_storages_to_serialize, StorageAndNextAccountsFileId},
},
ahash::RandomState as AHashRandomState,
bincode::{serialize_into, Error},
log::info,
rand::{thread_rng, Rng},
Expand Down Expand Up @@ -133,7 +134,10 @@ mod serde_snapshot_tests {
storage_access: StorageAccess,
) -> Result<StorageAndNextAccountsFileId, AccountsFileError> {
let storage_entries = accounts_db.get_storages(RangeFull).0;
let storage: AccountStorageMap = AccountStorageMap::with_capacity(storage_entries.len());
let storage: AccountStorageMap = AccountStorageMap::with_capacity_and_hasher(
storage_entries.len(),
AHashRandomState::new(),
);
let mut next_append_vec_id = 0;
for storage_entry in storage_entries.into_iter() {
// Copy file to new directory
Expand Down
6 changes: 5 additions & 1 deletion runtime/src/snapshot_utils/snapshot_storage_rebuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use {
self, reconstruct_single_storage, remap_and_reconstruct_single_storage,
snapshot_storage_lengths_from_fields, SerializedAccountsFileId,
},
ahash::RandomState as AHashRandomState,
crossbeam_channel::{select, unbounded, Receiver, Sender},
dashmap::DashMap,
log::*,
Expand Down Expand Up @@ -118,7 +119,10 @@ impl SnapshotStorageRebuilder {
snapshot_from: SnapshotFrom,
storage_access: StorageAccess,
) -> Self {
let storage = DashMap::with_capacity(snapshot_storage_lengths.len());
let storage = DashMap::with_capacity_and_hasher(
snapshot_storage_lengths.len(),
AHashRandomState::new(),
);
let storage_paths: DashMap<_, _> = snapshot_storage_lengths
.iter()
.map(|(slot, storage_lengths)| {
Expand Down

0 comments on commit ba05292

Please sign in to comment.