Skip to content
Open
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
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ num_cpus = "1.12.0"
rayon = {version = "1.3", optional = true}
serde = {version = "1.0.105", optional = true}
seize = "0.3.3"
fast-counter = "1.0.0"

[dependencies.ahash]
version = "0.8.4"
Expand Down
34 changes: 18 additions & 16 deletions src/map.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use fast_counter::ConcurrentCounter;
use seize::Linked;

use crate::iter::*;
Expand Down Expand Up @@ -92,7 +93,7 @@ pub struct HashMap<K, V, S = crate::DefaultHashBuilder> {
/// The next table index (plus one) to split while resizing.
transfer_index: AtomicIsize,

count: AtomicIsize,
counter: ConcurrentCounter,

/// Table initialization and resizing control. When negative, the
/// table is being initialized or resized: -1 for initialization,
Expand Down Expand Up @@ -279,7 +280,7 @@ impl<K, V, S> HashMap<K, V, S> {
table: Atomic::null(),
next_table: Atomic::null(),
transfer_index: AtomicIsize::new(0),
count: AtomicIsize::new(0),
counter: ConcurrentCounter::new(num_cpus()),
size_ctl: AtomicIsize::new(0),
build_hasher: hash_builder,
collector: Collector::new(),
Expand Down Expand Up @@ -348,6 +349,12 @@ impl<K, V, S> HashMap<K, V, S> {

/// Returns the number of entries in the map.
///
/// Note that the returned value is _NOT_ an
/// atomic snapshot; invocation in the absence of concurrent
/// updates returns an accurate result, but concurrent updates that
/// occur while the sum is being calculated might not be
/// incorporated.
///
/// # Examples
///
/// ```
Expand All @@ -360,7 +367,7 @@ impl<K, V, S> HashMap<K, V, S> {
/// assert!(map.pin().len() == 2);
/// ```
pub fn len(&self) -> usize {
let n = self.count.load(Ordering::Relaxed);
let n = self.counter.sum();
if n < 0 {
0
} else {
Expand Down Expand Up @@ -1132,23 +1139,18 @@ where
}

fn add_count(&self, n: isize, resize_hint: Option<usize>, guard: &Guard<'_>) {
// TODO: implement the Java CounterCell business here

use std::cmp;
let mut count = match n.cmp(&0) {
cmp::Ordering::Greater => self.count.fetch_add(n, Ordering::SeqCst) + n,
cmp::Ordering::Less => self.count.fetch_sub(n.abs(), Ordering::SeqCst) - n,
cmp::Ordering::Equal => self.count.load(Ordering::SeqCst),
};
if n != 0 {
self.counter.add(n)
}

// if resize_hint is None, it means the caller does not want us to consider a resize.
// if it is Some(n), the caller saw n entries in a bin
if resize_hint.is_none() {
// TODO: use the resize hint
let Some(_saw_bin_length) = resize_hint else {
return;
}
};

// TODO: use the resize hint
let _saw_bin_length = resize_hint.unwrap();
let mut count = self.counter.sum();

loop {
let sc = self.size_ctl.load(Ordering::SeqCst);
Expand Down Expand Up @@ -1208,7 +1210,7 @@ where
}

// another resize may be needed!
count = self.count.load(Ordering::SeqCst);
count = self.counter.sum();
}
}

Expand Down