From e43911ad2e5fbc555137fe45fb841f66905d87b0 Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Thu, 14 Sep 2023 11:54:57 +0530 Subject: [PATCH 1/2] fix(rln): if only a single insertion, dont use batch_insert --- rln/src/pm_tree_adapter.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rln/src/pm_tree_adapter.rs b/rln/src/pm_tree_adapter.rs index 6db5f4d..e41c6fa 100644 --- a/rln/src/pm_tree_adapter.rs +++ b/rln/src/pm_tree_adapter.rs @@ -178,6 +178,13 @@ impl ZerokitMerkleTree for PmTree { start: usize, values: I, ) -> Result<()> { + let values = values.into_iter().collect::>(); + if values.len() == 1 { + return self + .tree + .set(start, values[0]) + .map_err(|e| Report::msg(e.to_string())); + } self.tree .set_range(start, values) .map_err(|e| Report::msg(e.to_string())) From 9b703680d1d0f3f9d13bbfa2f9024abcd3d2a30f Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Thu, 14 Sep 2023 12:25:29 +0530 Subject: [PATCH 2/2] fix: more optimizations --- rln/src/pm_tree_adapter.rs | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/rln/src/pm_tree_adapter.rs b/rln/src/pm_tree_adapter.rs index e41c6fa..c42ce47 100644 --- a/rln/src/pm_tree_adapter.rs +++ b/rln/src/pm_tree_adapter.rs @@ -178,13 +178,6 @@ impl ZerokitMerkleTree for PmTree { start: usize, values: I, ) -> Result<()> { - let values = values.into_iter().collect::>(); - if values.len() == 1 { - return self - .tree - .set(start, values[0]) - .map_err(|e| Report::msg(e.to_string())); - } self.tree .set_range(start, values) .map_err(|e| Report::msg(e.to_string())) @@ -204,11 +197,13 @@ impl ZerokitMerkleTree for PmTree { let mut indices = indices.into_iter().collect::>(); indices.sort(); - match (leaves.is_empty(), indices.is_empty()) { - (true, true) => Err(Report::msg("no leaves or indices to be removed")), - (false, true) => self.set_range_with_leaves(start, leaves), - (true, false) => self.remove_indices(indices), - (false, false) => self.remove_indices_and_set_leaves(start, leaves, indices), + match (leaves.len(), indices.len()) { + (0, 0) => Err(Report::msg("no leaves or indices to be removed")), + (1, 0) => self.set(start, leaves[0]), + (0, 1) => self.delete(indices[0]), + (_, 0) => self.set_range_with_leaves(start, leaves), + (0, _) => self.remove_indices(&indices), + (_, _) => self.remove_indices_and_set_leaves(start, leaves, &indices), } } @@ -271,18 +266,11 @@ impl PmTree { .map_err(|e| Report::msg(e.to_string())) } - fn remove_indices(&mut self, indices: Vec) -> Result<()> { + fn remove_indices(&mut self, indices: &[usize]) -> Result<()> { let start = indices[0]; let end = indices.last().unwrap() + 1; - let mut new_leaves: Vec<_> = (start..end) - .map(|i| self.tree.get(i)) - .collect::>()?; - - new_leaves - .iter_mut() - .take(indices.len()) - .for_each(|leaf| *leaf = PmTreeHasher::default_leaf()); + let new_leaves = (start..end).map(|_| PmTreeHasher::default_leaf()); self.tree .set_range(start, new_leaves) @@ -293,16 +281,13 @@ impl PmTree { &mut self, start: usize, leaves: Vec, - indices: Vec, + indices: &[usize], ) -> Result<()> { let min_index = *indices.first().unwrap(); let max_index = start + leaves.len(); - // Generated a placeholder with the exact size needed, - // Initiated with default values to be overridden throughout the method let mut set_values = vec![PmTreeHasher::default_leaf(); max_index - min_index]; - // If the index is not in indices list, keep the original value for i in min_index..start { if !indices.contains(&i) { let value = self.tree.get(i)?; @@ -310,7 +295,6 @@ impl PmTree { } } - // Insert new leaves after 'start' position for (i, &leaf) in leaves.iter().enumerate() { set_values[start - min_index + i] = leaf; }