Skip to content
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
merged 1 commit into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions src/stream/src/executor/top_n/topn_cache_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Copy link
Member

@xxchan xxchan Jan 15, 2024

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:

  1. Are there any other places that the size is not correctly maintained?
  2. Why this kv_heap_size stuff is in TopNCacheState? It looks like a general BTreeMapWithEstimateSize or sth.

Copy link
Contributor Author

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.

&'a mut self,
mut pred: F1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why mut here? And no need to change to F1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just learn form @BugenZhao, FnMut::call_mut accept &mut self. Calling pred will mutate itself.

) -> 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);
}
}

Expand Down Expand Up @@ -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()
}
}
Loading