Skip to content

Commit e80f21e

Browse files
committed
perf: try to improve drain_key_min perf
needs benchmarking
1 parent 38f3648 commit e80f21e

File tree

1 file changed

+16
-31
lines changed

1 file changed

+16
-31
lines changed

src/mvcc_stream.rs

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,18 @@ use double_ended_peekable::{DoubleEndedPeekable, DoubleEndedPeekableExt};
1111
#[allow(clippy::module_name_repetitions)]
1212
pub struct MvccStream<I: DoubleEndedIterator<Item = crate::Result<InternalValue>>> {
1313
inner: DoubleEndedPeekable<I>,
14+
15+
lo_last_seen_key: Option<UserKey>,
1416
}
1517

1618
impl<I: DoubleEndedIterator<Item = crate::Result<InternalValue>>> MvccStream<I> {
1719
/// Initializes a new merge iterator
1820
#[must_use]
1921
pub fn new(iter: I) -> Self {
2022
let iter = iter.double_ended_peekable();
21-
Self { inner: iter }
22-
}
23-
24-
fn drain_key_min(&mut self, key: &UserKey) -> crate::Result<()> {
25-
loop {
26-
let Some(next) = self.inner.peek() else {
27-
return Ok(());
28-
};
29-
30-
let Ok(next) = next else {
31-
// NOTE: We just asserted, the peeked value is an error
32-
#[allow(clippy::expect_used)]
33-
return Err(self
34-
.inner
35-
.next()
36-
.expect("should exist")
37-
.expect_err("should be error"));
38-
};
39-
40-
// Consume version
41-
if next.key.user_key == key {
42-
// NOTE: We know the next value is not empty, because we just peeked it
43-
#[allow(clippy::expect_used)]
44-
self.inner.next().expect("should not be empty")?;
45-
} else {
46-
return Ok(());
47-
}
23+
Self {
24+
inner: iter,
25+
lo_last_seen_key: None,
4826
}
4927
}
5028
}
@@ -53,12 +31,19 @@ impl<I: DoubleEndedIterator<Item = crate::Result<InternalValue>>> Iterator for M
5331
type Item = crate::Result<InternalValue>;
5432

5533
fn next(&mut self) -> Option<Self::Item> {
56-
let head = fail_iter!(self.inner.next()?);
34+
loop {
35+
let head = fail_iter!(self.inner.next()?);
5736

58-
// As long as items are the same key, ignore them
59-
fail_iter!(self.drain_key_min(&head.key.user_key));
37+
if let Some(last_seen_key) = &self.lo_last_seen_key {
38+
if head.key.user_key == last_seen_key {
39+
continue;
40+
}
41+
}
42+
43+
self.lo_last_seen_key = Some(head.key.user_key.clone());
6044

61-
Some(Ok(head))
45+
return Some(Ok(head));
46+
}
6247
}
6348
}
6449

0 commit comments

Comments
 (0)