From 46094d794c7df8765c38e61f71136f7f9c2764b1 Mon Sep 17 00:00:00 2001 From: Michael Sutton Date: Thu, 1 Sep 2022 14:21:58 +0300 Subject: [PATCH] Some slight performance improvements to block window (#30) * Use with_capacity since the window will almost always reach exactly this size * fmt * Rename + use parent gd data and save a call to the store --- .../src/processes/dagtraversalmanager.rs | 64 ++++++++++--------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/consensus/src/processes/dagtraversalmanager.rs b/consensus/src/processes/dagtraversalmanager.rs index 918aa21ec..face806ff 100644 --- a/consensus/src/processes/dagtraversalmanager.rs +++ b/consensus/src/processes/dagtraversalmanager.rs @@ -36,9 +36,8 @@ impl DagTraversalManager, window_size: usize) -> BlockWindowHeap { - let mut window_heap = SizedUpBlockHeap::new(self.ghostdag_store.clone(), window_size); if window_size == 0 { - return window_heap.binary_heap; + return BlockWindowHeap::new(); } let mut current_gd = high_ghostdag_data; @@ -53,50 +52,59 @@ impl DagTraversalManager, ghostdag_data: &GhostdagData) -> bool { + fn try_push_mergeset( + &self, heap: &mut BoundedSizeBlockHeap, ghostdag_data: &GhostdagData, selected_parent_blue_work: BlueWorkType, + ) -> bool { // If the window is full and the selected parent is less than the minimum then we break // because this means that there cannot be any more blocks in the past with higher blue work - if !heap.try_push(ghostdag_data.selected_parent) { + if !heap.try_push(ghostdag_data.selected_parent, selected_parent_blue_work) { return true; } for block in ghostdag_data.descending_mergeset_without_selected_parent(self.ghostdag_store.deref()) { // If it's smaller than minimum then we won't be able to add the rest because we iterate in descending blue work order. - if !heap.try_push_with_blue_work(block.hash, block.blue_work) { + if !heap.try_push(block.hash, block.blue_work) { break; } } @@ -105,27 +113,21 @@ impl DagTraversalManager { +struct BoundedSizeBlockHeap { binary_heap: BlockWindowHeap, - ghostdag_store: Arc, size: usize, } -impl SizedUpBlockHeap { - fn new(ghostdag_store: Arc, size: usize) -> Self { - Self::from_binary_heap(ghostdag_store, size, BinaryHeap::new()) - } - - fn from_binary_heap(ghostdag_store: Arc, size: usize, binary_heap: BlockWindowHeap) -> Self { - Self { ghostdag_store, size, binary_heap } +impl BoundedSizeBlockHeap { + fn new(size: usize) -> Self { + Self::from_binary_heap(size, BinaryHeap::with_capacity(size)) } - fn try_push(&mut self, hash: Hash) -> bool { - let blue_work = self.ghostdag_store.get_blue_work(hash).unwrap(); - self.try_push_with_blue_work(hash, blue_work) + fn from_binary_heap(size: usize, binary_heap: BlockWindowHeap) -> Self { + Self { size, binary_heap } } - fn try_push_with_blue_work(&mut self, hash: Hash, blue_work: BlueWorkType) -> bool { + fn try_push(&mut self, hash: Hash, blue_work: BlueWorkType) -> bool { let r_sortable_block = Reverse(SortableBlock { hash, blue_work }); if self.binary_heap.len() == self.size { if let Some(max) = self.binary_heap.peek() {