Repository navigation
Conversation
…up window
`NegativeLimitBySortedStreamTransform::consume` sent every group, even one
that starts and ends inside the current chunk, through the sliding window:
`append_run` pushed a `ChunkSlice` into the empty window deque (allocating the
deque map and a block, and copying the chunk `shared_ptr`), `finalizeWindow`
copied it into `pending` (a second `shared_ptr` copy) and then `window = {}`
(libc++: `clear(); shrink_to_fit();`) freed the block and the map. So each
one-row group cost two memory-tracked allocations, two frees and two atomic
increment/decrement pairs, plus one `pending` entry.
The window is only needed for a group that touches a chunk boundary. A group
that ends inside the chunk while the window is empty also started inside this
chunk, so its output is known when its end is found: the last
`min(L, length + offset)` rows of the run without the trailing `offset` rows,
the same rows `append_run` + `dropExcessRows` + `dropOffsetRows` keep for a
one-slice window. Such groups are now staged into `pending` directly, and
`appendToPending` extends the last pending slice when the new rows continue
it in the same chunk, so the one-row groups of a chunk become one slice.
`generate()` produces the same chunk for a merged slice (same rows, first
start and last end). Groups touching a chunk boundary still use the window.
Evidence that this path is what moves the `negative_limit_by_in_order`
queries #0, ClickHouse#1 and ClickHouse#8 (millions of groups): ClickHouse#125000, which changed only the
allocator of these deques, moved them by -19/-16/-17% on master d3ee313 while
ClickHouse#2-ClickHouse#7 stayed flat; and the ClickHouse#125013 perf job's prewarm counters for #0 show
equal instructions on both sides (10301M vs 10333M, ~1030 per group) and +15%
cycles on one build.
Local A/B (release build, `CMAKE_BUILD_TYPE=None`, no ThinLTO, the perf
test's tables and queries, 12 rotating rounds, geomean of per-round ratios
new/base with bootstrap 95% CI):
#0 10M one-row groups 0.764 s -> 0.096 s 0.123 [0.113, 0.135]
ClickHouse#1 1M groups of 10 0.098 s -> 0.049 s 0.457 [0.413, 0.510]
ClickHouse#2 100k groups of 100 0.025 s -> 0.019 s 0.707 [0.642, 0.785]
ClickHouse#3-ClickHouse#7 100 groups 0.86-1.03, no CI lower bound above 1.0 (4-15 ms)
ClickHouse#8 5M two-key groups 0.447 s -> 0.116 s 0.250 [0.236, 0.265]
User-space instructions of 5 runs of #0: 60.3G -> 10.2G (~1190 -> ~190 per
group after subtracting 5 runs of ClickHouse#3), cycles 16.2G -> 3.1G; ClickHouse#8: 31.4G ->
6.4G instructions. In the base profile of #0 about half of the samples are
in the deque allocation, `shrink_to_fit`, `CurrentMemoryTracker` and
jemalloc; none of them is in the top 15 after the change.
Replacing only `window = {}` by `clear()` (keeps the deque blocks) gives
0.526 / 0.594 / 0.583 on #0 / ClickHouse#1 / ClickHouse#8, so the direct staging is 4.3x and
2.3x faster than that on #0 and ClickHouse#8.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (2 findings, both on the PR text, none on the code).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261113-003800 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261112-233501 |
|
Workflow [PR], commit [4ddb63b] Summary: ✅
AI ReviewSummaryThe PR speeds up sorted negative The slice consumers reviewed were Final Verdict✅ Approve. No new inline findings. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units8 translation units recompiled, 17 s compile time in total, 8 of them have a recent master baseline. |
|
@groeneai Check if similar issue exist in |
|
I checked The cost removed here was the per-group window: a Measured with the
For comparison, the sorted negative |
CI finish ledger - 4ddb63bEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Apart from the Session id: cron:our-pr-ci-monitor:20261113-043010 |
21809ea
|
Fixing PR for |
Related: #125013 (comment)
Related: #125000
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Negative
LIMIT BY(for exampleLIMIT -1 BY k) over data sorted by the key is several times faster when the groups are small.Description
The
negative_limit_by_in_orderqueries #0, #1 and #8 (millions of groups) are reported slower together on PRs that do not touch them (#125013 and three others in 30 days). Their time goes to the per-group bookkeeping ofNegativeLimitBySortedStreamTransform: every group, even one inside a chunk, went through the sliding window, whosewindow = {}freed the deque block and map after each group, so a one-row group cost two memory-tracked allocations, two frees and twoshared_ptrcopies. The #125013 job's counters for #0 show ~1,030 instructions per group, the same on both sides, with +15% cycles in one build; #125000, whose only change to this transform was the allocator of its containers, moved exactly #0, #1 and #8 by 16-19% at master d3ee313.A group that ends inside the chunk while the window is empty also started in this chunk, so its rows are staged for output directly, and adjacent rows of one chunk form one pending slice. Groups that touch a chunk boundary still use the window. The output is unchanged.
Local A/B (release build, no ThinLTO, the perf test's tables and queries, 12 rounds, ratio new / master, 95% CI):
Stringkeys#2 (100k groups) is 0.71x; #3-#7 (100 groups) are 0.86-1.03x, within noise. User-space instructions of #0 drop from ~1,190 to ~190 per group. Existing negative
LIMIT BYtests cover both paths: each of four mutations of the new code fails 3 to 8 of them.Workflow [PR]
Sync PR [sync-upstream/pr/131166]
Version info
26.11.1.2155-master(included in26.11and later)