Skip to content

Move probe_rows of the adaptive aggregator's frozen kernel into an ALWAYS_INLINE member function - #132740

Merged
nihalzp merged 2 commits into
ClickHouse:masterfrom
groeneai:adaptive-aggregation-probe-rows-member-function
Nov 19, 2026
Merged

nihalzp merged 2 commits into
ClickHouse:masterfrom
groeneai:adaptive-aggregation-probe-rows-member-function

Conversation

@groeneai

@groeneai groeneai commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #131551

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Description

Follow-up to the review of #131551: the probe_rows lambda in the map overload of Aggregator::executeFrozenImpl becomes the ALWAYS_INLINE const member function Aggregator::collectArgumentMisses, the counterpart of collectCombinedMisses for records with aggregate arguments. The body moves as is, except that two nested if constexpr merge into one. No behaviour change.

At -O3 the lambda was compiled out of line (56 copies: 28 key methods x 2 shapes). The new function is inlined in every instantiation (no out-of-line symbol): executeFrozenImpl grows by the inlined loops, and without the copies the kernel's code is 3.6% smaller in total. A release A/B of three frozen-kernel queries (probe bypass, half hits, Nullable key with NULLs) is within noise: PR / master medians 0.92-1.06 over five interleaved sets, in no consistent direction.

Validation: the 67 adaptive aggregator tests pass, 50 times with randomized settings; a scratch mutation shows they reach both shapes of the function.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/132740]

Version info

  • Merged into: 26.11.1.2817-master (included in 26.11 and later)

groeneai and others added 2 commits November 18, 2026 16:42
…ber function

The `probe_rows` generic lambda in the map overload of
`Aggregator::executeFrozenImpl` (the probe/staging loop of a block whose
records carry aggregate arguments) becomes the `ALWAYS_INLINE` const member
function template `Aggregator::collectArgumentMisses`, the counterpart of
`collectCombinedMisses`, which already has this shape for records without
arguments. Its implicit captures become parameters with the same names, and
the outer `emplace_null_key` and `stage_misses` lambdas are passed as
callables, as `collectCombinedMisses` takes `stage_misses`.

The body is moved as is, except for two edits the move forces: the nested
`if constexpr` on `keeps_null_key_slot<LocalMethod>` and `record_places`
are merged into one, with the comment explaining why they had to stay apart
inside the lambda dropped (in a function template both are template
parameters of the same instantiation, so the discarded branch is still never
instantiated); and `typename LocalMethod::StateNoCache` is spelled `State`,
the deduced type of `local_find_state`. `[[maybe_unused]]` is dropped from
`emplace_null_key`, which is now referenced unconditionally.

At -O3 the base did not inline the lambda: the object had 56 out-of-line
copies (28 map variants x 2 shapes). With `ALWAYS_INLINE` the fix object has
no out-of-line `collectArgumentMisses` and 56 inlined instances.

No behaviour change.

Suggested in ClickHouse#131551 (comment)
Requested in ClickHouse#131551 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the collectArgumentMisses declaration comment to its contract and fix the stale collectCountMisses name in a moved comment (it is collectCombinedMisses).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding) plus my own review (the same finding).

# Sev Finding Verdict Evidence / action
1 💡 The moved comment in collectArgumentMisses names collectCountMisses, which no longer exists; the rule it refers to is in collectCombinedMisses AGREE, fixed comment renamed to collectCombinedMisses in the trim commit

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-slot-9:20261118-173100

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? N/A as a failure (refactor, no defect). Deterministic oracles instead: at -O3 the base object has 56 out-of-line copies of the lambda (28 key methods x 2 shapes); the fix object has no out-of-line collectArgumentMisses and 56 inlined instances (DWARF).
b Root cause explained? Readability request on #131551: a 70-line generic lambda with compile-time branches inside the NO_INLINE executeFrozenImpl; it becomes a member function, as its sibling collectCombinedMisses already is.
c Fix matches root cause? Yes: member function, ALWAYS_INLINE as asked, body moved as is (two edits forced by the move: one merged if constexpr, State for typename LocalMethod::StateNoCache).
d Test intent preserved / new tests added? No test change (behaviour-preserving). The 67 adaptive aggregator tests cover both shapes: a scratch mutation throwing in the function fails 53 of them on record_places = true and 25 on record_places = false.
e Both directions demonstrated? Base and fix debug binaries: 67/67 OK each; fix 50x randomized 3350/3350 OK; an ad-hoc probe-bypass query returns byte-identical results on both.
f Fix is general across code paths? Both call sites and all 28 map instantiations go through the one template; the set overload and collectCombinedMisses are unchanged.
g Fix generalizes across inputs (params/datatypes/wrappers)? Instantiated for exactly the methods the lambda was (Nullable, LowCardinality, fixed, packed-string and serialized keys), all covered by the existing tests.
h Backward compatible? (maintainer-approved exception only) Yes: no setting, format, protocol or on-disk change.
i Invariants and contracts preserved? Same statements in the same order (place stores, misses.add, mid-block stage_misses); keep_key_bytes recomputed inside is the state's constructor-time constant, as in collectCombinedMisses; the NULL-slot branch is still not instantiated for methods without the slot. Release A/B of three frozen-kernel queries: fix/base median 1.019, 1.024, 1.011 (7 runs), within noise.

Session id: cron:clickhouse-impl-slot-5:20261118-160201

@clickhouse-gh

clickhouse-gh Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9b8b54c]

Summary: ✅


AI Review

Summary

PR #132740 extracts the adaptive aggregator's argument-record probe loop into Aggregator::collectArgumentMisses, an ALWAYS_INLINE member template. Reviewed commit 9b8b54c573683a154e926512baf4f82ea0b7946c, including both call paths, key lifetimes, NULL handling, staging callbacks, existing regression coverage, and prior discussion. No actionable findings. The reported validation supports this behavior-preserving refactor; no local build or tests were run during this review.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog can be tested Allows running workflows for external contributors comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). labels Nov 18, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9b8b54c57 with master 50034a436 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

4 object files changed (-107.45 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/AdaptiveAggregation/Producer.cpp.o 2.78 MiB 2.67 MiB -107.43 KiB (-3.78%)

711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

90 translation units recompiled, 1274 s compile time in total, 90 of them have a recent master baseline.

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 9b8b54c

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
AST fuzzer (amd_debug, targeted) / OOM in dmesg not from this PR (the query that grew sets enable_adaptive_aggregator = 0, and the server logs that adaptive aggregation is not engaged): a keyed GROUP BY with a constant 1 MiB FixedString key on 4 remote() shards charged 8 GiB per block per thread, while the global memory tracker had just been corrected up from negative values (-22.56 GiB, -17.66 GiB), so the server limit refused the chunks only after the container was full and the kernel killed the server #131625 (mine, open)
PR praktika rollup of the row above #131625 (mine, open)

The other CIDB FAIL rows on this head are Config Workflow / Pre Hooks of the first run, before the can be tested label was set; the re-run finished. CH Inc sync is green.

Session id: cron:our-pr-ci-monitor:20261118-210024

@nihalzp nihalzp self-assigned this Nov 19, 2026
@nihalzp
nihalzp added this pull request to the merge queue Nov 19, 2026
Merged via the queue into ClickHouse:master with commit b21e162 Nov 19, 2026
571 of 573 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 19, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 19, 2026
…-pack-in-chunks

Conflict in `src/Interpreters/AdaptiveAggregation/Producer.cpp`: master moved the
argument-miss loop of the frozen kernel into `collectArgumentMisses` (ClickHouse#132740),
which takes the find state's type from its argument, so master's side is kept and
`executeFrozenImpl` still builds the state type its caller picks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants