Skip to content

Commit 6303f4c

Browse files
authored
chore: add hint about ahash usage (#931)
## Related Issues Closes #921 ## Detailed Changes Be careful not to use `ahash` for non-memory scenarios. Note that when an upstream dependency also references the same library, their `FEATURES` are merged. Reference: [1] https://github.com/tkaitchuck/aHash/blob/master/README.md?plain=1#L22 ## Test Plan
1 parent d62e414 commit 6303f4c

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

common_types/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test = []
1616

1717
[dependencies]
1818
# In alphabetical order
19-
ahash = "0.8.3"
19+
ahash = { version = "0.8.2", default-features = false, features = ["runtime-rng"] }
2020
arrow = { workspace = true, optional = true }
2121
arrow_ext = { workspace = true }
2222
byteorder = "1.2"

common_types/src/hash.rs

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
Average time to DefaultHash a string of length 10: 33.6364 nanoseconds
88
Average time to ahash a string of length 10: 19.0412 nanoseconds
99
Average time to murmur3 a string of length 10: 33.0394 nanoseconds
10+
Warning: Do not use this hash in non-memory scenarios,
11+
One of the reasons is as follows:
12+
https://github.com/tkaitchuck/aHash/blob/master/README.md#goals-and-non-goals
1013
*/
1114
pub use ahash::AHasher;
1215
use byteorder::{ByteOrder, LittleEndian};

common_util/src/partitioned_lock.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ where
4747

4848
&self.partitions[(hasher.finish() as usize) & self.partition_mask]
4949
}
50+
51+
#[cfg(test)]
52+
fn get_partition_by_index(&self, index: usize) -> &RwLock<T> {
53+
&self.partitions[index]
54+
}
5055
}
5156

5257
/// Simple partitioned `Mutex`
@@ -79,9 +84,13 @@ where
7984
fn get_partition<K: Eq + Hash>(&self, key: &K) -> &Mutex<T> {
8085
let mut hasher = AHasher::default();
8186
key.hash(&mut hasher);
82-
8387
&self.partitions[(hasher.finish() as usize) & self.partition_mask]
8488
}
89+
90+
#[cfg(test)]
91+
fn get_partition_by_index(&self, index: usize) -> &Mutex<T> {
92+
&self.partitions[index]
93+
}
8594
}
8695

8796
#[cfg(test)]
@@ -128,13 +137,12 @@ mod tests {
128137
fn test_partitioned_mutex_vis_different_partition() {
129138
let tmp_vec: Vec<f32> = Vec::new();
130139
let test_locked_map = PartitionedMutex::new(tmp_vec, 4);
131-
let test_key_first = "test_key_first".to_string();
132-
let mutex_first = test_locked_map.get_partition(&test_key_first);
140+
let mutex_first = test_locked_map.get_partition_by_index(0);
141+
133142
let mut _tmp_data = mutex_first.lock().unwrap();
134143
assert!(mutex_first.try_lock().is_err());
135144

136-
let test_key_second = "test_key_second".to_string();
137-
let mutex_second = test_locked_map.get_partition(&test_key_second);
145+
let mutex_second = test_locked_map.get_partition_by_index(1);
138146
assert!(mutex_second.try_lock().is_ok());
139147
assert!(mutex_first.try_lock().is_err());
140148
}
@@ -143,13 +151,11 @@ mod tests {
143151
fn test_partitioned_rwmutex_vis_different_partition() {
144152
let tmp_vec: Vec<f32> = Vec::new();
145153
let test_locked_map = PartitionedRwLock::new(tmp_vec, 4);
146-
let test_key_first = "test_key_first".to_string();
147-
let mutex_first = test_locked_map.get_partition(&test_key_first);
154+
let mutex_first = test_locked_map.get_partition_by_index(0);
148155
let mut _tmp = mutex_first.write().unwrap();
149156
assert!(mutex_first.try_write().is_err());
150157

151-
let test_key_second = "test_key_second".to_string();
152-
let mutex_second_try_lock = test_locked_map.get_partition(&test_key_second);
158+
let mutex_second_try_lock = test_locked_map.get_partition_by_index(1);
153159
assert!(mutex_second_try_lock.try_write().is_ok());
154160
assert!(mutex_first.try_write().is_err());
155161
}

0 commit comments

Comments
 (0)