Repository navigation
Return the argument of emptyArrayToSingle when no array is empty - #130581
Conversation
An aligned `LEFT ARRAY JOIN` runs `emptyArrayToSingle` over every joined array column of a block. The function always rebuilt the column one row at a time, with a `PODArray::resize` and an out-of-line `memcpy` call per row, even when no array was empty and the result equals the input. That copy was the whole difference between `LEFT ARRAY JOIN` and `ARRAY JOIN` over the same data (`array_join` ClickHouse#1 0.112 s vs #0 0.058 s on master). A per-row loop around a call to a tiny out-of-line callee is sensitive to code placement. On ClickHouse#128472, whose diff only touches Iceberg manifest writing, `Performance Comparison (amd_release, master_head, 6/6)` flagged `array_join` ClickHouse#1 at +15% (confirmed after restart at three stack placements). In the job's CPU profile the extra time is all in this loop (self +30%, its `memcpy` calls +76%), while `executeImpl` is byte-identical in the reference and the PR binary, only at another address. Now the function checks the offsets once (`std::adjacent_find`, stopping at the first empty array) and returns its argument as is when no array is empty, which takes the loop off the path of such queries. Columns are copy-on-write, so sharing the argument is safe, as in `executeConst` here. Local RelWithDebInfo A/B, 10 interleaved rounds of 11 runs, 8 threads: `array_join` ClickHouse#1 0.1015 -> 0.0620 s (1.65x), #0 and a column with one empty array per block unchanged (0.98x, 1.00x). `00206_empty_array_to_single` gains queries for every element type path, `Nullable`, `LowCardinality`, nested arrays, an empty array only in the last row, a `LEFT ARRAY JOIN` mixing a column with and without empty arrays, and a `Map`; the reference was produced by the unchanged binary. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128472&sha=7d3c4efb51731de1f1f13b27344be6f8e4645e81&name_0=PR&name_1=Performance%20Comparison%20%28amd_release%2C%20master_head%2C%206%2F6%29 Related: ClickHouse#128472 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) plus my own review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261110-182627 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261110-172600 |
|
Workflow [PR], commit [c47f9d4] Summary: ✅
AI ReviewSummaryPR #130581, reviewed at Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units7 translation units recompiled, 6 s compile time in total, 7 of them have a recent master baseline. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Quite obscure, but good.
b192835
Related: #128472
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
LEFT ARRAY JOINis faster when no joined array is empty:emptyArrayToSinglenow returns its argument as is instead of copying it row by row, which makes theLEFT ARRAY JOINquery of thearray_joinperformance test about 1.6x faster.Description
Performance Comparison (amd_release, master_head, 6/6)on #128472, which only changes Iceberg manifest writing, flaggedarray_join #1(LEFT ARRAY JOINof two one-element arrays over 10M rows) at +15%, confirmed after restart: report.An aligned
LEFT ARRAY JOINrunsemptyArrayToSingleover every joined array. The function always rebuilt the column one row at a time, aPODArray::resizeand an out-of-linememcpycall per row, even when no array is empty and the result equals the input. That copy is the whole gap between#1(0.112 s on master) and the innerARRAY JOIN#0over the same data (0.058 s). In the job's CPU profile all extra time of the PR binary is in this loop (self +30%, itsmemcpycalls +76%), whileexecuteImplis byte-identical in both binaries, only at another address: a per-row call loop that is sensitive to code placement.Now
emptyArrayToSinglechecks the offsets once (std::adjacent_find, stopping at the first empty array) and returns its argument when no array is empty. Columns are copy-on-write, andexecuteConstalready returns its argument for the same case.Validation: local RelWithDebInfo A/B, 10 interleaved rounds of 11 runs, 8 threads:
#10.1015 -> 0.0620 s (1.65x);#0and a column with one empty array per block stay within noise (0.98x, 1.00x).00206_empty_array_to_singlegains queries for every element type path,Nullable,LowCardinality, nested arrays, an empty array only in the last row, aLEFT ARRAY JOINmixing both cases, and aMap; its new reference comes from the unchanged binary, and fast paths with either condition removed fail it.Workflow [PR]
Sync PR [sync-upstream/pr/130581]
Version info
26.11.1.1910-master(included in26.11and later)