From acf76d5fc8fa3aa5a0f267e80a4a49eeb08c5d1c Mon Sep 17 00:00:00 2001 From: nk_ysg Date: Sun, 30 Mar 2025 17:29:14 +0800 Subject: [PATCH] use hash_map::entry get and update --- qed-doge-data-link/src/block_cache.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/qed-doge-data-link/src/block_cache.rs b/qed-doge-data-link/src/block_cache.rs index 76f17dd..9a9d00b 100644 --- a/qed-doge-data-link/src/block_cache.rs +++ b/qed-doge-data-link/src/block_cache.rs @@ -24,7 +24,7 @@ substantial portions of the software: with contributions from Carter Feldman (https://x.com/cmpeq)." */ -use std::collections::HashMap; +use std::collections::{HashMap, hash_map::Entry}; use doge_light_client::core_data::QDogeBlock; use serde::{Deserialize, Serialize}; @@ -52,12 +52,16 @@ impl BlockFetcher { } } pub fn get_block(&mut self, height: u32) -> anyhow::Result { - if let Some(block) = self.store.get(&height) { - return Ok(block.clone()); + match self.store.entry(height) { + Entry::Occupied(entry) => { + return Ok(entry.get().clone()); + } + Entry::Vacant(entry) => { + let block = self.client.get_qdoge_block(height as u32)?; + entry.insert(block.clone()); + return Ok(block); + } } - let block = self.client.get_qdoge_block(height as u32)?; - self.store.insert(height, block.clone()); - Ok(block) } pub fn get_blocks(&mut self, heights: &[u32]) -> anyhow::Result> {