From e6d2fa22d6fe1d9f0308415c0a5051b47cbe2f2b Mon Sep 17 00:00:00 2001 From: tanruixiang Date: Thu, 25 May 2023 18:06:38 +0800 Subject: [PATCH 1/4] update testcase --- common_types/Cargo.toml | 2 +- common_util/src/partitioned_lock.rs | 26 ++++++++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/common_types/Cargo.toml b/common_types/Cargo.toml index ea8df8b490..ce356b0777 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_util/src/partitioned_lock.rs b/common_util/src/partitioned_lock.rs index bca0655a75..6ce4abfd08 100644 --- a/common_util/src/partitioned_lock.rs +++ b/common_util/src/partitioned_lock.rs @@ -6,7 +6,7 @@ use std::{ hash::{Hash, Hasher}, sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}, }; - +/// Warning, do not use this hash in non-memory scenarios. use common_types::hash::AHasher; /// Simple partitioned `RwLock` pub struct PartitionedRwLock { @@ -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{ + &self.partitions[index] + } } /// Simple partitioned `Mutex` @@ -79,9 +84,13 @@ where fn get_partition(&self, key: &K) -> &Mutex { 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{ + &self.partitions[index] + } } #[cfg(test)] @@ -128,13 +137,12 @@ mod tests { fn test_partitioned_mutex_vis_different_partition() { let tmp_vec: Vec = 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 = 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()); } From bb1c5e0903e6379a55e13130f3e94db959de9382 Mon Sep 17 00:00:00 2001 From: tanruixiang Date: Thu, 25 May 2023 19:02:17 +0800 Subject: [PATCH 2/4] fmt --- common_util/src/partitioned_lock.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/common_util/src/partitioned_lock.rs b/common_util/src/partitioned_lock.rs index 6ce4abfd08..c98ce09216 100644 --- a/common_util/src/partitioned_lock.rs +++ b/common_util/src/partitioned_lock.rs @@ -6,6 +6,7 @@ use std::{ hash::{Hash, Hasher}, sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}, }; + /// Warning, do not use this hash in non-memory scenarios. use common_types::hash::AHasher; /// Simple partitioned `RwLock` @@ -49,7 +50,7 @@ where } #[cfg(test)] - fn get_partition_by_index(&self, index: usize) -> &RwLock{ + fn get_partition_by_index(&self, index: usize) -> &RwLock { &self.partitions[index] } } @@ -88,7 +89,7 @@ where } #[cfg(test)] - fn get_partition_by_index(&self, index: usize) -> &Mutex{ + fn get_partition_by_index(&self, index: usize) -> &Mutex { &self.partitions[index] } } From 51e10eec6717352d7455f14aeb4acdcc6848667f Mon Sep 17 00:00:00 2001 From: tanruixiang Date: Thu, 25 May 2023 21:18:25 +0800 Subject: [PATCH 3/4] update --- common_types/src/hash.rs | 3 +++ common_util/src/partitioned_lock.rs | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) 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 c98ce09216..3bbc397669 100644 --- a/common_util/src/partitioned_lock.rs +++ b/common_util/src/partitioned_lock.rs @@ -7,7 +7,6 @@ use std::{ sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}, }; -/// Warning, do not use this hash in non-memory scenarios. use common_types::hash::AHasher; /// Simple partitioned `RwLock` pub struct PartitionedRwLock { From 242053546f6147ff3492fd8a94728ad0d14889f9 Mon Sep 17 00:00:00 2001 From: tanruixiang <819464715@qq.com> Date: Thu, 25 May 2023 23:46:58 +0800 Subject: [PATCH 4/4] cargo sort --- common_types/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common_types/Cargo.toml b/common_types/Cargo.toml index ce356b0777..2ffc52188e 100644 --- a/common_types/Cargo.toml +++ b/common_types/Cargo.toml @@ -16,7 +16,7 @@ test = [] [dependencies] # In alphabetical order -ahash = { version = "0.8.2", default-features = false, features = ["runtime-rng"] } +ahash = { version = "0.8.2", default-features = false, features = ["runtime-rng"] } arrow = { workspace = true, optional = true } arrow_ext = { workspace = true } byteorder = "1.2"