Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: replace DefaultHash with AHasher #928

Merged
merged 6 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions common_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ test = []

[dependencies]
# In alphabetical order
ahash = "0.8.3"
arrow = { workspace = true, optional = true }
arrow_ext = { workspace = true }
byteorder = "1.2"
Expand Down
8 changes: 8 additions & 0 deletions common_types/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0.

// custom hash mod

/* We compared the speed difference between murmur3 and ahash for a string of
length 10, and the results show that ahash has a clear advantage.
Average time to DefaultHash a string of length 10: 33.6364 nanoseconds
Average time to ahash a string of length 10: 19.0412 nanoseconds
Average time to murmur3 a string of length 10: 33.0394 nanoseconds
*/
pub use ahash::AHasher;
use byteorder::{ByteOrder, LittleEndian};
use murmur3::murmur3_x64_128;

Expand Down
6 changes: 3 additions & 3 deletions common_util/src/partitioned_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
//! Partitioned locks

use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard},
};

use common_types::hash::AHasher;
/// Simple partitioned `RwLock`
pub struct PartitionedRwLock<T> {
partitions: Vec<RwLock<T>>,
Expand Down Expand Up @@ -42,7 +42,7 @@ where
}

fn get_partition<K: Eq + Hash>(&self, key: &K) -> &RwLock<T> {
let mut hasher = DefaultHasher::new();
let mut hasher = AHasher::default();
key.hash(&mut hasher);

&self.partitions[(hasher.finish() as usize) & self.partition_mask]
Expand Down Expand Up @@ -77,7 +77,7 @@ where
}

fn get_partition<K: Eq + Hash>(&self, key: &K) -> &Mutex<T> {
let mut hasher = DefaultHasher::new();
let mut hasher = AHasher::default();
key.hash(&mut hasher);

&self.partitions[(hasher.finish() as usize) & self.partition_mask]
Expand Down