-
Notifications
You must be signed in to change notification settings - Fork 10
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
Implement Nibbles to reduce memory allocations #207
Conversation
This commit implements Nibbles and implements them for insert and proofs only. See the docs inline for details of what Nibbles are and how they are used.
self.split( | ||
node, | ||
&mut parents, | ||
&chunked_key[key_nib_offset..], | ||
&rem_path, |
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.
There's a more efficient way to do this, but it requires changes in split
. Maybe you can add a todo and we can get rid of the allocation for rem_path
.
In any case, it is better since you aren't allocating key_nib_offet
bytes.
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.
I think there are a LOT of improvements possible here, not sure that this one is worse than some of the others I already know about.
firewood/src/merkle.rs
Outdated
|| !remaining_path | ||
.iter() | ||
.take(n_path.len()) | ||
.eq(n_path.iter().cloned()) |
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.
I find these multi-line-if clauses super cumbersome to read; think we could pop this out into a variable (with a name that explains why we are going this too)?
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.
I split it into two if statements; I think it's a bit easier to read now.
firewood/src/nibbles.rs
Outdated
|
||
static NIBBLES: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; | ||
|
||
/// Nibbles is a newtype that contains only a pointer to a u8, and produces |
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.
/// Nibbles is a newtype that contains only a pointer to a u8, and produces | |
/// Nibbles is a newtype that contains only a reference to a `[u8]`, and produces |
firewood/src/nibbles.rs
Outdated
pub struct Nibbles<'a, const LEADING_ZEROES: usize>(pub &'a [u8]); | ||
impl<'a, const LEADING_ZEROES: usize> Index<usize> for Nibbles<'a, LEADING_ZEROES> { |
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.
pub struct Nibbles<'a, const LEADING_ZEROES: usize>(pub &'a [u8]); | |
impl<'a, const LEADING_ZEROES: usize> Index<usize> for Nibbles<'a, LEADING_ZEROES> { | |
pub struct Nibbles<'a, const LEADING_ZEROES: usize>(pub &'a [u8]); | |
impl<'a, const LEADING_ZEROES: usize> Index<usize> for Nibbles<'a, LEADING_ZEROES> { |
#[must_use] | ||
pub fn len(&self) -> usize { | ||
LEADING_ZEROES + 2 * self.0.len() | ||
} |
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.
👍
This might be useful for improving that split
function and taking in Nibbles
instead of &[u8]
- Added additional comments to complex if statement - doc: pointer -> reference - and more whitespace
This commit implements Nibbles and implements them for insert and proofs only.
See the docs inline for details of what Nibbles are and how they are used.