Repository navigation
Reduce Adaptive Aggregator memory usage - #128495
Conversation
… chunks of their own size
… caller choose it
…r` takes a chunk of its own size
|
Workflow [PR], commit [e502f55] Summary: ✅
AI ReviewSummaryReviewed #128495 at Consumers checked for the changed record representation:
The PR provides before/after memory and timing measurements and focused regression coverage. The fetched CI reports report 14,410 passed, zero failed, and 4,479 skipped tests. No local build or tests were run. Final VerdictStatus: ✅ Approve. No new inline findings; the previously resolved thread remains resolved. |
…finished_producer` for the smaller staged chunks
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/CMakeFiles/dbms.dir/Interpreters/AdaptiveAggregation/Merge.cpp.o |
8.39 MiB | 9.50 MiB | +1.11 MiB (+13.26%) |
src/CMakeFiles/dbms.dir/Interpreters/AdaptiveAggregation/Producer.cpp.o |
1.63 MiB | 2.01 MiB | +393.53 KiB (+23.57%) |
719 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.93 (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&) |
7.6 s | 16.0 s | +8.9 s (+125%) |
Compile time of recompiled translation units
92 translation units recompiled, 942 s compile time in total, 92 of them have a recent master baseline.
…regation` for the smaller staged records
…_bytes_to_spill` for the smaller staged records
55ae036
|
At 4 vCPU this PR makes ClickBench Q32 (
"+ this PR" is the PR's CI build ( Setup: Xeon 8375C, 4 vCPU, 30 GiB;
I tested only 4 vCPU; your 16- and 64-thread runs report unchanged times overall. In a full ClickBench run with the master builds, none of the other queries in your table was significantly slower at 4 vCPU (Q31, the same keys with |
|
Follow-up to my earlier comment on the 4-vCPU Q32 slowdown: it does not come from the 12-byte packed key. In local builds, two other changes in this PR account for it: the 8 KiB chunk cap and the merged append loop in Not the packed key. Master builds before and after this PR (
The full 16-byte key, which is not packed and saves no memory, slows down the most. Two contributors. Local builds (Release, clang-21, no ThinLTO) of
Both together, 5 rounds:
Q32 peak memory: 2012 MiB before, 1618 with the PR, 1629 with both changes. The Not resolved: the 16-byte key's remaining 3.2%. In the earlier profile, the other increases were in Limits. Everything was measured at 4 vCPU only (2 partitions per bucket), and the local builds show a smaller regression than the CI builds (Q32 about 4% vs 5.4%). The 32 KiB cap was a diagnostic applied at every producer count. A rule like 32 KiB up to 4 producers and the current 8/4 KiB above would give the measured setting here and keep the memory saving at 16 and 64 threads, but I did not measure other thread counts. If they fit, both changes could go into a follow-up PR; the cap is a one-line change in Append-loop patch for current master (whitespace-insensitive view: the existing per-record loop moves unchanged into the
|
|
@snelheidai Thanks for the report. Fixed in #131003. |
|
@nihalzp On the 4-vCPU Xeon where I measured the slowdown, #131003 removes only part of it. With official builds, Q32 stays about 4% slower than before #128495, and the 16-byte-key variant from my second comment (
Builds: before #128495 is master Local builds of The one-size append loop from my second comment, which #131003 does not include, is not a clean fix for the rest. On top of #131003 (local builds) it brings Q32 back to its time before #128495 (1.008 [0.998, 1.018]), but it makes the count-only query on the same keys 1.6% and 2.0% slower in two runs (1.016 [1.010, 1.022], 1.020 [1.015, 1.025]), so that query ends level with its time before #128495 instead of 1.7% faster. Its records go through the key-record loop ( Method: |
Related: #123531
Background: staged records
The adaptive aggregator (
enable_adaptive_aggregator, enabled by default) runs aGROUP BYin two phases:adaptive_aggregator_freeze_threshold). Then the table freezes. A row whose key is already in the table still updates it in place. A row with a new key, a miss, is not inserted: the thread writes it down as a staged record, a few bytes that hold everything the merge needs to aggregate the row later.A staged record holds the key, often its hash, and a payload: the number of rows it stands for when
count()is the only aggregate, nothing for aGROUP BYwithout aggregate functions, and the row's aggregate arguments otherwise. Each thread keeps one buffer per partition, a chain of memory chunks, and there are 1,024 partitions per thread at 16 threads and 2,048 at 64.The staged records stay in memory until the merge, so they are most of the peak memory: 50-98% of it on ClickBench. That memory has two parts: the bytes of the records, and the unused end of the last chunk of every buffer. The changes below shrink both.
Key changes
Smaller records. Each field, before → after, and why the smaller form is enough:
Stringkeys and of keys serialized from several columns. The merge needs the hash to insert the key into its table, and the record stores it because computing it again would read all the key bytes. But these tables hash with CRC32 (on CPUs with a CRC instruction) or with the hash of packed strings, and both produce only 32 bits: the upper half of the 64-bit value is either zero or a copy of the lower half, and the key itself tells which. So the record keeps the lower half, and the merge rebuilds the rest. Tables with other hash functions keep all 8 bytes.Stringargument, 4 → 1 byte. Nearly all keys and strings are shorter than 255 bytes, so one byte holds the length. A longer one takes the byte 255 followed by the 4-byte length.count()is the only aggregate. The count is the number of rows of one key that a thread folds into one record within one block, which is nearly always small. A larger count takes the byte 255 followed by the 4-byte count, as a long length does, so a key that repeats many times in a block takes one record however long it is. A fixed-width key, which is short, writes a larger count as several records of the key, and the merge adds them up, as it adds up all the records of a key.WatchID, ClientIP. Such keys are packed into one integer of up to 32 bytes, whose bytes past the columns and their null flags are always zero. The record stores only the filled bytes, and the merge reads the integer back with the rest set to zero.Strings does not store its size, since its end follows from the lengths in it.For example, the
count()records of a 20-byteSearchPhraseand of aUserID:Smaller chunks. The chunks of a buffer double in size from 2 KiB, so that a small partition takes little memory and a large one takes few chunks.
Folded records for
GROUP BYwithout aggregate functions.count()is the only aggregate. This is correct because such a record only says that its key exists: the merge inserts the key once, however many records carry it.Results
ClickBench
hits(100M rows) on AWSc8a.16xlarge(64 cores, one thread per core),FORMAT Null. Peak memory ismemory_usagefromsystem.query_log. Master already has the adaptive aggregator of #123531.GROUP BY SearchPhrase:count, whereSearchPhrase <> ''GROUP BY SearchPhrase:count(DISTINCT UserID), whereSearchPhrase <> ''GROUP BY SearchEngineID, SearchPhrase:count, whereSearchPhrase <> ''GROUP BY UserID:countGROUP BY UserID, SearchPhrase:countGROUP BY UserID, minute(EventTime), SearchPhrase:countGROUP BY SearchEngineID, ClientIP:count,sum,avg, whereSearchPhrase <> ''GROUP BY WatchID, ClientIP:count,sum,avgGROUP BY URL:countGROUP BY 1, URL:countGROUP BY ClientIP, ClientIP - 1, ClientIP - 2, ClientIP - 3:countQuery times stay the same, with a geometric mean speedup of 1.01× at both widths.
The same aggregations without
ORDER BYandLIMITproduce all their groups, and the reduction is about the same.GROUP BY SearchPhrase:count, whereSearchPhrase <> ''GROUP BY SearchPhrase:count(DISTINCT UserID), whereSearchPhrase <> ''GROUP BY SearchEngineID, SearchPhrase:count, whereSearchPhrase <> ''GROUP BY UserID:countGROUP BY UserID, SearchPhrase:countGROUP BY UserID, minute(EventTime), SearchPhrase:countGROUP BY SearchEngineID, ClientIP:count,sum,avg, whereSearchPhrase <> ''GROUP BY WatchID, ClientIP:count,sum,avgGROUP BY URL:countGROUP BY ClientIP, ClientIP - 1, ClientIP - 2, ClientIP - 3:countGROUP BYwithout aggregate functions also runs faster. The queries below are the key-only forms of the ones above,SELECT count() FROM (SELECT <keys> FROM hits GROUP BY <keys>). On master, such a query stages one record per missed row. In this PR, the repeats of a missed key within a block are removed: consecutive repeats always, and forStringand serialized keys also repeats of the last key missed in the same partition.GROUP BYkeysSearchPhrase, whereSearchPhrase <> ''SearchEngineID, SearchPhrase, whereSearchPhrase <> ''UserIDUserID, SearchPhraseUserID, minute(EventTime), SearchPhraseSearchEngineID, ClientIP, whereSearchPhrase <> ''WatchID, ClientIPURLClientIP, ClientIP - 1, ClientIP - 2, ClientIP - 3Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The adaptive aggregator (
enable_adaptive_aggregator, enabled by default) uses less memory forGROUP BYwith many distinct keys: it stages smaller records in smaller chunks. Across 11 ClickBench aggregation queries, peak memory is 25% lower at 64 threads and 21% lower at 16 threads, at the same speed. AGROUP BYwithout aggregate functions now deduplicates the repeats of a key whenever it can, which makes such queries up to 2.2x faster.Workflow [PR]
Sync PR [sync-upstream/pr/128495]
Version info
26.11.1.1957-master(included in26.11and later)