Fix a bunch of linter warnings

This commit is contained in:
Erik Grinaker
2025-06-30 11:10:02 +02:00
parent 9d9e3cd08a
commit 67b04f8ab3
28 changed files with 228 additions and 404 deletions

View File

@@ -121,7 +121,7 @@ where
}
HashMapInit {
shmem_handle: shmem_handle,
shmem_handle,
shared_ptr,
}
}
@@ -152,7 +152,7 @@ where
let mut success = None;
self.update_with_fn(key, |existing| {
if let Some(_) = existing {
if existing.is_some() {
success = Some(false);
UpdateAction::Nothing
} else {
@@ -294,7 +294,7 @@ where
bucket_ptr.write(core::Bucket {
hash: 0,
next: if i < num_buckets {
i as u32 + 1
i + 1
} else {
inner.free_head
},
@@ -317,8 +317,8 @@ where
buckets = std::slice::from_raw_parts_mut(buckets_ptr, num_buckets as usize);
dictionary = std::slice::from_raw_parts_mut(dictionary_ptr, dictionary_size);
}
for i in 0..dictionary.len() {
dictionary[i] = core::INVALID_POS;
for item in dictionary.iter_mut() {
*item = core::INVALID_POS;
}
for i in 0..old_num_buckets as usize {

View File

@@ -90,8 +90,8 @@ where
let dictionary =
unsafe { std::slice::from_raw_parts_mut(dictionary_ptr, dictionary_size as usize) };
for i in 0..dictionary.len() {
dictionary[i].write(INVALID_POS);
for item in dictionary.iter_mut() {
item.write(INVALID_POS);
}
// TODO: use std::slice::assume_init_mut() once it stabilizes
unsafe {
@@ -121,7 +121,7 @@ where
let bucket = &self.buckets[next as usize];
let (bucket_key, bucket_value) = bucket.inner.as_ref().expect("entry is in use");
if bucket_key == key {
return Some(&bucket_value);
return Some(bucket_value);
}
next = bucket.next;
}
@@ -228,6 +228,6 @@ where
bucket.next = INVALID_POS;
bucket.inner = Some((key, value));
return Ok(pos);
Ok(pos)
}
}

View File

@@ -81,7 +81,7 @@ fn sparse() {
for _ in 0..10000 {
loop {
let key = rand::random::<u128>();
if used_keys.get(&key).is_some() {
if used_keys.contains(&key) {
continue;
}
used_keys.insert(key);

View File

@@ -163,7 +163,7 @@ fn next_recurse<'e, V: Value>(
) -> Result<Option<&'e V>, ConcurrentUpdateError> {
let rnode = node.read_lock_or_restart()?;
let prefix = rnode.get_prefix();
if prefix.len() != 0 {
if !prefix.is_empty() {
path.extend_from_slice(prefix);
}
@@ -213,13 +213,14 @@ fn next_recurse<'e, V: Value>(
}
// This corresponds to the 'insertOpt' function in the paper
pub(crate) fn update_recurse<'e, 'g, K: Key, V: Value, A: ArtAllocator<V>, F>(
#[allow(clippy::too_many_arguments)]
pub(crate) fn update_recurse<'e, K: Key, V: Value, A: ArtAllocator<V>, F>(
key: &[u8],
value_fn: F,
node: NodeRef<'e, V>,
rparent: Option<(ReadLockedNodeRef<V>, u8)>,
rgrandparent: Option<(ReadLockedNodeRef<V>, u8)>,
guard: &'g mut TreeWriteGuard<'e, K, V, A>,
guard: &'_ mut TreeWriteGuard<'e, K, V, A>,
level: usize,
orig_key: &[u8],
) -> Result<(), ArtError>
@@ -248,8 +249,8 @@ where
return Ok(());
}
let prefix_match_len = prefix_match_len.unwrap();
let key = &key[prefix_match_len as usize..];
let level = level + prefix_match_len as usize;
let key = &key[prefix_match_len..];
let level = level + prefix_match_len;
if rnode.is_leaf() {
assert_eq!(key.len(), 0);
@@ -321,7 +322,7 @@ where
};
wnode.write_unlock();
}
return Ok(());
Ok(())
} else {
let next_child = next_node.unwrap(); // checked above it's not None
if let Some((ref rparent, _)) = rparent {
@@ -357,14 +358,14 @@ impl std::fmt::Debug for PathElement {
}
}
pub(crate) fn dump_tree<'e, V: Value + std::fmt::Debug>(
pub(crate) fn dump_tree<V: Value + std::fmt::Debug>(
root: RootPtr<V>,
epoch_pin: &'e EpochPin,
epoch_pin: &'_ EpochPin,
dst: &mut dyn std::io::Write,
) {
let root_ref = NodeRef::from_root_ptr(root);
let _ = dump_recurse(&[], root_ref, &epoch_pin, 0, dst);
let _ = dump_recurse(&[], root_ref, epoch_pin, 0, dst);
}
// TODO: return an Err if writeln!() returns error, instead of unwrapping
@@ -380,7 +381,7 @@ fn dump_recurse<'e, V: Value + std::fmt::Debug>(
let rnode = node.read_lock_or_restart()?;
let mut path = Vec::from(path);
let prefix = rnode.get_prefix();
if prefix.len() != 0 {
if !prefix.is_empty() {
path.push(PathElement::Prefix(Vec::from(prefix)));
}
@@ -426,13 +427,13 @@ fn dump_recurse<'e, V: Value + std::fmt::Debug>(
/// [foo]b -> [a]r -> value
/// e -> [ls]e -> value
///```
fn insert_split_prefix<'e, K: Key, V: Value, A: ArtAllocator<V>>(
fn insert_split_prefix<K: Key, V: Value, A: ArtAllocator<V>>(
key: &[u8],
value: V,
node: &mut WriteLockedNodeRef<V>,
parent: &mut WriteLockedNodeRef<V>,
parent_key: u8,
guard: &'e TreeWriteGuard<K, V, A>,
guard: &'_ TreeWriteGuard<K, V, A>,
) -> Result<(), OutOfMemoryError> {
let old_node = node;
let old_prefix = old_node.get_prefix();
@@ -463,11 +464,11 @@ fn insert_split_prefix<'e, K: Key, V: Value, A: ArtAllocator<V>>(
Ok(())
}
fn insert_to_node<'e, K: Key, V: Value, A: ArtAllocator<V>>(
fn insert_to_node<K: Key, V: Value, A: ArtAllocator<V>>(
wnode: &mut WriteLockedNodeRef<V>,
key: &[u8],
value: V,
guard: &'e TreeWriteGuard<K, V, A>,
guard: &'_ TreeWriteGuard<K, V, A>,
) -> Result<(), OutOfMemoryError> {
let value_child = allocate_node_for_value(&key[1..], value, guard.tree_writer.allocator)?;
wnode.insert_child(key[0], value_child.into_ptr());

View File

@@ -105,13 +105,13 @@ impl AtomicLockAndVersion {
}
fn set_locked_bit(version: u64) -> u64 {
return version + 2;
version + 2
}
fn is_obsolete(version: u64) -> bool {
return (version & 1) == 1;
(version & 1) == 1
}
fn is_locked(version: u64) -> bool {
return (version & 2) == 2;
(version & 2) == 2
}

View File

@@ -305,14 +305,13 @@ impl<V: Value> NodePtr<V> {
&self,
allocator: &impl ArtAllocator<V>,
) -> Result<NodePtr<V>, OutOfMemoryError> {
let bigger = match self.variant() {
match self.variant() {
NodeVariant::Internal4(n) => n.grow(allocator),
NodeVariant::Internal16(n) => n.grow(allocator),
NodeVariant::Internal48(n) => n.grow(allocator),
NodeVariant::Internal256(_) => panic!("cannot grow Internal256 node"),
NodeVariant::Leaf(_) => panic!("cannot grow Leaf node"),
};
bigger
}
}
pub(crate) fn insert_child(&mut self, key_byte: u8, child: NodePtr<V>) {
@@ -464,7 +463,7 @@ impl<V: Value> NodeInternal4<V> {
new.extend_from_slice(prefix);
new.push(prefix_byte);
new.extend_from_slice(&self.prefix[0..self.prefix_len as usize]);
(&mut self.prefix[0..new.len()]).copy_from_slice(&new);
self.prefix[0..new.len()].copy_from_slice(&new);
self.prefix_len = new.len() as u8;
}
@@ -558,7 +557,7 @@ impl<V: Value> NodeInternal4<V> {
tag: NodeTag::Internal16,
lock_and_version: AtomicLockAndVersion::new(),
prefix: self.prefix.clone(),
prefix: self.prefix,
prefix_len: self.prefix_len,
num_children: self.num_children,
@@ -585,7 +584,7 @@ impl<V: Value> NodeInternal16<V> {
new.extend_from_slice(prefix);
new.push(prefix_byte);
new.extend_from_slice(&self.prefix[0..self.prefix_len as usize]);
(&mut self.prefix[0..new.len()]).copy_from_slice(&new);
self.prefix[0..new.len()].copy_from_slice(&new);
self.prefix_len = new.len() as u8;
}
@@ -679,7 +678,7 @@ impl<V: Value> NodeInternal16<V> {
tag: NodeTag::Internal48,
lock_and_version: AtomicLockAndVersion::new(),
prefix: self.prefix.clone(),
prefix: self.prefix,
prefix_len: self.prefix_len,
num_children: self.num_children,
@@ -706,7 +705,7 @@ impl<V: Value> NodeInternal16<V> {
tag: NodeTag::Internal4,
lock_and_version: AtomicLockAndVersion::new(),
prefix: self.prefix.clone(),
prefix: self.prefix,
prefix_len: self.prefix_len,
num_children: self.num_children,
@@ -736,7 +735,7 @@ impl<V: Value> NodeInternal48<V> {
idx,
self.num_children
);
assert!(shadow_indexes.get(&idx).is_none());
assert!(!shadow_indexes.contains(&idx));
shadow_indexes.insert(idx);
count += 1;
}
@@ -750,7 +749,7 @@ impl<V: Value> NodeInternal48<V> {
new.extend_from_slice(prefix);
new.push(prefix_byte);
new.extend_from_slice(&self.prefix[0..self.prefix_len as usize]);
(&mut self.prefix[0..new.len()]).copy_from_slice(&new);
self.prefix[0..new.len()].copy_from_slice(&new);
self.prefix_len = new.len() as u8;
}
@@ -853,7 +852,7 @@ impl<V: Value> NodeInternal48<V> {
tag: NodeTag::Internal256,
lock_and_version: AtomicLockAndVersion::new(),
prefix: self.prefix.clone(),
prefix: self.prefix,
prefix_len: self.prefix_len,
num_children: self.num_children as u16,
@@ -879,7 +878,7 @@ impl<V: Value> NodeInternal48<V> {
tag: NodeTag::Internal16,
lock_and_version: AtomicLockAndVersion::new(),
prefix: self.prefix.clone(),
prefix: self.prefix,
prefix_len: self.prefix_len,
num_children: self.num_children,
@@ -912,7 +911,7 @@ impl<V: Value> NodeInternal256<V> {
new.extend_from_slice(prefix);
new.push(prefix_byte);
new.extend_from_slice(&self.prefix[0..self.prefix_len as usize]);
(&mut self.prefix[0..new.len()]).copy_from_slice(&new);
self.prefix[0..new.len()].copy_from_slice(&new);
self.prefix_len = new.len() as u8;
}
@@ -987,7 +986,7 @@ impl<V: Value> NodeInternal256<V> {
tag: NodeTag::Internal48,
lock_and_version: AtomicLockAndVersion::new(),
prefix: self.prefix.clone(),
prefix: self.prefix,
prefix_len: self.prefix_len,
num_children: self.num_children as u8,
@@ -1019,7 +1018,7 @@ impl<V: Value> NodeLeaf<V> {
new.extend_from_slice(prefix);
new.push(prefix_byte);
new.extend_from_slice(&self.prefix[0..self.prefix_len as usize]);
(&mut self.prefix[0..new.len()]).copy_from_slice(&new);
self.prefix[0..new.len()].copy_from_slice(&new);
self.prefix_len = new.len() as u8;
}

View File

@@ -61,13 +61,11 @@ impl<'t, V: crate::Value> ArtMultiSlabAllocator<'t, V> {
let (allocator_area, remain) = alloc_from_slice::<ArtMultiSlabAllocator<V>>(area);
let (tree_area, remain) = alloc_from_slice::<Tree<V>>(remain);
let allocator = allocator_area.write(ArtMultiSlabAllocator {
allocator_area.write(ArtMultiSlabAllocator {
tree_area: spin::Mutex::new(Some(tree_area)),
inner: MultiSlabAllocator::new(remain, &Self::LAYOUTS),
phantom_val: PhantomData,
});
allocator
})
}
}

View File

@@ -119,7 +119,7 @@ impl<'t> BlockAllocator<'t> {
}
// out of blocks
return INVALID_BLOCK;
INVALID_BLOCK
}
// TODO: this is currently unused. The slab allocator never releases blocks

View File

@@ -374,11 +374,11 @@ mod tests {
assert!(unsafe { (*all[i]).val == i });
}
let distribution = Zipf::new(10 as f64, 1.1).unwrap();
let distribution = Zipf::new(10.0, 1.1).unwrap();
let mut rng = rand::rng();
for _ in 0..100000 {
slab.0.dump();
let idx = (rng.sample(distribution) as usize).into();
let idx = rng.sample(distribution) as usize;
let ptr: *mut TestObject = all[idx];
if !ptr.is_null() {
assert_eq!(unsafe { (*ptr).val }, idx);

View File

@@ -3,7 +3,6 @@
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use crossbeam_utils::CachePadded;
use spin;
const NUM_SLOTS: usize = 1000;
@@ -62,10 +61,8 @@ impl EpochShared {
pub(crate) fn advance(&self) -> u64 {
// Advance the global epoch
let old_epoch = self.global_epoch.fetch_add(2, Ordering::Relaxed);
let new_epoch = old_epoch + 2;
// Anyone that release their pin after this will update their slot.
new_epoch
old_epoch + 2
}
pub(crate) fn broadcast(&self) {
@@ -99,10 +96,8 @@ impl EpochShared {
let delta = now.wrapping_sub(this_epoch);
if delta > u64::MAX / 2 {
// this is very recent
} else {
if delta > now.wrapping_sub(oldest) {
oldest = this_epoch;
}
} else if delta > now.wrapping_sub(oldest) {
oldest = this_epoch;
}
}
oldest

View File

@@ -239,7 +239,7 @@ where
phantom_key: PhantomData<K>,
}
impl<'a, 't: 'a, K: Key, V: Value, A: ArtAllocator<V>> TreeInitStruct<'t, K, V, A> {
impl<'t, K: Key, V: Value, A: ArtAllocator<V>> TreeInitStruct<'t, K, V, A> {
pub fn new(allocator: &'t A) -> TreeInitStruct<'t, K, V, A> {
let tree_ptr = allocator.alloc_tree();
let tree_ptr = NonNull::new(tree_ptr).expect("out of memory");
@@ -295,7 +295,7 @@ impl<'t, K: Key, V: Value, A: ArtAllocator<V>> TreeWriteAccess<'t, K, V, A> {
pub fn start_read(&'t self) -> TreeReadGuard<'t, K, V> {
TreeReadGuard {
tree: &self.tree,
tree: self.tree,
epoch_pin: self.epoch_handle.pin(),
phantom_key: PhantomData,
}
@@ -305,7 +305,7 @@ impl<'t, K: Key, V: Value, A: ArtAllocator<V>> TreeWriteAccess<'t, K, V, A> {
impl<'t, K: Key, V: Value> TreeReadAccess<'t, K, V> {
pub fn start_read(&'t self) -> TreeReadGuard<'t, K, V> {
TreeReadGuard {
tree: &self.tree,
tree: self.tree,
epoch_pin: self.epoch_handle.pin(),
phantom_key: PhantomData,
}
@@ -360,7 +360,7 @@ impl<'e, K: Key, V: Value, A: ArtAllocator<V>> TreeWriteGuard<'e, K, V, A> {
let mut success = None;
self.update_with_fn(key, |existing| {
if let Some(_) = existing {
if existing.is_some() {
success = Some(false);
UpdateAction::Nothing
} else {
@@ -461,11 +461,9 @@ where
K: Key + for<'a> From<&'a [u8]>,
{
pub fn new_wrapping() -> TreeIterator<K> {
let mut next_key = Vec::new();
next_key.resize(K::KEY_LEN, 0);
TreeIterator {
done: false,
next_key,
next_key: vec![0; K::KEY_LEN],
max_key: None,
phantom_key: PhantomData,
}
@@ -495,11 +493,9 @@ where
let mut wrapped_around = false;
loop {
assert_eq!(self.next_key.len(), K::KEY_LEN);
if let Some((k, v)) = algorithm::iter_next(
&mut self.next_key,
read_guard.tree.root,
&read_guard.epoch_pin,
) {
if let Some((k, v)) =
algorithm::iter_next(&self.next_key, read_guard.tree.root, &read_guard.epoch_pin)
{
assert_eq!(k.len(), K::KEY_LEN);
assert_eq!(self.next_key.len(), K::KEY_LEN);

View File

@@ -102,7 +102,7 @@ fn sparse() {
for _ in 0..10000 {
loop {
let key = rand::random::<u128>();
if used_keys.get(&key).is_some() {
if used_keys.contains(&key) {
continue;
}
used_keys.insert(key);
@@ -182,7 +182,7 @@ fn test_iter<A: ArtAllocator<TestValue>>(
let mut iter = TreeIterator::new(&(TestKey::MIN..TestKey::MAX));
loop {
let shadow_item = shadow_iter.next().map(|(k, v)| (k.clone(), v.clone()));
let shadow_item = shadow_iter.next().map(|(k, v)| (*k, *v));
let r = tree.start_read();
let item = iter.next(&r);
@@ -194,8 +194,7 @@ fn test_iter<A: ArtAllocator<TestValue>>(
tree.start_read().dump(&mut std::io::stderr());
eprintln!("SHADOW:");
let mut si = shadow.iter();
while let Some(si) = si.next() {
for si in shadow {
eprintln!("key: {:?}, val: {}", si.0, si.1);
}
panic!(