Repository navigation
Conversation
The threshold that gates software prefetch for aggregation and join hash tables was 4 * L2 cache size. Enable prefetch once the table no longer fits in L2 instead (threshold = L2 cache size), which is where the measured benefit actually begins. Background: the threshold historically read the L2 size via sysconf(_SC_LEVEL2_CACHE_SIZE). That call is x86-only in glibc and unimplemented in musl, so on aarch64 it failed and the code fell back to the 256 KiB default, making the effective gate 4 * 256 KiB = 1 MiB. Once the L2 size started being read on more platforms (CPUID on x86, sysfs on non-x86 Linux), aarch64 began reporting its real 2 MiB L2 and the gate jumped to 4 * 2 MiB = 8 MiB. That disabled prefetch for medium hash tables (roughly 1-8 MiB), e.g. a ~100k-key GROUP BY, and regressed those workloads. Measurements on a host with a 2 MiB L2 (same L2 size as the affected ARM Neoverse-V2 runner, so getL2CacheSize() returns the same value), query "SELECT intHash32(number) % 100000 AS k, min(toDateTime(number)) FROM numbers_mt(1e8) GROUP BY k ORDER BY min ASC LIMIT 10 FORMAT Null", max_threads=32, median client time over 13 runs: gate = 4 * L2 = 8 MiB (prefetch off): 0.31s gate = 1 * L2 = 2 MiB (prefetch on): 0.27s (-13%) Sweeping the gate and the key count shows the crossover sits right at 1 * L2: prefetch wins by 13-17% whenever the per-group hash table exceeds L2, and is neutral below it (the table is cache resident, so prefetch is pure overhead). 4 * L2 sat far above that crossover. Tables that already exceed every candidate gate (e.g. ~1M keys) are unaffected, and small cache-resident tables stay below the gate and keep prefetch disabled. The same threshold and the same regression existed in getMinBytesForPrefetchInJoin (identical code from the same change), so both are updated together. Prefetch only changes when lookups are issued, not the computed result; aggregation and join outputs are identical with prefetch on and off. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-2:20260626-193800 |
|
cc @KochetovNicolai @nickitat — could you review? Lowers the aggregation/join software-prefetch gate from |
|
Workflow [PR], commit [dcb6d85] Summary: ✅
AI ReviewSummaryThis PR lowers the software-prefetch gate for aggregation and hash join hash tables from Final VerdictStatus: ✅ Approve |
|
📊 Cloud Performance Report 🟢 AI verdict: This PR lowers the hash-table prefetch threshold from 4x L2 cache size to 1x L2 in both the Aggregator and HashJoin, so prefetching now kicks in for smaller tables. ClickBench Q15 is a high-cardinality GROUP BY that runs directly through the aggregation hash-table path the diff touches, making its 10.5% speedup plausible and on-path. The improvement is a large, consistent delta confirmed by both tests, so the verdict stands. clickbench🟢 1 improved Flagged queries (1 of 43)
q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
CI finish ledger - fa8b33dCI fully finished (Finish Workflow passed 2026-06-27T00:29Z; Performance/coverage all completed; no checks queued or in progress; >20 min since the last check completed). Every failure below has an owner.
Session id: cron:our-pr-ci-monitor:20260627-013000 |
|
@groeneai, perf tests show mixed results. There are improvements and slowdowns. Investigate. |
|
@nickitat investigated: no slowdown is caused by this change. The mixed signal is the real improvements plus run-to-run noise. Decisive check: the gate is arch-identical here. Both the ARM Neoverse-V2 and the amd_release host report L2 = 2 MiB, so the gate moves 8 MiB -> 2 MiB identically on both, and a real gate-caused regression would have to appear on BOTH arches. The 10 flagged-slower queries are disjoint across arches (none appears on both):
The non-prefetch ones are clearly noise: rand = The feature's own test #5/#7 = The repeatable signal is the improvements on that window: cloud-benchmarks flagged ClickBench Q15 -13.7% and TPC-H Q4 (semi-join) -78.2%, verdict Net: no-op for tables far above L2, a win for tables in the L2..4*L2 window (the #108311 regression, ClickBench Q15, TPC-H Q4), flagged slowdowns are arch-disjoint noise. Happy to gate it behind a setting if you prefer a more conservative rollout. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 3/3 (100.00%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 3 line(s) · Uncovered code |
067caf4
…gation-prefetch-l2-gate Lower software-prefetch threshold for aggregation and join hash tables
A two-level hash table allocates all 256 buckets at their initial size up front, so its getBufferSizeInBytes() is a constant however few keys it holds: 2 MiB for string keys with a saved hash, 2.5 MiB for keys256 with a mapped value, about 6.5 MiB for TwoLevelStringHashMap. Since ClickHouse#108655 lowered the aggregation prefetch threshold from 4 x L2 to L2, every host whose L2 is below that constant prefetches every row of such a table, even with 13 keys. The look-ahead then hashes each key a second time for probes that touch a handful of cells (ClickHouse#125845: 2.3x slower on 26.8 than on 26.6). bytesForPrefetch() measures a two-level table that is still at its initial allocation by its keys, as the smallest table that holds them at the maximum load factor 1/2, and every other table by its buffer as before. Both users of the gate go through it: Aggregator::executeImpl and the self-prefetching HashMethodSerialized. StringHashTable gets initial_buffer_bytes so that TwoLevelStringHashTable is covered as well. Measured on a release build, base vs fix (executed PREFETCH instructions via perf sw_prefetch_access.any, and time): - issue query at an emulated 1 MiB L2: 301M -> 1.8M, 0.23 s -> 0.075 s - Nullable(String), 13 keys, 2 MiB L2: 99.9M -> 0.4M, 0.22 s -> 0.18 s - keys256, 13 keys, 2 MiB L2: 58.9M -> 0.4M, 0.15 s -> 0.11 s - unchanged: the ClickHouse#108311 shape, 20K-key two-level tables at 1 MiB L2 (prefetch stays on), 2M-key single-level tables Closes ClickHouse#125845 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #108311
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Lower the software-prefetch threshold for aggregation and join hash tables from
4 * L2 cache sizetoL2 cache size, so prefetch is enabled once the hash table no longer fits in L2. This recovers a regression onGROUP BYandJOINover medium-sized hash tables (~1-8 MiB) on platforms whose reported L2 is large (e.g. aarch64 with a 2 MiB L2).Description
Fixes the ARM
topn_aggregationquery 1 regression reported in #108311.Root cause.
getMinBytesForPrefetch()(aggregation) andgetMinBytesForPrefetchInJoin()(join) gate software prefetch at4 * L2 cache size. The L2 size used to be read viasysconf(_SC_LEVEL2_CACHE_SIZE), which is x86-only in glibc and unimplemented in musl. On aarch64 it failed and fell back to the 256 KiB default, so the effective gate was4 * 256 KiB = 1 MiB. Once the real L2 size started being read on more platforms (CPUID on x86, sysfs on non-x86 Linux), aarch64 reported its true 2 MiB L2 and the gate jumped to4 * 2 MiB = 8 MiB, disabling prefetch for medium hash tables and regressing ~100k-keyGROUP BYworkloads.Fix. Set the gate to
1 * L2: enable prefetch exactly when the hash table no longer fits in L2 (lookups miss to slower levels / DRAM and prefetch hides the latency); below that the table is cache resident and prefetch is pure overhead. This is architecture-adaptive (scales with the real L2) rather than a fixed constant, and the same regression in the join path is fixed alongside it.Measurements. On an x86 host with a 2 MiB L2 (same L2 size as the affected ARM Neoverse-V2 runner, so
getL2CacheSize()returns the same value), querySELECT intHash32(number) % 100000 AS k, min(toDateTime(number)) FROM numbers_mt(1e8) GROUP BY k ORDER BY min ASC LIMIT 10 FORMAT Null,max_threads=32, median client time over 13 runs:4 * L2= 8 MiB (master)1 * L2= 2 MiB (this PR)Sweeping the gate and the key count puts the prefetch-on-vs-off crossover right at
1 * L2: prefetch wins by 13-17% whenever the per-group table exceeds L2 and is neutral below it. Tables that already exceed every candidate gate (e.g. ~1M keys) are unaffected. The direction and magnitude match the -17% the reporter measured on ARM.Prefetch only changes when lookups are issued, not the computed result; aggregation and join outputs are byte-identical with prefetch on and off (verified locally).
Full performance-suite and ARM-hardware validation is left to CI (the reporter's ARM measurements + the public history at https://performance.ci.clickhouse.com/history/topn_aggregation/1?metric=client_time&arch=arm).
JOIN path measurements. The join gate (
getMinBytesForPrefetchInJoin, default-on, used in both build and probe loops) was measured separately on the same 2 MiB-L2 host. With the gate isolated soenable_software_prefetch_in_joincontrols prefetch, the only join hash tables this change flips are those whosegetBufferSizeInBytes()lands in(2 MiB, 8 MiB](roughly 40k-100k distinct keys; smaller tables stay prefetch-off under both gates, larger were already prefetched). 200M-row probe with random keys, INNER JOIN, hash algorithm,max_threads=32, median of 11 runs:1 * L2(on, this PR)4 * L2(off, master)Faster at every cardinality, no regression; join output byte-identical with prefetch on vs off.
Note:
src/Common/ColumnsHashing.halso callsgetL2CacheSize()but for a different decision (batch serialization,total_size <= 4 * L2), with an aarch64 short-circuit before the L2 logic, so it is intentionally left unchanged.Version info
26.7.1.288(included in26.7and later)