Update hash for slice.len() == 1

This commit is contained in:
Nicolas Patry 2024-09-14 15:06:03 +02:00
parent 5fa332156f
commit 6769e45711
No known key found for this signature in database
GPG Key ID: 64AF4752B2967863
2 changed files with 20 additions and 9 deletions

6
Cargo.lock generated
View File

@ -1839,6 +1839,12 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "indoc"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5"
[[package]] [[package]]
name = "init-tracing-opentelemetry" name = "init-tracing-opentelemetry"
version = "0.14.1" version = "0.14.1"

View File

@ -6,11 +6,16 @@ use std::{
sync::Arc, sync::Arc,
}; };
fn hash_(slice: &[u32]) -> u64 { fn hash(slice: &[u32]) -> u64 {
assert!(!slice.is_empty());
if slice.len() == 1 {
slice[0] as u64
} else {
let mut s = std::hash::DefaultHasher::new(); let mut s = std::hash::DefaultHasher::new();
slice.hash(&mut s); slice.hash(&mut s);
s.finish() s.finish()
} }
}
pub struct RadixAllocator { pub struct RadixAllocator {
allocation_id: u64, allocation_id: u64,
@ -282,7 +287,7 @@ impl RadixTrie {
let node = &self.nodes[node_id]; let node = &self.nodes[node_id];
if key.len() >= self.block_size { if key.len() >= self.block_size {
let node_key = hash_(&key[..self.block_size]); let node_key = hash(&key[..self.block_size]);
if let Some(&child_id) = node.children.get(&node_key) { if let Some(&child_id) = node.children.get(&node_key) {
self.update_access_time(child_id); self.update_access_time(child_id);
let child = self.nodes.get(child_id).expect("Invalid child identifier"); let child = self.nodes.get(child_id).expect("Invalid child identifier");
@ -423,7 +428,7 @@ impl RadixTrie {
assert_eq!(tokens.len(), blocks.len() * self.block_size); assert_eq!(tokens.len(), blocks.len() * self.block_size);
let node_key = hash_(&tokens[..self.block_size]); let node_key = hash(&tokens[..self.block_size]);
if let Some(&child_id) = self.nodes[node_id].children.get(&node_key) { if let Some(&child_id) = self.nodes[node_id].children.get(&node_key) {
self.update_access_time(child_id); self.update_access_time(child_id);
let child = self let child = self
@ -480,7 +485,7 @@ impl RadixTrie {
std::mem::swap(&mut node.key, &mut parent_key); std::mem::swap(&mut node.key, &mut parent_key);
std::mem::swap(&mut node.blocks, &mut parent_blocks); std::mem::swap(&mut node.blocks, &mut parent_blocks);
let node_key = hash_(&node.key[..self.block_size]); let node_key = hash(&node.key[..self.block_size]);
let grandparent_id = node.parent.expect("Node does not have a parent"); let grandparent_id = node.parent.expect("Node does not have a parent");
let parent_id = self.add_node(grandparent_id, parent_key, parent_blocks); let parent_id = self.add_node(grandparent_id, parent_key, parent_blocks);
@ -505,7 +510,7 @@ impl RadixTrie {
) -> NodeId { ) -> NodeId {
let key = key.into(); let key = key.into();
let blocks = blocks.into(); let blocks = blocks.into();
let first = hash_(&key[..self.block_size]); let first = hash(&key[..self.block_size]);
let child = TrieNode::new(key, blocks, self.time, Some(parent_id)); let child = TrieNode::new(key, blocks, self.time, Some(parent_id));
let child_id = self.nodes.insert(child); let child_id = self.nodes.insert(child);
@ -539,7 +544,7 @@ impl RadixTrie {
let parent_id = node.parent.expect("Attempted to remove root node"); let parent_id = node.parent.expect("Attempted to remove root node");
let parent = self.nodes.get_mut(parent_id).expect("Unknown parent node"); let parent = self.nodes.get_mut(parent_id).expect("Unknown parent node");
let node_key = hash_(&node.key[..self.block_size]); let node_key = hash(&node.key[..self.block_size]);
parent.children.remove(&node_key); parent.children.remove(&node_key);
self.decref(parent_id) self.decref(parent_id)
.expect("Failed to decrease parent refcount"); .expect("Failed to decrease parent refcount");