From 8fe556c53abe693f410fbf257d1d9478b4072595 Mon Sep 17 00:00:00 2001 From: Ruixiang Tan Date: Wed, 24 May 2023 19:46:16 +0800 Subject: [PATCH] chore: replace DefaultHash with AHasher (#928) ## 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 --- Cargo.lock | 1 + common_types/Cargo.toml | 1 + common_types/src/hash.rs | 8 ++++++++ common_util/src/partitioned_lock.rs | 6 +++--- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 502de2e6dd..d7c45fdc5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1380,6 +1380,7 @@ dependencies = [ name = "common_types" version = "1.2.0" dependencies = [ + "ahash 0.8.3", "arrow 38.0.0", "arrow_ext", "byteorder", diff --git a/common_types/Cargo.toml b/common_types/Cargo.toml index 37dfe2be7d..ea8df8b490 100644 --- a/common_types/Cargo.toml +++ b/common_types/Cargo.toml @@ -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" diff --git a/common_types/src/hash.rs b/common_types/src/hash.rs index 9edc8c69cb..996dd6b6af 100644 --- a/common_types/src/hash.rs +++ b/common_types/src/hash.rs @@ -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; diff --git a/common_util/src/partitioned_lock.rs b/common_util/src/partitioned_lock.rs index 36cc3c03b5..bca0655a75 100644 --- a/common_util/src/partitioned_lock.rs +++ b/common_util/src/partitioned_lock.rs @@ -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 { partitions: Vec>, @@ -42,7 +42,7 @@ where } fn get_partition(&self, key: &K) -> &RwLock { - let mut hasher = DefaultHasher::new(); + let mut hasher = AHasher::default(); key.hash(&mut hasher); &self.partitions[(hasher.finish() as usize) & self.partition_mask] @@ -77,7 +77,7 @@ where } fn get_partition(&self, key: &K) -> &Mutex { - let mut hasher = DefaultHasher::new(); + let mut hasher = AHasher::default(); key.hash(&mut hasher); &self.partitions[(hasher.finish() as usize) & self.partition_mask]