From edd01e590208ec1374bbd97a5e87c62a8fe43bc9 Mon Sep 17 00:00:00 2001 From: Kould <2435992353@qq.com> Date: Fri, 2 Feb 2024 15:02:47 +0800 Subject: [PATCH] perf: `merging_iter` remove excess key copies --- Cargo.toml | 2 +- src/kernel/lsm/iterator/merging_iter.rs | 26 ++++++++++++------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f1048dc..95058dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kip_db" -version = "0.1.2-alpha.25" +version = "0.1.2-alpha.25.fix1" edition = "2021" authors = ["Kould "] description = "轻量级、异步 基于LSM Leveled Compaction K-V数据库" diff --git a/src/kernel/lsm/iterator/merging_iter.rs b/src/kernel/lsm/iterator/merging_iter.rs index ab50488..cd74bc2 100644 --- a/src/kernel/lsm/iterator/merging_iter.rs +++ b/src/kernel/lsm/iterator/merging_iter.rs @@ -28,7 +28,7 @@ impl Ord for IterKey { } struct InnerIter { - map_buf: BTreeMap, + map_buf: BTreeMap>, pre_key: Option, } @@ -125,20 +125,20 @@ macro_rules! impl_try_next { ($func:ident, $vec_iter:ty) => { impl InnerIter { fn $func(&mut self, vec_iter: &mut [$vec_iter]) -> KernelResult> { - while let Some((IterKey { num, .. }, old_item)) = self.map_buf.pop_first() { + while let Some((IterKey { num, key }, value)) = self.map_buf.pop_first() { if let Some(item) = vec_iter[num].try_next()? { Self::buf_map_insert(&mut self.map_buf, num, item); } // 跳过重复元素 - if let Some(key) = &self.pre_key { - if key == &old_item.0 { + if let Some(pre_key) = &self.pre_key { + if pre_key == &key { continue; } } - self.pre_key = Some(old_item.0.clone()); + self.pre_key = Some(key.clone()); - return Ok(Some(old_item)); + return Ok(Some((key, value))); } Ok(None) @@ -158,14 +158,12 @@ impl_try_next!( impl InnerIter { #[allow(clippy::mutable_key_type)] - fn buf_map_insert(seek_map: &mut BTreeMap, num: usize, item: KeyValue) { - let _ = seek_map.insert( - IterKey { - num, - key: item.0.clone(), - }, - item, - ); + fn buf_map_insert( + seek_map: &mut BTreeMap>, + num: usize, + (key, value): KeyValue, + ) { + let _ = seek_map.insert(IterKey { num, key }, value); } }