Skip to content

Commit

Permalink
parallel mmap dense tree creation
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKitsune committed Mar 12, 2024
1 parent bf7b2c2 commit 8da252c
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/lazy_merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,10 +915,20 @@ impl<H: Hasher> DenseMMapTree<H> {

let mut mmap = MmapMutWrapper::new_with_initial_values(path_buf, empty_leaf, storage_size)?;
mmap[first_leaf_index..(first_leaf_index + values.len())].clone_from_slice(values);
for i in (1..first_leaf_index).rev() {
let left = &mmap[2 * i];
let right = &mmap[2 * i + 1];
mmap[i] = H::hash_node(left, right);

// We iterate over mutable layers of the tree
for current_depth in (1..=depth).rev() {
let (top, child_layer) = mmap.split_at_mut(1 << current_depth);
let parent_layer = &mut top[(1 << (current_depth - 1))..];

parent_layer
.par_iter_mut()
.enumerate()
.for_each(|(i, value)| {
let left = &child_layer[2 * i];
let right = &child_layer[2 * i + 1];
*value = H::hash_node(left, right);
});
}

Ok(Self {
Expand Down

0 comments on commit 8da252c

Please sign in to comment.