From daef321d2fb00c1fd2a54fdea5138b7e44476f9c Mon Sep 17 00:00:00 2001 From: Davidson Souza Date: Mon, 29 Jul 2024 20:51:29 -0300 Subject: [PATCH] get_proof_positions: call sort less often 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. --- src/accumulator/util.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/accumulator/util.rs b/src/accumulator/util.rs index 4848248..b9a90b9 100644 --- a/src/accumulator/util.rs +++ b/src/accumulator/util.rs @@ -275,7 +275,7 @@ 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::>() .into_iter() @@ -283,11 +283,9 @@ pub fn get_proof_positions(targets: &[u64], num_leaves: u64, forest_rows: u8) -> 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); @@ -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