Skip to content

Commit 31d5742

Browse files
committed
fix(hash_cache, #175): update doc regarding the capacity
1 parent 5c7388a commit 31d5742

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Version 2
44

5+
2.3.1
6+
7+
* Fix documentation.
8+
59
2.3.0
610

711
* Fix incorrect Sync trait bounds of `Bag` and `Hash*`.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "scc"
33
description = "High-performance containers and utilities for concurrent and asynchronous programming"
44
documentation = "https://docs.rs/scc"
5-
version = "2.3.0"
5+
version = "2.3.1"
66
authors = ["wvwwvwwv <[email protected]>"]
77
edition = "2021"
88
rust-version = "1.65.0"

src/hash_cache.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1109,8 +1109,9 @@ where
11091109

11101110
/// Creates an empty [`HashCache`] with the specified capacity.
11111111
///
1112-
/// The supplied minimum and maximum capacity values are adjusted to any suitable
1113-
/// `power-of-two` values that are close to them.
1112+
/// The supplied minimum and maximum capacity values are adjusted to power-of-two values equal
1113+
/// to or larger than them, while the adjusted values cannot be in a range of (0, 64), and the
1114+
/// maximum capacity cannot be 0.
11141115
///
11151116
/// # Examples
11161117
///
@@ -1121,6 +1122,10 @@ where
11211122
///
11221123
/// let result = hashcache.capacity();
11231124
/// assert_eq!(result, 1024);
1125+
///
1126+
/// let hashcache: HashCache<u64, u32> = HashCache::with_capacity(0, 0);
1127+
/// let result = hashcache.capacity_range();
1128+
/// assert_eq!(result, 0..=64);
11241129
/// ```
11251130
#[inline]
11261131
#[must_use]

src/tests/correctness.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod hashmap_test {
1212
use std::rc::Rc;
1313
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};
1414
use std::sync::atomic::{AtomicU64, AtomicUsize};
15-
use std::sync::Arc;
15+
use std::sync::{Arc, Barrier};
1616
use tokio::sync::Barrier as AsyncBarrier;
1717

1818
static_assertions::assert_not_impl_all!(HashMap<Rc<String>, Rc<String>>: Send, Sync);
@@ -392,6 +392,47 @@ mod hashmap_test {
392392
}
393393
}
394394

395+
#[cfg_attr(miri, ignore)]
396+
#[test]
397+
fn insert_read() {
398+
for i in 0..65536 {
399+
let hashmap: Arc<HashMap<[u8; 32], usize>> = Arc::new(HashMap::default());
400+
let num_tasks = 16;
401+
let workload_size = 65536;
402+
let mut task_handles = Vec::with_capacity(num_tasks);
403+
let barrier = Arc::new(Barrier::new(num_tasks));
404+
for task_id in 0..num_tasks {
405+
let barrier_clone = barrier.clone();
406+
let hashmap_clone = hashmap.clone();
407+
task_handles.push(std::thread::spawn(move || {
408+
barrier_clone.wait();
409+
let range = (task_id * workload_size)..((task_id + 1) * workload_size);
410+
let mut key = [0_u8; 32];
411+
for id in range.clone() {
412+
for i in 0..8 {
413+
key[i] = (id >> (i * 8)) as u8;
414+
}
415+
let e = hashmap_clone.entry(key.clone()).insert_entry(id);
416+
assert_eq!(e.get(), &id);
417+
drop(e);
418+
assert!(hashmap_clone.read(&key, |_, _| ()).is_some());
419+
}
420+
for id in range {
421+
for i in 0..8 {
422+
key[i] = (id >> (i * 8)) as u8;
423+
}
424+
assert!(hashmap_clone.read(&key, |_, _| ()).is_some());
425+
}
426+
}));
427+
}
428+
println!("OK {i}");
429+
430+
for r in task_handles {
431+
assert!(r.join().is_ok());
432+
}
433+
}
434+
}
435+
395436
#[cfg_attr(miri, ignore)]
396437
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
397438
async fn entry_next_retain() {

0 commit comments

Comments
 (0)