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 3 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
25 changes: 16 additions & 9 deletions common_util/src/partitioned_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard},
};

/// Warning, do not use this hash in non-memory scenarios.
tanruixiang marked this conversation as resolved.
Show resolved Hide resolved
use common_types::hash::AHasher;
/// Simple partitioned `RwLock`
pub struct PartitionedRwLock<T> {
Expand Down Expand Up @@ -47,6 +48,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 +85,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 +138,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 +152,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