Skip to content

Fix low-thread slowdown from smaller Adaptive Aggregator staging chunks - #131003

Merged
nihalzp merged 2 commits into
ClickHouse:masterfrom
nihalzp:adaptive-aggregator-staging-chunk-costs
Nov 12, 2026
Merged

nihalzp merged 2 commits into
ClickHouse:masterfrom
nihalzp:adaptive-aggregator-staging-chunk-costs

Conversation

@nihalzp

@nihalzp nihalzp commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Related: #128495

Background

The adaptive aggregator (enable_adaptive_aggregator, enabled by default) stages the rows that miss the frozen hash table of their thread as records, and merges them later, one partition at a time. Every thread keeps one buffer per partition: a chain of memory chunks that double in size from 2 KiB up to a cap. #128495 lowered the cap from 32 KiB to 8 KiB, and to 4 KiB from 17 threads on, because the unused end of the last chunk of every buffer adds up: at 64 threads there are 131,072 buffers.

With few threads, the buffers are few, so a small cap saves little memory, but each buffer holds many records in many chunks. Q32 at 4 threads stages 100 million records of 15 bytes into 2,048 buffers, about 730 KB each, which take 92 chunks of up to 8 KiB, where chunks of up to 32 KiB take 26. The merge pays for every chunk: it follows the chunks of a partition from header to header, with a cache miss each, and the hardware prefetchers start a new stream at every chunk. A top-K query by count() with other aggregates, such as Q32, also reads every partition twice: once to count its groups, and once to build the states of the best ones. That second read did not request the next chunk ahead, so it waited at the start of every chunk.

So at 4 threads the 8 KiB cap made Q32 5% slower than chunks of up to 32 KiB, for 1% less peak memory (1,637 → 1,621 MiB). #128495 (comment) found Q32 5% slower on 4 vCPUs after that PR, and 32 KiB chunks gave its time back.

Changes

The cap now follows the number of buffers, so it is large where they are few, and every read of the staged records requests the next chunk ahead.

  • Chunk cap.
    • Before: 8 KiB up to 16 threads, and 4 KiB from 17 threads on.
    • After: the largest power of two at which one unused chunk in every buffer of every thread fits in 128 MiB, but at least 4 KiB and at most 32 KiB. That is 32 KiB up to 4 threads, 16 KiB up to 8, and as before 8 KiB up to 16 and 4 KiB above.
    • Why this costs little memory: up to 8 threads there are at most 8,192 buffers, so their unused ends stay within 128 MiB. Peak memory changes by at most 2% in the results below.
  • Next chunk ahead. Every read of the staged records now requests the start of the next chunk into the cache as it enters a chunk. Before, only the reads that also prefetch their hash table slots did that: the reads of keys stored as bytes, such as String keys, and the reads into large tables.

Results

ClickBench hits (100M rows) on AWS c8a.16xlarge (64 cores, one thread per core), clickhouse-local, FORMAT Null, with each width pinned to as many cores. A cell is the median of three processes, each running the query 5 times and taking the average of runs 3 to 5. Peak memory is the peak of the query from --memory-usage.

QueryShape4 threads8 threads16 threads64 threads
TimePeak memoryTimePeak memoryTimePeak memoryTimePeak memory
Q32GROUP BY WatchID, ClientIP: count, sum, avg, top 10761.7 → 714.7 ms, 1.07×1,620 → 1,635 MiB, +1%468.0 → 450.7 ms, 1.04×1,653 → 1,686 MiB, +2%267.0 → 260.3 ms, 1.03×1,760 → 1,763 MiB, 0%112.0 → 112.3 ms, 1.00×2,153 → 2,156 MiB, 0%
GROUP BY WatchID, toUInt64(ClientIP): count, sum, avg, top 10796.7 → 739.0 ms, 1.08×2,005 → 2,017 MiB, +1%512.3 → 488.0 ms, 1.05×2,040 → 2,068 MiB, +1%287.3 → 290.3 ms, 0.99×2,142 → 2,139 MiB, 0%121.7 → 120.0 ms, 1.01×2,683 → 2,672 MiB, 0%
Q33GROUP BY URL: count, top 101,499.0 → 1,444.0 ms, 1.04×5,338 → 5,230 MiB, −2%807.7 → 791.0 ms, 1.02×5,206 → 5,153 MiB, −1%467.7 → 474.0 ms, 0.99×5,252 → 5,251 MiB, 0%213.7 → 218.3 ms, 0.98×5,858 → 5,849 MiB, 0%
Geomean1.06×0%1.04×+1%1.00×0%1.00×0%

Q32 and its variant with a 16-byte key gain the most, because their merges read every partition twice. Q33 groups by a String key, whose reads already requested the next chunk, so only the larger chunks change it, at 4 and 8 threads. From 9 threads on, the chunks stay as on master. Against the code before #128495, Q32 at 4 threads was 3% slower and is now 2% faster. Every query returns the same rows as master.

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):

The adaptive aggregator (enable_adaptive_aggregator, enabled by default) is faster for GROUP BY with many distinct keys at low thread counts: it stages records in larger chunks when few threads aggregate, and its merge requests every next chunk of records ahead. On ClickBench Q32 and Q33 and a variant of Q32 with 16-byte keys, the geometric mean speedup is 1.06x at 4 threads and 1.04x at 8 threads, up to 1.08x, with peak memory within 2%; from 16 threads on, speed and memory stay the same.


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

Version info

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

@clickhouse-gh

clickhouse-gh Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ca1bf65]

Summary: ✅


AI Review

Summary

PR #131003 sizes adaptive aggregation staging chunks by the number of producer partitions and prefetches the next range during walks without hash-table-slot prefetching. No actionable findings at ca1bf6595d5008c323b4f86d7cf28a041d24b3a7. The supplied before/after benchmarks support the claimed improvement at low thread counts.

Missing context / blind spots
  • ⚠️ No local build or executable was available. Relevant existing partition-layout, long-record, and spill tests passed in Fast test CI, but results for the updated AdaptivePartitionLayout unit tests and sanitizer runs were unavailable. Completed unit and sanitizer reports would close this runtime-validation gap.
Final Verdict

✅ Approve — no changes requested.

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

clickhouse-gh Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ca1bf6595 with master ab16fef53 (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 (+96.34 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/AdaptiveAggregation/Merge.cpp.o 9.50 MiB 9.59 MiB +96.32 KiB (+0.99%)

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.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.59, programs/clickhouse-keeper ×0.63 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 6.0 s 10.4 s +6.9 s (+195%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.6 s 7.2 s +5.0 s (+236%)
Compile time of recompiled translation units

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

Job report

@nihalzp nihalzp self-assigned this Nov 12, 2026
@nihalzp
nihalzp added this pull request to the merge queue Nov 12, 2026
Merged via the queue into ClickHouse:master with commit 7adf9a6 Nov 12, 2026
401 of 402 checks passed
@nihalzp
nihalzp deleted the adaptive-aggregator-staging-chunk-costs branch November 12, 2026 18:06
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 12, 2026
@nihalzp nihalzp mentioned this pull request Nov 19, 2026
7 of 33 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

2 participants