Repository navigation
Presize the result and copy short values inline in ColumnArray::replicateNumber - #127665
alexey-milovidov merged 2 commits into
Conversation
…l per row ColumnArray::replicateNumber grew both result arrays and called memcpy for every output row. For `materialize(range(10))` over 10M rows that is about 10M calls of 10 bytes, and in CI's CPU profiles of `rewrite_array_exists` this is where the slow runs spend their extra time (on average 81% of the gap between the slow and fast binary of a run, while `has` is flat). Compute the exact result size once, write the offsets by index, and copy each row with memcpySmallAllowReadWriteOverflow15, as ColumnString::replicate and ColumnArray::replicateString already do. Both containers are PaddedPODArray, and the copies go in increasing order, so the overflow of each copy lands in the next one or in the right padding. Locally (release, 4 pinned cores) `rewrite_array_exists` goes from 0.048 s to 0.030 s per query, `column_array_replicate` is 5-40% faster for the narrower element types and unchanged for 256-bit types, and 8000-byte arrays are unchanged. The new test covers 8 element types (widths 1 to 32 bytes, Nullable, Tuple), array lengths 0 to 36 and repeat counts 0 to 3. Co-Authored-By: Claude Opus 5.5 <[email protected]>
memcpySmallAllowReadWriteOverflow15 copies in 16-byte steps and is slower than memcpy for long values: with constant 4 MB arrays the previous commit was about 5% slower than master (per-round medians 0.189-0.193 s on master, 0.199-0.204 s with the change). Copy values of up to 64 bytes inline and keep memcpy for longer ones, the same threshold as copyStagedKeyBytes in AdaptiveAggregationKernels.cpp. Release builds, 4 pinned cores, 10 interleaved rounds, median vs master: constant 4 MB arrays 0.990, `materialize(range(1000000))` over 128 rows 1.000, `rewrite_array_exists` queries 0.638, 1 KB arrays of varying length 0.60 (the exact result size means the result is no longer regrown). 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; 3 findings on the first head, 0 on the delta recheck).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261023-231200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261023-220900 |
|
Workflow [PR], commit [19c711b] Summary: ✅
AI ReviewSummaryThe PR preallocates the result of Missing context / blind spots
Final Verdict✅ Approve — no unresolved findings. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 9 s compile time in total, 7 of them have a recent master baseline. |
9dbfeed
Related: #117136 (comment)
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Repeating arrays of numbers, as
materializeof a constant array andarrayWithConstantdo, is faster: the result is allocated once, and arrays of up to 64 bytes are copied inline instead of through a separatememcpycall. For example,SELECT has(materialize(range(10)), 5) FROM numbers(10000000)is about 35% faster.Description
Performance Comparison (amd_release, master_head, 3/6)on #117136 reportedrewrite_array_exists+17% (job). The same two queries were also slower on three master runs. In CI's CPU profiles of this test (amd, 30 days), 597 of 1056 run pairs differ by at least 40 samples, and on average 81% of that gap is inColumnArray::replicate+memcpy, whilehasitself does not change.materialize(range(10))goes throughColumnArray::replicateNumber, which for every output row grew both result arrays and calledmemcpyfor 10 bytes, about 10M calls per query. It now computes the exact result size first, asColumnString::replicatedoes, and copies values of up to 64 bytes withmemcpySmallAllowReadWriteOverflow15; longer values keepmemcpy, which is faster for them.Validation, local release A/B (10 interleaved rounds, 90 samples per query, fix/base median):
rewrite_array_existsqueries 0 and 1: 0.047 s -> 0.030 s (0.64)column_array_replicate: Decimal32 0.62, Decimal64 0.66, (U)Int128 0.87/0.89, Decimal128 0.91; 256-bit types 0.97 to 1.07 (UInt256 read 0.89 in a re-run; all three run the same code)These are local numbers; this PR's own performance comparison is the real check. The change removes the per-row call, it does not prove that a slow run cannot happen again.
New test
05446_array_replicate_numericcovers 8 element types (widths 1 to 32 bytes,Nullable,Tuple), lengths 0 to 36 and repeat counts 0 to 3. Four mutations of the new loop each make it fail.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/127665]
Version info
26.11.1.719-master(included in26.11and later)