Repository navigation
Conversation
`LimitByTransform::consumeImpl` looks up every row's key in a hash table built with the Aggregator's hash methods, but its row loop issues no software prefetch. When the table holds millions of keys every lookup misses to memory, and how many of those misses overlap is left to the out-of-order core: on ARM release binaries `limit_by_misc ClickHouse#3` and `limit_by_in_pk_order ClickHouse#1` are 15-30% slower in some builds whose `consumeImpl` machine code is byte-identical to the reference and only sits at another address (seen as red performance checks on unrelated PRs, e.g. ClickHouse#131355 and ClickHouse#131211). The same query at cardinality 1000, whose table is cache resident, is not slower in those builds. Use the look-ahead prefetch the Aggregator, the hash join probe and `uniqExact` already use for large tables: when the method builds its key holder cheaply and its table has a `prefetch` member, a chunk that starts with a table larger than L2 runs `consumeImpl<true>`, which prefetches the cell of row `i + look_ahead` with the adaptive look-ahead of `PrefetchingHelper`. Otherwise the chunk runs `consumeImpl<false>`, whose code is unchanged. `NegativeLimitByTransform` is not changed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The new prefetching row loop of `LimitByTransform` engages only once the table outgrows L2, which no other LIMIT BY stateless test reaches. Cover key64, keys64 (with and without OFFSET), keys128, a Nullable key and a String key, checking each result against the arithmetic of `numbers`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
With ThinLTO both `consumeImpl` instantiations were inlined into `consume`, which recompiled the loop that runs for tables within L2. Out of line it is master's machine code (x86 release: 69/69 methods identical up to the logged function-name length), the convention `Aggregator::executeImplBatch` uses for its own `prefetch` template parameter. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…changes String and LowCardinality keys paid the prefetch on rows the consecutive-keys cache or the dictionary cache answer without a lookup (String key in runs of 64: 1.07x, LowCardinality(String) column in runs of 64: 1.19-1.23x on x86); use the JOIN probe's `has_cheap_key_calculation` without the single LowCardinality column method, and prefetch only when the key changes, since a row inside a run finds the cell its run already loaded. Plus a perf query for keys in long runs; the test's String query becomes a numeric key in runs of 3. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`low_cardinality_keys128` / `low_cardinality_keys256` (a single `LowCardinality` key of a 16- or 32-byte number, or a fixed-size multi-key that contains a `LowCardinality` column) are `AggregationMethodKeysFixed` with `has_low_cardinality = true`. Their `low_cardinality_optimization` is false, so they took the prefetching loop, and their key is rebuilt through the positions column and the dictionary for the look-ahead row as well. Measured on x86 (release, 10M rows, table above L2, 13-round confirmation): a single `LowCardinality(UInt128)` key with distinct keys ran at 1.195x master with the prefetch (+116 instructions and +19 cycles per row). Exclude every method with `has_low_cardinality = true`; all other methods are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Three `UInt64` BY columns form a 24-byte fixed key (`keys256`), which has no prepared key array and is packed column by column for every row; the prefetching loop packs the look-ahead row again at each key change. No existing performance query covers a fixed key wider than 16 bytes. Measured on x86 (release, 10M rows, 8 cores): this query 0.965x master (master 0.256 s). The same key over a 100K-key MergeTree table was 1.037x in a 13-round confirmation (cycles per row +0.3%), over 1M keys 0.825x, in runs of 64 rows 0.964x, so the method keeps the prefetch. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Clarify which reader of has_cheap_key_calculation uses join_prefetch_supported, and drop two cross-file citations from the LIMIT BY prefetch predicate. 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; 1 finding in each of rounds 1-3, none in round 4), plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261117-113500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261117-111100 |
|
Workflow [PR], commit [a96804e] Summary: ✅
AI ReviewSummaryThe PR adds adaptive prefetching to Missing context / blind spots
Final Verdict✅ Approve |
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/Processors/Transforms/LimitByTransform.cpp.o |
1.45 MiB | 1.74 MiB | +295.38 KiB (+19.84%) |
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
10 translation units recompiled, 49 s compile time in total, 10 of them have a recent master baseline.
Related: #131355
Related: #131211
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
LIMIT BYover many distinct numeric or fixed-size keys is faster: once its hash table no longer fits in the L2 cache, the row loop prefetches the cells of the rows ahead whenever the key changes, as the hash join probe does.Description
limit_by_misc #3andlimit_by_in_pk_order #1are red on ARM in #131355, a RabbitMQ-only PR (3/8, 2/8). In 45 days the pair was slower on ARM in 14 builds (2 on master), red in at least 5. The extra samples are inLimitByTransform::consumeImplforkeys64, identical machine code in both binaries, moved by 512 bytes. The same query with 1000 keys (limit_by_misc #2, cache resident) is not slower there: the mode needs lookups that miss the cache, and how many misses the core overlaps decides the loop's speed.GROUP BY, the hash join probe anduniqExactprefetch rows ahead once the table outgrows L2. This PR does the same inLimitByTransformfor the keys the join probe prefetches (has_cheap_key_calculation) exceptLowCardinalityones, and only where the key changes: a row inside a run finds the cell its run already loaded. String andLowCardinalitykeys keep the current loop: with the prefetch they ran up to 1.07 (String) and 1.23 (LowCardinality). The loop without prefetch keeps master's machine code apart from a log message's length (x86 release, all 69 methods).NegativeLimitByTransformis not changed.x86, release, PR / master:
limit_by_misc #30.60,limit_by_in_pk_order #10.66,limit_by #10.68,limit_by #100.67; String,LowCardinalityand cache-resident queries 0.98-1.02; none of the 82 LIMIT BY perf queries confirmed above 1.05. A 24-byte key, packed per row: 1.04 with 100K keys, 0.83 with 1M;Nullable(FixedString(3))+UInt320.80. Keys in runs longer than the look-ahead are not prefetched (runs of 64: one key 0.98, two keys 1.05). Keys repeating interleaved in a chunk prefetch on every change: 0.77 (GROUP BYon that shape: 0.82). ARM is measured only by this PR's CI. New: twolimit_byqueries (keys in long runs, a 24-byte key) and a stateless test of the prefetching loop (six key shapes).🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/132328]