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

Sharded counter optimisation #109

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 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.2.1"
fast-counter = "0.0.2"

[dependencies.ahash]
version = "0.7.6"
Expand All @@ -36,6 +37,10 @@ rayon = "1.3"
criterion = "0.3"
serde_json = "1.0.50"

[features]
default = []
nightly = ["fast-counter/nightly"]

[[bench]]
name = "flurry_dashmap"
harness = false
Expand Down
26 changes: 14 additions & 12 deletions src/map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use seize::Linked;
use fast_counter::ConcurrentCounter;

use crate::iter::*;
use crate::node::*;
Expand Down Expand Up @@ -93,7 +94,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 @@ -280,7 +281,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 @@ -349,6 +350,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 @@ -361,7 +368,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 @@ -1141,21 +1148,16 @@ 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),
};
self.counter.add(n);
JackThomson2 marked this conversation as resolved.
Show resolved Hide resolved

// 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() {
return;
}

let mut count = self.counter.sum();

// TODO: use the resize hint
let _saw_bin_length = resize_hint.unwrap();

Expand Down Expand Up @@ -1217,7 +1219,7 @@ where
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ impl<K, V> TreeBin<K, V> {
guard: &'g Guard<'_>,
) {
guard.retire(bin.as_ptr(), |mut link| {
let bin = unsafe {
let bin = {
// SAFETY: `bin` is a `BinEntry<K, V>`
let ptr = link.cast::<BinEntry<K, V>>();
// SAFETY: `retire` guarantees that we
Expand Down