Repository navigation
Speed up mortonEncode by resolving argument types and mask ratios once per block - #123354
Conversation
…nce per block `mortonEncode` with two arguments ran one row loop for every argument type and mask: per row and argument it tested the mask and `is_const`, reloaded the ratio, switched on the column width and, with a mask, called the out-of-line `expand` (36 call sites in the arm `executeImpl`). On arm the speed of the branch-dense paths depends on the binary's layout, so `space_filling_curve` ClickHouse#1 (constant mask) and ClickHouse#4 (`UInt32` arguments) have per-build speed modes and flag unrelated PRs in Performance Comparison (ClickHouse#122482), while ClickHouse#2 (two `UInt64` columns) is stable. Now each argument is resolved once per call into a contiguous `UInt64` array with the range expansion applied: a `UInt64` column that needs no expansion is read in place, other widths are widened with one type dispatch, and a constant ratio is applied in one loop per ratio value (`expand` is `ALWAYS_INLINE` and called with a template-constant ratio, so its switch folds). A per-row mask keeps the per-row `expand`. `nd == 1` writes the result column directly. The encode loops only encode. x86, one core, median fix/base of 5 interleaved rounds over the queries of `space_filling_curve.xml`: ClickHouse#1 0.57 (BMI2) / 0.55 (lookup-table path, built with `-mno-bmi2`), ClickHouse#2/ClickHouse#3/ClickHouse#6 0.45-0.64, ClickHouse#4 0.86-0.88, untouched functions 0.99-1.03. Results are unchanged: 545 checksums over every width, mask and ratio are byte-identical before and after on both code paths, and the new test fails on both paths under two mutations of the change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Every checksum in the test was `sum(cityHash64(mortonEncode(...)))`, which does not change when results move between rows, so a defect that writes the encoded values to the wrong rows of a block would pass. Hash `number` together with each result instead. The reference is regenerated with the unchanged base binary and is byte-identical on the patched one, on both the BMI2 and the lookup-table code paths. 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; 4 findings over 3 runs, the last run clean).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261001-222600 |
|
Workflow [PR], commit [042f365] Summary: ✅
AI ReviewSummaryThis PR refactors Missing context / blind spots
Final Verdict
LLVM Coverage ReportMeasured on commit 042f365.
Changed lines: Changed C/C++ lines covered: 61/63 (96.83%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes4 object files changed (-25.30 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units7 translation units recompiled, 6 s compile time in total, 7 of them have a recent master baseline. |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
Related: #122482
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Function
mortonEncodenow resolves the type of each argument and the ratios of a constant range mask once per block instead of once per row, which makes it up to 2x faster.Description
Requested in #122482 (comment):
space_filling_curve#1 (mortonEncode((1, 2), number, number * 2),max_threads = 1) was +15.8% in Performance Comparison (arm_release, master_head, 4/6). On arm, #1 moved by more than 8% in 2984 of 7548 runs in 30 days and #4 (UInt32arguments) in 917 of 7571, while #2 (twoUInt64columns, no mask) moved in 3: their speed depends on the binary.All three run the same row loop. Per row and argument it tested the mask, switched on the column width and, with a mask, tested
is_const, reloaded the ratio and called the out-of-lineexpand. Now each argument is resolved once per call into a contiguousUInt64array with the range expansion applied (aUInt64column without expansion is used in place), and a constant ratio is applied in one loop per ratio value. The encode loops only encode: in the arm codegen thend == 2loop is 31 instructions per row with one branch and no call. A per-row mask keeps the per-row path. Results are unchanged. The cost is a temporaryUInt64buffer of the block's size for each argument that needs widening or expansion.space_filling_curve.xmlon x86, one pinned core, median fix/base ratio of 5 interleaved rounds:-mno-bmi2)mortonDecode,hilbertEncode(untouched)I measured on x86 only; the arm effect can only be checked in this PR's Performance Comparison. The new test pins each row's result for every argument width, mask kind and ratio on both code paths; speed is covered by
space_filling_curve.xml.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/123354]
Version info
26.10.1.1324-master(included in26.10and later)