Repository navigation
Speed up min/max on 128-bit and 256-bit types - #111965
Conversation
`min` and `max` on `Int128`, `UInt128`, `Int256`, `UInt256`, `Decimal128` and
`Decimal256` fell back to the generic per-row loop in `SingleValueDataFixed`,
because `has_find_extreme_implementation` only covered types up to 64 bits.
That loop keeps the accumulator in the aggregate state, so the compiler has to
reload the column data pointer on every row, and for `Decimal128` / `Decimal256`
it also calls the out-of-line `Decimal` comparison operator once per row.
Route these types through `findExtreme*` instead. Decimals reuse the existing
`NativeType` routing, so they compare as wide integers inline. The wide-integer
kernel updates the accumulator behind a branch rather than a select, since a
select would put the 128/256-bit accumulator on the loop-carried dependency
chain.
`findExtreme*Index` scans twice and only pays off when the first pass is
vectorized, so `argMin` / `argMax` and `ORDER BY ... LIMIT 1` keep their
single-pass loop, gated by the new `has_find_extreme_index_implementation`.
Measured on `SingleValueDataFixed::setGreatest` over a 65409-row block
(rand-full / low-64 / ascending / descending inputs, min, max, `-If` and
not-null variants):
Int128, UInt128 1.16x - 1.76x
Int256, UInt256 1.09x - 1.38x
Decimal128 1.74x - 3.45x
Decimal256 1.84x - 2.01x
No result changes: `04650_min_max_wide_integers` passes both before and after.
Co-Authored-By: Claude Opus 5 <[email protected]>
…h it `findExtremeWideImpl` took the comparator class only to re-derive a single `is_min` flag from it; take the flag directly. `executeMinOrMax` in `arrayAggregation.cpp` hand-lists the types it dispatches to `executeMinOrMaxNumeric`, so widening `has_find_extreme_implementation` left `arrayMin` / `arrayMax` on wide-integer arrays behind on the generic `compareAt` loop while the concept claimed otherwise. Add the four wide integer types to that list. Also record why `has_find_extreme_index_implementation` goes through `NativeType`: without it the concept would admit `Decimal128` / `Decimal256`, for which the index functions are deliberately not instantiated. Co-Authored-By: Claude Opus 5 <[email protected]>
|
I would be interested in checking this with #111164 |
|
Workflow [PR], commit [af34589] Summary: ✅
AI ReviewSummaryThis PR routes Final Verdict✅ No findings. |
|
📊 Cloud Performance Report
This PR only extends the min/max find-extreme kernels and array aggregation to 128/256-bit (wide) integer and Decimal types; the native-width path that every ClickBench query actually uses is left byte-identical (native types still satisfy the new concept and take the same branch). None of the flagged ClickBench queries operate on wide-integer columns, so none can be plausibly affected. The high-confidence Q32 +16.3% regression is therefore downgraded as off-path run-to-run variance, and the envelope-suppressed rows (Q4/Q9/Q15/Q31/Q34) are consistent with that reading — their deltas fall inside master's current variance band. Verdicts are also built on only 6 source runs, which widens the noise floor further. clickbenchFlagged queries (6 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. 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
|
|
Merged The only red, Tracked by #111421, with fixes already in flight: #109023 and #111964. |
|
Merged The previous run's only red was a timeout in @groeneai, please take a look at that timeout flake: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111965&sha=0e60967f14fb72a0e679fe896b31e14bb59c98ea&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20sequential%2C%202%2F2%29 — if it is worth a fix, please do it in a separate pull request and link it here. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 36/37 (97.30%) · Uncovered code |
|
It is worth a fix, and it is not really a random flake: the whole #90094 added The
Two controls make the attribution tight. One correction to the obvious reading: the guarded probe does not scan the table. On a real server its Current headroom on that check over 14 days: I will send a separate pull request that makes the diagnostics genuinely opt-in so the probes are not executed on the success path, and keep them available when a case actually fails. I will link it here. I am not proposing to raise the timeout. |
|
As promised above: #112913 It makes the Measured on No timeout raised. |
minandmaxonInt128,UInt128,Int256,UInt256,Decimal128andDecimal256fell back to the generic per-row loop inSingleValueDataFixed, becausehas_find_extreme_implementationonly covered types up to 64 bits. That loop keeps the accumulator in the aggregate state, so the column data pointer is reloaded on every row, and forDecimal128/Decimal256it also calls the out-of-lineDecimalcomparison operator once per row.These types now go through
findExtreme*. Decimals reuse the existingNativeTyperouting, so they compare as wide integers inline. The wide-integer kernel updates the accumulator behind a branch rather than a select, because a select would put the 128/256-bit accumulator on the loop-carried dependency chain.findExtreme*Indexscans twice and only pays off when the first pass is vectorized, soargMin/argMaxandORDER BY ... LIMIT 1keep their single-pass loop.Measured on
SingleValueDataFixed::setGreatestover a 65409-row block, across random / low-64-bit / ascending / descending inputs and themin,max,-Ifand not-null variants:Int128,UInt128Int256,UInt256Decimal128Decimal256Results are unchanged:
04650_min_max_wide_integerspasses both before and after the change.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Speed up
minandmaxon 128-bit and 256-bit types (Int128,UInt128,Int256,UInt256,Decimal128,Decimal256). Up to 1.8x faster for wide integers and up to 3.4x faster for wide decimals.