-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
chore(trie): remove PrefixSetMut::contains
#10466
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,65 +75,35 @@ pub struct TriePrefixSets { | |
/// ``` | ||
/// use reth_trie::{prefix_set::PrefixSetMut, Nibbles}; | ||
/// | ||
/// let mut prefix_set = PrefixSetMut::default(); | ||
/// prefix_set.insert(Nibbles::from_nibbles_unchecked(&[0xa, 0xb])); | ||
/// prefix_set.insert(Nibbles::from_nibbles_unchecked(&[0xa, 0xb, 0xc])); | ||
/// let mut prefix_set_mut = PrefixSetMut::default(); | ||
/// prefix_set_mut.insert(Nibbles::from_nibbles_unchecked(&[0xa, 0xb])); | ||
/// prefix_set_mut.insert(Nibbles::from_nibbles_unchecked(&[0xa, 0xb, 0xc])); | ||
/// let mut prefix_set = prefix_set_mut.freeze(); | ||
/// assert!(prefix_set.contains(&[0xa, 0xb])); | ||
/// assert!(prefix_set.contains(&[0xa, 0xb, 0xc])); | ||
/// ``` | ||
#[derive(Clone, Default, Debug)] | ||
pub struct PrefixSetMut { | ||
keys: Vec<Nibbles>, | ||
sorted: bool, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should always be treated as unsorted |
||
index: usize, | ||
} | ||
|
||
impl<I> From<I> for PrefixSetMut | ||
where | ||
I: IntoIterator<Item = Nibbles>, | ||
{ | ||
fn from(value: I) -> Self { | ||
Self { keys: value.into_iter().collect(), ..Default::default() } | ||
Self { keys: value.into_iter().collect() } | ||
} | ||
} | ||
|
||
impl PrefixSetMut { | ||
/// Create [`PrefixSetMut`] with pre-allocated capacity. | ||
pub fn with_capacity(capacity: usize) -> Self { | ||
Self { keys: Vec::with_capacity(capacity), ..Default::default() } | ||
} | ||
|
||
/// Returns `true` if any of the keys in the set has the given prefix or | ||
/// if the given prefix is a prefix of any key in the set. | ||
pub fn contains(&mut self, prefix: &[u8]) -> bool { | ||
if !self.sorted { | ||
self.keys.sort(); | ||
self.keys.dedup(); | ||
self.sorted = true; | ||
} | ||
|
||
while self.index > 0 && self.keys[self.index] > *prefix { | ||
self.index -= 1; | ||
} | ||
|
||
for (idx, key) in self.keys[self.index..].iter().enumerate() { | ||
if key.has_prefix(prefix) { | ||
self.index += idx; | ||
return true | ||
} | ||
|
||
if *key > *prefix { | ||
self.index += idx; | ||
return false | ||
} | ||
} | ||
|
||
false | ||
Self { keys: Vec::with_capacity(capacity) } | ||
} | ||
|
||
/// Inserts the given `nibbles` into the set. | ||
pub fn insert(&mut self, nibbles: Nibbles) { | ||
self.sorted = false; | ||
self.keys.push(nibbles); | ||
} | ||
|
||
|
@@ -142,7 +112,6 @@ impl PrefixSetMut { | |
where | ||
I: IntoIterator<Item = Nibbles>, | ||
{ | ||
self.sorted = false; | ||
self.keys.extend(nibbles_iter); | ||
} | ||
|
||
|
@@ -160,15 +129,12 @@ impl PrefixSetMut { | |
/// | ||
/// If not yet sorted, the elements will be sorted and deduplicated. | ||
pub fn freeze(mut self) -> PrefixSet { | ||
if !self.sorted { | ||
self.keys.sort(); | ||
self.keys.dedup(); | ||
} | ||
|
||
self.keys.sort(); | ||
self.keys.dedup(); | ||
// we need to shrink in both the sorted and non-sorted cases because deduping may have | ||
// occurred either on `freeze`, or during `contains`. | ||
self.keys.shrink_to_fit(); | ||
PrefixSet { keys: Arc::new(self.keys), index: self.index } | ||
PrefixSet { keys: Arc::new(self.keys), index: 0 } | ||
} | ||
} | ||
|
||
|
@@ -185,7 +151,7 @@ impl PrefixSet { | |
/// Returns `true` if any of the keys in the set has the given prefix or | ||
/// if the given prefix is a prefix of any key in the set. | ||
#[inline] | ||
pub fn contains(&mut self, prefix: &Nibbles) -> bool { | ||
pub fn contains(&mut self, prefix: &[u8]) -> bool { | ||
while self.index > 0 && &self.keys[self.index] > prefix { | ||
self.index -= 1; | ||
} | ||
|
@@ -235,12 +201,13 @@ mod tests { | |
|
||
#[test] | ||
fn test_contains_with_multiple_inserts_and_duplicates() { | ||
let mut prefix_set = PrefixSetMut::default(); | ||
prefix_set.insert(Nibbles::from_nibbles([1, 2, 3])); | ||
prefix_set.insert(Nibbles::from_nibbles([1, 2, 4])); | ||
prefix_set.insert(Nibbles::from_nibbles([4, 5, 6])); | ||
prefix_set.insert(Nibbles::from_nibbles([1, 2, 3])); // Duplicate | ||
let mut prefix_set_mut = PrefixSetMut::default(); | ||
prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 3])); | ||
prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 4])); | ||
prefix_set_mut.insert(Nibbles::from_nibbles([4, 5, 6])); | ||
prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 3])); // Duplicate | ||
|
||
let mut prefix_set = prefix_set_mut.freeze(); | ||
assert!(prefix_set.contains(&[1, 2])); | ||
assert!(prefix_set.contains(&[4, 5])); | ||
assert!(!prefix_set.contains(&[7, 8])); | ||
|
@@ -249,40 +216,40 @@ mod tests { | |
|
||
#[test] | ||
fn test_freeze_shrinks_capacity() { | ||
let mut prefix_set = PrefixSetMut::default(); | ||
prefix_set.insert(Nibbles::from_nibbles([1, 2, 3])); | ||
prefix_set.insert(Nibbles::from_nibbles([1, 2, 4])); | ||
prefix_set.insert(Nibbles::from_nibbles([4, 5, 6])); | ||
prefix_set.insert(Nibbles::from_nibbles([1, 2, 3])); // Duplicate | ||
let mut prefix_set_mut = PrefixSetMut::default(); | ||
prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 3])); | ||
prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 4])); | ||
prefix_set_mut.insert(Nibbles::from_nibbles([4, 5, 6])); | ||
prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 3])); // Duplicate | ||
|
||
assert_eq!(prefix_set_mut.keys.len(), 4); // Length should be 3 (including duplicate) | ||
assert_eq!(prefix_set_mut.keys.capacity(), 4); // Capacity should be 4 (including duplicate) | ||
|
||
let mut prefix_set = prefix_set_mut.freeze(); | ||
assert!(prefix_set.contains(&[1, 2])); | ||
assert!(prefix_set.contains(&[4, 5])); | ||
assert!(!prefix_set.contains(&[7, 8])); | ||
assert_eq!(prefix_set.keys.len(), 3); // Length should be 3 (excluding duplicate) | ||
assert_eq!(prefix_set.keys.capacity(), 4); // Capacity should be 4 (including duplicate) | ||
|
||
let frozen = prefix_set.freeze(); | ||
assert_eq!(frozen.keys.len(), 3); // Length should be 3 (excluding duplicate) | ||
assert_eq!(frozen.keys.capacity(), 3); // Capacity should be 3 after shrinking | ||
assert_eq!(prefix_set.keys.capacity(), 3); // Capacity should be 3 after shrinking | ||
} | ||
|
||
#[test] | ||
fn test_freeze_shrinks_existing_capacity() { | ||
// do the above test but with preallocated capacity | ||
let mut prefix_set = PrefixSetMut::with_capacity(101); | ||
prefix_set.insert(Nibbles::from_nibbles([1, 2, 3])); | ||
prefix_set.insert(Nibbles::from_nibbles([1, 2, 4])); | ||
prefix_set.insert(Nibbles::from_nibbles([4, 5, 6])); | ||
prefix_set.insert(Nibbles::from_nibbles([1, 2, 3])); // Duplicate | ||
let mut prefix_set_mut = PrefixSetMut::with_capacity(101); | ||
prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 3])); | ||
prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 4])); | ||
prefix_set_mut.insert(Nibbles::from_nibbles([4, 5, 6])); | ||
prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 3])); // Duplicate | ||
|
||
assert_eq!(prefix_set_mut.keys.len(), 4); // Length should be 3 (including duplicate) | ||
assert_eq!(prefix_set_mut.keys.capacity(), 101); // Capacity should be 101 (including duplicate) | ||
|
||
let mut prefix_set = prefix_set_mut.freeze(); | ||
assert!(prefix_set.contains(&[1, 2])); | ||
assert!(prefix_set.contains(&[4, 5])); | ||
assert!(!prefix_set.contains(&[7, 8])); | ||
assert_eq!(prefix_set.keys.len(), 3); // Length should be 3 (excluding duplicate) | ||
assert_eq!(prefix_set.keys.capacity(), 101); // Capacity should be 101 (including duplicate) | ||
|
||
let frozen = prefix_set.freeze(); | ||
assert_eq!(frozen.keys.len(), 3); // Length should be 3 (excluding duplicate) | ||
assert_eq!(frozen.keys.capacity(), 3); // Capacity should be 3 after shrinking | ||
assert_eq!(prefix_set.keys.capacity(), 3); // Capacity should be 3 after shrinking | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs docs