Skip to content

Commit f4a2f8d

Browse files
authored
refactor(storage): sstable iter compare fullkey struct instead of encoded key to avoid memory allocation (risingwavelabs#8607)
1 parent ab701c7 commit f4a2f8d

File tree

4 files changed

+15
-37
lines changed

4 files changed

+15
-37
lines changed

src/storage/src/hummock/compactor/iterator.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,12 @@ impl HummockIterator for ConcatSstableIterator {
388388
/// Resets the iterator and seeks to the first position where the stored key >= `key`.
389389
fn seek<'a>(&'a mut self, key: FullKey<&'a [u8]>) -> Self::SeekFuture<'a> {
390390
async move {
391-
let encoded_key = key.encode();
392-
let key_slice = encoded_key.as_slice();
393-
let seek_key: &[u8] = if self.key_range.left.is_empty() {
394-
key_slice
391+
let seek_key = if self.key_range.left.is_empty() {
392+
key
395393
} else {
396-
match KeyComparator::compare_encoded_full_key(key_slice, &self.key_range.left) {
397-
Ordering::Less | Ordering::Equal => &self.key_range.left,
398-
Ordering::Greater => key_slice,
394+
match key.cmp(&FullKey::decode(&self.key_range.left)) {
395+
Ordering::Less | Ordering::Equal => FullKey::decode(&self.key_range.left),
396+
Ordering::Greater => key,
399397
}
400398
};
401399
let table_idx = self.tables.partition_point(|table| {
@@ -406,7 +404,7 @@ impl HummockIterator for ConcatSstableIterator {
406404
// Note that we need to use `<` instead of `<=` to ensure that all keys in an SST
407405
// (including its max. key) produce the same search result.
408406
let max_sst_key = &table.key_range.as_ref().unwrap().right;
409-
KeyComparator::compare_encoded_full_key(max_sst_key, seek_key) == Ordering::Less
407+
FullKey::decode(max_sst_key).cmp(&seek_key) == Ordering::Less
410408
});
411409

412410
self.seek_idx(table_idx, Some(key)).await
@@ -424,7 +422,6 @@ mod tests {
424422

425423
use risingwave_hummock_sdk::key::{next_full_key, prev_full_key, FullKey};
426424
use risingwave_hummock_sdk::key_range::KeyRange;
427-
use risingwave_hummock_sdk::KeyComparator;
428425

429426
use crate::hummock::compactor::ConcatSstableIterator;
430427
use crate::hummock::iterator::test_utils::mock_sstable_store;
@@ -597,11 +594,9 @@ mod tests {
597594
let mut iter =
598595
ConcatSstableIterator::new(table_infos.clone(), kr.clone(), sstable_store.clone());
599596
// Use block_2_smallest_key as seek key and result in invalid iterator.
600-
let seek_key = block_2_smallest_key.clone();
601-
assert!(KeyComparator::compare_encoded_full_key(&seek_key, &kr.right) == Ordering::Greater);
602-
iter.seek_idx(0, Some(FullKey::decode(&seek_key)))
603-
.await
604-
.unwrap();
597+
let seek_key = FullKey::decode(&block_2_smallest_key);
598+
assert!(seek_key.cmp(&FullKey::decode(&kr.right)) == Ordering::Greater);
599+
iter.seek_idx(0, Some(seek_key)).await.unwrap();
605600
assert!(!iter.is_valid());
606601
// Use a small enough seek key and result in the second KV of block 1.
607602
let seek_key = test_key_of(0).encode();

src/storage/src/hummock/iterator/concat_inner.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::sync::Arc;
1919
use itertools::Itertools;
2020
use risingwave_common::must_match;
2121
use risingwave_hummock_sdk::key::FullKey;
22-
use risingwave_hummock_sdk::KeyComparator;
2322
use risingwave_pb::hummock::SstableInfo;
2423

2524
use crate::hummock::iterator::{DirectionEnum, HummockIterator, HummockIteratorDirection};
@@ -195,22 +194,17 @@ impl<TI: SstableIteratorType> HummockIterator for ConcatIteratorInner<TI> {
195194

196195
fn seek<'a>(&'a mut self, key: FullKey<&'a [u8]>) -> Self::SeekFuture<'a> {
197196
async move {
198-
let encoded_key = key.encode();
199197
let table_idx = self
200198
.tables
201199
.partition_point(|table| match Self::Direction::direction() {
202200
DirectionEnum::Forward => {
203-
let ord = KeyComparator::compare_encoded_full_key(
204-
table.smallest_key(),
205-
&encoded_key[..],
206-
);
201+
let ord = FullKey::decode(table.smallest_key()).cmp(&key);
202+
207203
ord == Less || ord == Equal
208204
}
209205
DirectionEnum::Backward => {
210-
let ord = KeyComparator::compare_encoded_full_key(
211-
table.largest_key(),
212-
&encoded_key[..],
213-
);
206+
let ord = FullKey::decode(table.largest_key()).cmp(&key);
207+
214208
ord == Greater || ord == Equal
215209
}
216210
})

src/storage/src/hummock/sstable/backward_sstable_iterator.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use std::future::Future;
1717
use std::sync::Arc;
1818

1919
use risingwave_hummock_sdk::key::FullKey;
20-
use risingwave_hummock_sdk::KeyComparator;
2120

2221
use crate::hummock::iterator::{Backward, HummockIterator};
2322
use crate::hummock::sstable::SstableIteratorReadOptions;
@@ -132,8 +131,6 @@ impl HummockIterator for BackwardSstableIterator {
132131

133132
fn seek<'a>(&'a mut self, key: FullKey<&'a [u8]>) -> Self::SeekFuture<'a> {
134133
async move {
135-
let encoded_key = key.encode();
136-
let encoded_key_slice = encoded_key.as_slice();
137134
let block_idx = self
138135
.sst
139136
.value()
@@ -143,10 +140,7 @@ impl HummockIterator for BackwardSstableIterator {
143140
// Compare by version comparator
144141
// Note: we are comparing against the `smallest_key` of the `block`, thus the
145142
// partition point should be `prev(<=)` instead of `<`.
146-
let ord = KeyComparator::compare_encoded_full_key(
147-
block_meta.smallest_key.as_slice(),
148-
encoded_key_slice,
149-
);
143+
let ord = FullKey::decode(&block_meta.smallest_key).cmp(&key);
150144
ord == Less || ord == Equal
151145
})
152146
.saturating_sub(1); // considering the boundary of 0

src/storage/src/hummock/sstable/forward_sstable_iterator.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::ops::Bound::*;
1919
use std::sync::Arc;
2020

2121
use risingwave_hummock_sdk::key::FullKey;
22-
use risingwave_hummock_sdk::KeyComparator;
2322

2423
use super::super::{HummockResult, HummockValue};
2524
use super::Sstable;
@@ -301,7 +300,6 @@ impl HummockIterator for SstableIterator {
301300

302301
fn seek<'a>(&'a mut self, key: FullKey<&'a [u8]>) -> Self::SeekFuture<'a> {
303302
async move {
304-
let encoded_key = key.encode();
305303
let block_idx = self
306304
.sst
307305
.value()
@@ -311,10 +309,7 @@ impl HummockIterator for SstableIterator {
311309
// compare by version comparator
312310
// Note: we are comparing against the `smallest_key` of the `block`, thus the
313311
// partition point should be `prev(<=)` instead of `<`.
314-
let ord = KeyComparator::compare_encoded_full_key(
315-
block_meta.smallest_key.as_slice(),
316-
encoded_key.as_slice(),
317-
);
312+
let ord = FullKey::decode(&block_meta.smallest_key).cmp(&key);
318313
ord == Less || ord == Equal
319314
})
320315
.saturating_sub(1); // considering the boundary of 0

0 commit comments

Comments
 (0)