From a828b2ee65cf18f6876e1fda61d7d8a0cb5f48da Mon Sep 17 00:00:00 2001 From: Joshua Oladele Date: Fri, 23 Feb 2024 10:05:27 +0100 Subject: [PATCH] Optimize chain reorg and node functions --- chaindexing/src/chain_reorg.rs | 16 ++++++++-------- chaindexing/src/nodes.rs | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/chaindexing/src/chain_reorg.rs b/chaindexing/src/chain_reorg.rs index e80f361..6450719 100644 --- a/chaindexing/src/chain_reorg.rs +++ b/chaindexing/src/chain_reorg.rs @@ -61,32 +61,32 @@ impl UnsavedReorgedBlock { pub struct ReorgedBlocks; impl ReorgedBlocks { - pub fn only_earliest_per_chain(reorged_blocks: &[ReorgedBlock]) -> Vec { + pub fn only_earliest_per_chain<'a>( + reorged_blocks: &'a [ReorgedBlock], + ) -> Vec<&'a ReorgedBlock> { reorged_blocks .iter() .fold( - HashMap::::new(), + HashMap::::new(), |mut reorged_blocks_by_chain, reorged_block| { let ReorgedBlock { chain_id, .. } = reorged_block; if let Some(earliest_reorged_block) = reorged_blocks_by_chain.get(chain_id) { if reorged_block.block_number < earliest_reorged_block.block_number { - reorged_blocks_by_chain.insert(*chain_id, reorged_block.clone()); + reorged_blocks_by_chain.insert(*chain_id, reorged_block); } } else { - reorged_blocks_by_chain - .insert(reorged_block.chain_id, reorged_block.clone()); + reorged_blocks_by_chain.insert(reorged_block.chain_id, reorged_block); } reorged_blocks_by_chain }, ) - .values() - .cloned() + .into_values() .collect() } - pub fn get_ids(reorged_blocks: &[ReorgedBlock]) -> Vec { + pub fn get_ids<'a>(reorged_blocks: &'a [&'a ReorgedBlock]) -> Vec { reorged_blocks.iter().map(|r| r.id).collect() } } diff --git a/chaindexing/src/nodes.rs b/chaindexing/src/nodes.rs index e9dcaca..6429601 100644 --- a/chaindexing/src/nodes.rs +++ b/chaindexing/src/nodes.rs @@ -22,7 +22,7 @@ impl Node { } } -pub fn elect_leader(nodes: &Vec) -> Node { +pub fn elect_leader<'a>(nodes: &'a Vec) -> &'a Node { let mut nodes_iter = nodes.iter(); let mut leader: Option<&Node> = nodes_iter.next(); @@ -32,5 +32,5 @@ pub fn elect_leader(nodes: &Vec) -> Node { } } - leader.unwrap().clone() + leader.unwrap() }