Skip to content

Commit

Permalink
Bump ci rust toolchain (#18)
Browse files Browse the repository at this point in the history
* bump ci rust toolchain

* fix clippy::bool_assert_comparison

* fix clippy::default_numeric_fallback

* fix clippy::separated_literal_suffix

* fix clippy::redundant_else

* fix clippy::equatable_if_let

* fix clippy::shadow_reuse
  • Loading branch information
Nugine authored Jun 8, 2022
1 parent 27f965b commit aba9f1d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on: [push, pull_request]
name: CI

env:
CI_RUST_TOOLCHAIN: 1.47.0
CI_RUST_TOOLCHAIN: 1.61.0

jobs:
check:
Expand Down
11 changes: 6 additions & 5 deletions examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use lockfree_cuckoohash::{pin, LockFreeCuckooHash};
use std::ops::Not;
use std::sync::Arc;

fn simple_read_write_example() {
Expand All @@ -16,8 +17,8 @@ fn simple_read_write_example() {
let key = 1;
let value = "value";
// The returned value indicates whether the map had the key before this insertion.
assert_eq!(map.insert(key, value), false);
assert_eq!(map.insert(key, "value2"), true);
assert!(map.insert(key, value).not());
assert!(map.insert(key, "value2"));
// If you want to get the replaced value, try `insert_with_guard`.
assert_eq!(map.insert_with_guard(key, value, &guard), Some(&"value2"));

Expand All @@ -27,9 +28,9 @@ fn simple_read_write_example() {

// Remove a key-value pair.
// `remove` returns `false` if the map does not have the key.
assert_eq!(map.remove(&2), false);
assert_eq!(map.remove(&key), true);
assert_eq!(map.remove(&key), false);
assert!(map.remove(&2).not());
assert!(map.remove(&key));
assert!(map.remove(&key).not());

// If you want to get the removed value, use `remove_with_guard` instead.
map.insert(key, value);
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@
clippy::blanket_clippy_restriction_lints, // allow clippy::restriction
clippy::panic, // allow debug_assert, panic in production code
clippy::implicit_return, // actually omitting the return keyword is idiomatic Rust code
clippy::separated_literal_suffix, // conflicts with clippy::unseparated_literal_suffix
)]
#![allow(
clippy::undocumented_unsafe_blocks, // FIXME
clippy::missing_panics_doc, // FIXME
clippy::single_char_lifetime_names, // FIXME
)]

/// `pointer` defines atomic pointers which will be used for lockfree operations.
mod pointer;

Expand Down
64 changes: 33 additions & 31 deletions src/map_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,22 @@ where
K: std::fmt::Debug,
V: std::fmt::Debug,
{
// This is not thread-safe.
// FIXME: This is not thread-safe.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let capacity = self.tables[0].len();
let guard = pin();
let mut f = f.debug_map();
let mut debug = f.debug_map();
for tbl_idx in 0..2 {
for slot_idx in 0..capacity {
let slot = self.tables[tbl_idx][slot_idx].load(Ordering::SeqCst, &guard);
unsafe {
if let Some(kv) = slot.as_raw().as_ref() {
f.entry(&kv.key, &kv.value);
debug.entry(&kv.key, &kv.value);
}
}
}
}
f.finish()
debug.finish()
}
}

Expand All @@ -172,12 +172,12 @@ where
/// `with_capacity` creates a new `MapInner` with specified capacity.
pub fn with_capacity(capacity: usize, hash_builders: [RandomState; 2]) -> Self {
let single_table_capacity = match capacity.checked_add(1) {
Some(capacity) => capacity.overflow_div(2),
Some(cap) => cap.overflow_div(2),
None => capacity.overflow_div(2),
};
let mut tables = Vec::with_capacity(2);

for _ in 0..2 {
for _ in 0_u32..2 {
let mut table = Vec::with_capacity(single_table_capacity);
for _ in 0..single_table_capacity {
table.push(AtomicPtr::null());
Expand Down Expand Up @@ -274,11 +274,11 @@ where
let slot_idx1 = self.get_index(1, new_key);
loop {
let find_result = self.find(new_key, slot_idx0, slot_idx1, outer_map, guard);
let (tbl_idx, slot0, slot1) = match find_result {
let (tbl_idx_opt, slot0, slot1) = match find_result {
Some(r) => (r.tbl_idx, r.slot0, r.slot1),
None => return InsertResult::Retry,
};
let (slot_idx, target_slot, key_exist) = match tbl_idx {
let (slot_idx_opt, target_slot, key_exist) = match tbl_idx_opt {
Some(tbl_idx) => {
// The key has already been in the table, we need to replace the value.
if tbl_idx == 0 {
Expand All @@ -302,7 +302,7 @@ where

let mut need_relocate = false;

if let Some(slot_idx) = slot_idx {
if let Some(slot_idx) = slot_idx_opt {
// We found the key exists or we have an empty slot,
// just replace the slot with the new one.

Expand Down Expand Up @@ -435,11 +435,11 @@ where
let new_slot = SharedPtr::null();
loop {
let find_result = self.find(key, slot_idx0, slot_idx1, outer_map, guard);
let (tbl_idx, slot0, slot1) = match find_result {
let (tbl_idx_opt, slot0, slot1) = match find_result {
Some(r) => (r.tbl_idx, r.slot0, r.slot1),
None => return RemoveResult::Retry,
};
let tbl_idx = match tbl_idx {
let tbl_idx = match tbl_idx_opt {
Some(idx) => idx,
None => return RemoveResult::Succ(None), // The key does not exist.
};
Expand All @@ -458,7 +458,8 @@ where
}
Err(_) => continue,
}
} else {
}
{
if self.tables[0][slot_idx0.slot_idx]
.load(Ordering::SeqCst, guard)
.as_raw()
Expand Down Expand Up @@ -595,14 +596,14 @@ where
match state0 {
SlotState::Reloc => {
// The slot is being relocated, we help this relocation.
if self.help_relocate(slot_idx0, false, true, outer_map, guard) {
return if self.help_relocate(slot_idx0, false, true, outer_map, guard) {
// The relocation succeeds, we set the second returned value as `true`.
return (result, true, false);
(result, true, false)
} else {
// The relocation fails, because the table has been resized.
// We set the third returned value as `true`.
return (result, false, true);
}
(result, false, true)
};
}
SlotState::Copied => {
// The slot is being copied or has copied to the new map, so we try
Expand All @@ -627,11 +628,11 @@ where
let (_, entry1, state1) = Self::unwrap_slot(slot1);
match state1 {
SlotState::Reloc => {
if self.help_relocate(slot_idx1, false, true, outer_map, guard) {
return (result, true, false);
return if self.help_relocate(slot_idx1, false, true, outer_map, guard) {
(result, true, false)
} else {
return (result, false, true);
}
(result, false, true)
};
}
SlotState::Copied => {
self.help_resize(outer_map, guard);
Expand Down Expand Up @@ -719,7 +720,7 @@ where
let dst_idx = self.get_index(1_usize.overflow_sub(src_idx.tbl_idx), src_key);
let dst_slot = self.get_slot(dst_idx, guard);
let (dst_count, dst_entry, dst_state) = Self::unwrap_slot(dst_slot);
if let SlotState::Copied = dst_state {
if dst_state == SlotState::Copied {
let new_slot_without_mark = src_slot.with_lower_u2(SlotState::NullOrKey.into_u8());
self.tables[src_idx.tbl_idx][src_idx.slot_idx]
.compare_and_set(src_slot, new_slot_without_mark, Ordering::SeqCst, guard)
Expand Down Expand Up @@ -840,11 +841,12 @@ where
let copied_num = self.copied_num.load(Ordering::SeqCst);
if copied_num == capacity {
// try to promote the new map
let current_map = SharedPtr::from_raw(self);
let new_map = self.new_map.load(Ordering::SeqCst, guard);
if let Ok(current_map) =
let result = {
let current_map = SharedPtr::from_raw(self);
let new_map = self.new_map.load(Ordering::SeqCst, guard);
outer_map.compare_and_set(current_map, new_map, Ordering::SeqCst, guard)
{
};
if let Ok(current_map) = result {
unsafe {
guard.defer_unchecked(move || {
drop(current_map.into_box());
Expand Down Expand Up @@ -945,20 +947,20 @@ where
}
slot = self.get_slot(slot_idx, guard);
}
let (_, entry, state) = Self::unwrap_slot(slot);
if let SlotState::Copied = state {
let (_, entry_opt, state) = Self::unwrap_slot(slot);
if state == SlotState::Copied {
self.help_resize(outer_map, guard);
return RelocateResult::Resized;
}
if let Some(entry) = entry {
if let Some(entry) = entry_opt {
let key = &entry.key;

// If there are duplicated keys in both slots, we may
// meet an endless loop. So we must do the dedup here.
let next_slot_idx = self.get_index(1_usize.overflow_sub(slot_idx.tbl_idx), key);
let next_slot = self.get_slot(next_slot_idx, guard);
let (_, next_entry, next_state) = Self::unwrap_slot(next_slot);
if let SlotState::Copied = next_state {
if next_state == SlotState::Copied {
self.help_resize(outer_map, guard);
return RelocateResult::Resized;
}
Expand Down Expand Up @@ -1000,15 +1002,15 @@ where
src_slot = self.get_slot(src_idx, guard);
}
let (_, entry, state) = Self::unwrap_slot(src_slot);
if let SlotState::Copied = state {
if state == SlotState::Copied {
self.help_resize(outer_map, guard);
return RelocateResult::Resized;
}
if let Some(pair) = entry {
let dst_idx =
self.get_index(1_usize.overflow_sub(src_idx.tbl_idx), &pair.key);
let (_, dst_entry, dst_state) = self.get_entry(dst_idx, guard);
if let SlotState::Copied = dst_state {
if dst_state == SlotState::Copied {
self.help_resize(outer_map, guard);
return RelocateResult::Resized;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn test_multi_thread() {
let rnd_idx: usize = rng.gen_range(0..warmup_entries.len());
let warmup_entry = &warmup_entries[rnd_idx];
let res = cuckoo_map.get(&warmup_entry.0, &guard);
assert_eq!(res.is_some(), true);
assert!(res.is_some());
assert_eq!(*res.unwrap(), warmup_entry.1);
}
let insert_pair = &new_insert_entries[entry_idx];
Expand Down

0 comments on commit aba9f1d

Please sign in to comment.