perf: Optimize hashing, null-free fast path for percentile_cont, median - #23954
Merged
Conversation
The non-distinct PercentileContAccumulator and MedianAccumulator used the default SipHash hasher for their internal HashMaps. This is slow; switching to foldhash is significantly faster. Also, add a null-free fast path to `update_batch` and `retract_batch` in both accumulators. Benchmarks: percentile_cont no_nulls window=256 -61.1% (249.0 -> 96.6 us) percentile_cont with_nulls window=256 -59.4% (232.7 -> 94.5 us) percentile_cont no_nulls window=4096 -50.9% (756.3 -> 370.4 us) percentile_cont with_nulls window=4096 -48.2% (552.2 -> 286.2 us) percentile_cont no_nulls window=16384 -47.3% (2.311 -> 1.218 ms) percentile_cont with_nulls window=16384 -42.0% (1.465 -> 0.851 ms) median no_nulls window=256 -62.8% (247.0 -> 92.0 us) median with_nulls window=256 -59.8% (228.8 -> 92.0 us) median no_nulls window=4096 -40.0% (411.4 -> 246.6 us) median with_nulls window=4096 -39.4% (364.4 -> 221.0 us) median no_nulls window=16384 -31.5% (1.107 -> 0.759 ms) median with_nulls window=16384 -32.9% (0.947 -> 0.637 ms)
If `retract_batch` is asked to remove values the accumulator is not tracking, its state has diverged from the window frame and continuing would silently produce wrong results; return an internal error rather than silently ignoring.
Contributor
Author
|
cc @viirya |
viirya
approved these changes
Jul 28, 2026
viirya
left a comment
Member
There was a problem hiding this comment.
LGTM — nice to see the #23946 ideas carried over to the non-distinct accumulators, and the benchmark wins are substantial.
I went through all three changes:
- foldhash on the
to_removemap — matches what we did for the distinct path. - null-free fast path:
update_batchusesextend_from_slice(values.values())when there are no nulls, which is correct sincenull_count() == 0guarantees every slot in the values buffer is a valid logical value (equivalent toiter().flatten()), and it's the wholesale append that drives the speedup.retract_batch's null-free branch mirrors it. - retract error: checking
!to_remove.is_empty()after the removal loop is the right adaptation for the non-distinct multiset — any leftover entries are values that were retracted but never present, which is exactly the divergence worth surfacing rather than silently dropping. Consistent with theinternal_err!we added on the distinct side.
Test coverage is good: the error-path test and the dense-vs-sparse update_batch equivalence test cover both new branches. LGTM.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #23954 +/- ##
==========================================
+ Coverage 80.69% 80.75% +0.05%
==========================================
Files 1095 1096 +1
Lines 372529 373511 +982
Branches 372529 373511 +982
==========================================
+ Hits 300626 301627 +1001
+ Misses 53942 53895 -47
- Partials 17961 17989 +28 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Jul 28, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Which issue does this PR close?
percentile_cont,median#23953Rationale for this change
This PR makes three improvements to non-distinct
PercentileContAccumulatorandMedianAccumulator, inspired by recent work onpercentile_cont(DISTINCT)(#23946):update_batchandretract_batchretract_batch, rather than silently ignoring it.Benchmarks:
What changes are included in this PR?
See above.
Are these changes tested?
Yes: existing tests pass, new tests added for the null-free fast path and the improved error handling.
Are there any user-facing changes?
No.