From 661b3fdfedfcf4fc5b67e53a902333faf3de35f4 Mon Sep 17 00:00:00 2001 From: Arya Date: Wed, 24 Apr 2024 13:36:44 -0400 Subject: [PATCH] Adds a comment about avoiding duplicate ncts and avoids spawning rayon scope if there are no notes --- zebra-chain/src/parallel/tree.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/zebra-chain/src/parallel/tree.rs b/zebra-chain/src/parallel/tree.rs index e194411ea..50183cddd 100644 --- a/zebra-chain/src/parallel/tree.rs +++ b/zebra-chain/src/parallel/tree.rs @@ -79,8 +79,19 @@ impl NoteCommitmentTrees { let mut sapling_result = None; let mut orchard_result = None; + // Note: Only updating the note commitment trees when there are more notes that + // need to be appended to the tree prevents unnecessarily duplicating note commitment + // tree data in the non-finalized state. + let has_sprout_notes = !sprout_note_commitments.is_empty(); + let has_sapling_notes = !sapling_note_commitments.is_empty(); + let has_orchard_notes = !orchard_note_commitments.is_empty(); + + if !(has_sprout_notes || has_sapling_notes || has_orchard_notes) { + return Ok(()); + } + rayon::in_place_scope_fifo(|scope| { - if !sprout_note_commitments.is_empty() { + if has_sprout_notes { scope.spawn_fifo(|_scope| { sprout_result = Some(Self::update_sprout_note_commitment_tree( sprout, @@ -89,7 +100,7 @@ impl NoteCommitmentTrees { }); } - if !sapling_note_commitments.is_empty() { + if has_sapling_notes { scope.spawn_fifo(|_scope| { sapling_result = Some(Self::update_sapling_note_commitment_tree( sapling, @@ -98,7 +109,7 @@ impl NoteCommitmentTrees { }); } - if !orchard_note_commitments.is_empty() { + if has_orchard_notes { scope.spawn_fifo(|_scope| { orchard_result = Some(Self::update_orchard_note_commitment_tree( orchard,