Repository navigation
Avoid a 4 KiB allocation per value when building a large literal IN list - #132227
Conversation
A literal list such as `x IN (v1, ..., vN)` becomes one `Tuple` constant with N one-row element columns. `IDataType::createColumnConst` created an empty column and inserted the value, so the first `push_back` of every nested `PODArray` went through `reserveForNextSize` and allocated the 4 KiB initial chunk: N allocations, ~40 MB for N = 10000, freed again with the query tree. `DataTypeNullable::createColumnConst` did the same for the nested column and the null map. Reserve exactly one row before the insert (`IColumn::reserve` recurses into tuple elements, nullable/array/map/low-cardinality/object/dynamic parts), and create the null map with its single value. Only the capacity of the one-row column changes. Peak query memory (Release, median of 5): 10000 UInt64 keys in a PK window 51.0 -> 13.2 MiB, 10000 String keys 58.1 -> 16.4 MiB, set_index.xml ClickHouse#4 50.6 -> 12.9 MiB, ClickHouse#8 100.1 -> 24.1 MiB; queries without a literal list unchanged. Closes ClickHouse#132144 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Rewrite the createColumnConst comment as an exact invariant (only PODArrays that grow by push_back get the 4 KiB initial chunk). 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).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261117-041900 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261117-035800 |
|
Workflow [PR], commit [187d4a0] Summary: ✅
AI ReviewSummaryThe PR reserves one row before inserting constant values in Final VerdictStatus: ✅ Approve |
CI finish ledger - 2161730Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The other CIDB Session id: cron:our-pr-ci-monitor:20261117-073017 |
Algunenano
left a comment
There was a problem hiding this comment.
Really good improvement for such simple fix. Nice
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 12 s compile time in total, 8 of them have a recent master baseline. |
Closes: #132144
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Building the constant of a large literal list such as
x IN (v1, ..., vN)no longer allocates a 4 KiB buffer per value: a query with 10000 values in the list runs about 17% faster and uses about 38 MiB less memory.Description
A literal list such as
x IN (v1, ..., vN)becomes oneTupleconstant with N one-row element columns.IDataType::createColumnConstcreated an empty column and then inserted the value, so the firstpush_backinto every nestedPODArrayallocated its 4 KiB initial chunk: N allocations, about 40 MB for N = 10000, freed again with the query tree.DataTypeNullable::createColumnConstdid the same for its nested column and null map. The analysis is from #132144.The change reserves exactly one row before the insert (
IColumn::reservealready recurses into tuple elements and nullable, array, map and low-cardinality parts) and creates the null map with its single value. Only the capacity of the one-row column changes.Release build, 4M-row MergeTree tables, PR / master (95% CI), peak memory:
UInt64keys in a primary key window: 0.825 (0.804-0.847), 51.0 -> 13.2 MiBStringkeys: 0.813 (0.794-0.833), 58.1 -> 16.4 MiBUInt64keys: 0.880 (0.853-0.908)set_index.xmldoc: fix typo #4 (10000Stringliterals): 0.738 (0.717-0.760), 50.6 -> 12.9 MiB; Add Dockerfiles for client and server #8 (10000 pairs): 0.657 (0.648-0.666), 100.1 -> 24.1 MiBArrayliteral) 0.995 (0.984-1.006); 100 keys 0.972 (0.948-0.996)Timing: one Release binary that switches the code path at runtime (measurement-only, not part of this PR), 12 fresh server processes per side in a crossover over 3 data directories, median of 25 runs per process; ratios PR / master with 95% CI over the 12 processes; an A/A pair on the same code path was within +-2.3%. The perf comparison covers this through
tests/performance/set_index.xmlqueries #4/#5 (10000 literals) and #8/#9 (10000 pairs), with #6/#7 (Arrayliterals) as controls.No new test: no query result can change. About 1700 related stateless tests and an equivalence script over constants of 20 types gave the same results as master.
Workflow [PR]
Sync PR [sync-upstream/pr/132227]
Version info
26.11.1.2610-master(included in26.11and later)