Skip to content

Commit

Permalink
chore: replace DefaultHash with AHasher (#928)
Browse files Browse the repository at this point in the history
## Related Issues
Closes #921 

## Detailed Changes
Replace the hash algorithm from `DefaultHasher` to `ahash` for better
performance in partition lock.

## Test Plan
Time cost to compute hash on a 10-char string:
- DefaultHash: 10: 33.6364 ns
- ahash: 10: 19.0412 ns
- murmur3: 10: 33.0394 ns
  • Loading branch information
tanruixiang authored May 24, 2023
1 parent d90a94d commit 8fe556c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
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

0 comments on commit 8fe556c

Please sign in to comment.