diff --git a/common_types/Cargo.toml b/common_types/Cargo.toml index ea8df8b490..2ffc52188e 100644 --- a/common_types/Cargo.toml +++ b/common_types/Cargo.toml @@ -16,7 +16,7 @@ test = [] [dependencies] # In alphabetical order -ahash = "0.8.3" +ahash = { version = "0.8.2", default-features = false, features = ["runtime-rng"] } 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 996dd6b6af..be0da38f14 100644 --- a/common_types/src/hash.rs +++ b/common_types/src/hash.rs @@ -7,6 +7,9 @@ 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 + Warning: Do not use this hash in non-memory scenarios, + One of the reasons is as follows: + https://github.com/tkaitchuck/aHash/blob/master/README.md#goals-and-non-goals */ pub use ahash::AHasher; use byteorder::{ByteOrder, LittleEndian}; diff --git a/common_util/src/partitioned_lock.rs b/common_util/src/partitioned_lock.rs index bca0655a75..3bbc397669 100644 --- a/common_util/src/partitioned_lock.rs +++ b/common_util/src/partitioned_lock.rs @@ -47,6 +47,11 @@ where &self.partitions[(hasher.finish() as usize) & self.partition_mask] } + + #[cfg(test)] + fn get_partition_by_index(&self, index: usize) -> &RwLock<T> { + &self.partitions[index] + } } /// Simple partitioned `Mutex` @@ -79,9 +84,13 @@ where fn get_partition<K: Eq + Hash>(&self, key: &K) -> &Mutex<T> { let mut hasher = AHasher::default(); key.hash(&mut hasher); - &self.partitions[(hasher.finish() as usize) & self.partition_mask] } + + #[cfg(test)] + fn get_partition_by_index(&self, index: usize) -> &Mutex<T> { + &self.partitions[index] + } } #[cfg(test)] @@ -128,13 +137,12 @@ mod tests { fn test_partitioned_mutex_vis_different_partition() { let tmp_vec: Vec<f32> = Vec::new(); let test_locked_map = PartitionedMutex::new(tmp_vec, 4); - let test_key_first = "test_key_first".to_string(); - let mutex_first = test_locked_map.get_partition(&test_key_first); + let mutex_first = test_locked_map.get_partition_by_index(0); + let mut _tmp_data = mutex_first.lock().unwrap(); assert!(mutex_first.try_lock().is_err()); - let test_key_second = "test_key_second".to_string(); - let mutex_second = test_locked_map.get_partition(&test_key_second); + let mutex_second = test_locked_map.get_partition_by_index(1); assert!(mutex_second.try_lock().is_ok()); assert!(mutex_first.try_lock().is_err()); } @@ -143,13 +151,11 @@ mod tests { fn test_partitioned_rwmutex_vis_different_partition() { let tmp_vec: Vec<f32> = Vec::new(); let test_locked_map = PartitionedRwLock::new(tmp_vec, 4); - let test_key_first = "test_key_first".to_string(); - let mutex_first = test_locked_map.get_partition(&test_key_first); + let mutex_first = test_locked_map.get_partition_by_index(0); let mut _tmp = mutex_first.write().unwrap(); assert!(mutex_first.try_write().is_err()); - let test_key_second = "test_key_second".to_string(); - let mutex_second_try_lock = test_locked_map.get_partition(&test_key_second); + let mutex_second_try_lock = test_locked_map.get_partition_by_index(1); assert!(mutex_second_try_lock.try_write().is_ok()); assert!(mutex_first.try_write().is_err()); }