-
Notifications
You must be signed in to change notification settings - Fork 591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: estimate size for topn cache extract_if #14564
Merged
+38
−6
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,18 +108,25 @@ impl TopNCacheState { | |
self.inner.range(range) | ||
} | ||
|
||
pub fn extract_if<F>(&mut self, pred: F) -> ExtractIf<'_, CacheKey, CompactedRow, F, Global> | ||
pub fn extract_if<'a, F1>( | ||
&'a mut self, | ||
mut pred: F1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just learn form @BugenZhao, FnMut::call_mut accept &mut self. Calling |
||
) -> TopNExtractIf<'a, impl FnMut(&CacheKey, &mut CompactedRow) -> bool> | ||
where | ||
F: FnMut(&CacheKey, &mut CompactedRow) -> bool, | ||
F1: 'a + FnMut(&CacheKey, &CompactedRow) -> bool, | ||
{ | ||
self.inner.extract_if(pred) | ||
let pred_immut = move |key: &CacheKey, value: &mut CompactedRow| pred(key, value); | ||
TopNExtractIf { | ||
inner: self.inner.extract_if(pred_immut), | ||
kv_heap_size: &mut self.kv_heap_size, | ||
} | ||
} | ||
|
||
pub fn retain<F>(&mut self, f: F) | ||
pub fn retain<F>(&mut self, mut f: F) | ||
where | ||
F: FnMut(&CacheKey, &mut CompactedRow) -> bool, | ||
F: FnMut(&CacheKey, &CompactedRow) -> bool, | ||
{ | ||
self.inner.retain(f) | ||
self.extract_if(|k, v| !f(k, v)).for_each(drop); | ||
} | ||
} | ||
|
||
|
@@ -153,3 +160,28 @@ impl fmt::Debug for TopNCacheState { | |
self.inner.fmt(f) | ||
} | ||
} | ||
|
||
pub struct TopNExtractIf<'a, F> | ||
where | ||
F: FnMut(&CacheKey, &mut CompactedRow) -> bool, | ||
{ | ||
inner: ExtractIf<'a, CacheKey, CompactedRow, F, Global>, | ||
kv_heap_size: &'a mut KvSize, | ||
} | ||
|
||
impl<'a, F> Iterator for TopNExtractIf<'a, F> | ||
where | ||
F: 'a + FnMut(&CacheKey, &mut CompactedRow) -> bool, | ||
{ | ||
type Item = (CacheKey, CompactedRow); | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.inner | ||
.next() | ||
.inspect(|(k, v)| self.kv_heap_size.sub(k, v)) | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.inner.size_hint() | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change makes me think:
kv_heap_size
stuff is inTopNCacheState
? It looks like a generalBTreeMapWithEstimateSize
or sth.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some special usage of an estimated structure. For example https://github.com/singularity-data/risingwave/blob/9cd7f6486e659af72857aef7ac27247305e880c0/src/stream/src/executor/managed_state/join/join_entry_state.rs#L75. It can be hard to provide an EstimatedBtreeMap with an unified semantic.