Skip to content
This repository has been archived by the owner on Aug 4, 2024. It is now read-only.

Commit

Permalink
perf: merging_iter remove excess key copies
Browse files Browse the repository at this point in the history
  • Loading branch information
KKould committed Feb 2, 2024
1 parent 797f6aa commit edd01e5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
description = "轻量级、异步 基于LSM Leveled Compaction K-V数据库"
Expand Down
26 changes: 12 additions & 14 deletions src/kernel/lsm/iterator/merging_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Ord for IterKey {
}

struct InnerIter {
map_buf: BTreeMap<IterKey, KeyValue>,
map_buf: BTreeMap<IterKey, Option<Bytes>>,
pre_key: Option<Bytes>,
}

Expand Down Expand Up @@ -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<Option<KeyValue>> {
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)
Expand All @@ -158,14 +158,12 @@ impl_try_next!(

impl InnerIter {
#[allow(clippy::mutable_key_type)]
fn buf_map_insert(seek_map: &mut BTreeMap<IterKey, KeyValue>, num: usize, item: KeyValue) {
let _ = seek_map.insert(
IterKey {
num,
key: item.0.clone(),
},
item,
);
fn buf_map_insert(
seek_map: &mut BTreeMap<IterKey, Option<Bytes>>,
num: usize,
(key, value): KeyValue,
) {
let _ = seek_map.insert(IterKey { num, key }, value);
}
}

Expand Down

0 comments on commit edd01e5

Please sign in to comment.