Skip to content
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

Merged
merged 4 commits into from
Aug 21, 2023
Merged

Implement Nibbles to reduce memory allocations #207

merged 4 commits into from
Aug 21, 2023

Conversation

rkuris
Copy link
Collaborator

@rkuris rkuris commented Aug 21, 2023

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.

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.
firewood/src/merkle.rs Show resolved Hide resolved
self.split(
node,
&mut parents,
&chunked_key[key_nib_offset..],
&rem_path,
Copy link
Contributor

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.

Copy link
Collaborator Author

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.

Comment on lines 1878 to 1881
|| !remaining_path
.iter()
.take(n_path.len())
.eq(n_path.iter().cloned())
Copy link
Contributor

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)?

Copy link
Collaborator Author

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.


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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

Comment on lines 34 to 35
pub struct Nibbles<'a, const LEADING_ZEROES: usize>(pub &'a [u8]);
impl<'a, const LEADING_ZEROES: usize> Index<usize> for Nibbles<'a, LEADING_ZEROES> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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()
}
Copy link
Contributor

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
@rkuris rkuris merged commit 906034d into main Aug 21, 2023
5 checks passed
@rkuris rkuris deleted the rkuris/nibbles branch August 21, 2023 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants