Skip to content

Commit

Permalink
get_proof_positions: call sort less often
Browse files Browse the repository at this point in the history
Every call to sort takes a big chunk of CPU time as the vectors can get
really long, and as std's merge sort is unstable. We aren't supposed to
get the vector unsorted while working inside a row, so we can call sort
only after finishing a row.
  • Loading branch information
Davidson-Souza committed Jul 30, 2024
1 parent 67d806e commit daef321
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/accumulator/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,19 +275,17 @@ pub fn get_proof_positions(targets: &[u64], num_leaves: u64, forest_rows: u8) ->
for row in 0..=forest_rows {
let mut row_targets = computed_positions
.iter()
.copied()
.cloned()
.filter(|x| super::util::detect_row(*x, forest_rows) == row)
.collect::<Vec<_>>()
.into_iter()
.peekable();

while let Some(node) = row_targets.next() {
if is_root_position(node, num_leaves, forest_rows) {
let idx = computed_positions.iter().position(|x| node == *x).unwrap();

computed_positions.remove(idx);
continue;
}

if let Some(next) = row_targets.peek() {
if !is_sibling(node, *next) {
proof_positions.push(node ^ 1);
Expand All @@ -299,8 +297,9 @@ pub fn get_proof_positions(targets: &[u64], num_leaves: u64, forest_rows: u8) ->
}

computed_positions.push(parent(node, forest_rows));
computed_positions.sort();
}

computed_positions.sort();
}

proof_positions
Expand Down

0 comments on commit daef321

Please sign in to comment.