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: add hint about ahash usage #931

Merged
merged 5 commits into from
May 26, 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
2 changes: 1 addition & 1 deletion common_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions common_types/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
24 changes: 15 additions & 9 deletions common_util/src/partitioned_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand Down