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

perf: remove reallocations in compute hashes #22

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/cell/cell_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<'a> CellParts<'a> {
let references = self.references.as_ref();

// `hashes_len` is guaranteed to be in range 1..4
let mut hashes_len = level + 1;
let mut old_hashes_len = level + 1;

let (cell_type, computed_level_mask) = if unlikely(descriptor.is_exotic()) {
let Some(&first_byte) = self.data.first() else {
Expand All @@ -114,7 +114,7 @@ impl<'a> CellParts<'a> {
return Err(Error::InvalidCell);
}

hashes_len = 1;
old_hashes_len = 1;
(CellType::PrunedBranch, level_mask)
}
// 8 bits type, hash, depth
Expand Down Expand Up @@ -157,7 +157,20 @@ impl<'a> CellParts<'a> {
let level_offset = cell_type.is_merkle() as u8;
let is_pruned = cell_type.is_pruned_branch();

let mut hashes = Vec::<(HashBytes, u16)>::with_capacity(hashes_len);
// this calculation mimics the underlying loop to avoid reallocations
let mut hashes = {
let mut hashes_len = 0;
for lvl in 0..4 {
// Skip non-zero levels for pruned branches and insignificant hashes for other cells
if lvl != 0 && (is_pruned || !level_mask.contains(lvl)) {
continue;
}
hashes_len += 1;
}
assert_eq!(hashes_len, old_hashes_len);
Vec::<(HashBytes, u16)>::with_capacity(hashes_len)
};

for level in 0..4 {
// Skip non-zero levels for pruned branches and insignificant hashes for other cells
if level != 0 && (is_pruned || !level_mask.contains(level)) {
Expand Down
Loading