Repository navigation
Do not build a string for every range when index analysis looks up the cached result of a key function - #127495
Conversation
`applyFunction` caches the result of a monotonic key function over the index column in `field.columns` under the name `_0x<function pointer>_<column index>`. It built that name on every call with a `WriteBufferFromOwnString`, `writePointerHex` and `"_" + toString(column_idx)`, and it is called for both bounds of every range the analysis checks: about a million calls per query in the `long_primary_key` performance test, where `toDate(dt) AS dt` puts `toDate` on the key in `WHERE`. Format the name into a stack buffer (byte-identical text: `_0x`, 16 lowercase hex digits, `_`, the decimal index) and look it up as a `std::string_view`; make a `String` only when an entry is added. x86 RelWithDebInfo, `clickhouse local`, the test's tables with one part each, 16 interleaved launches: `FilteringMarksWithPrimaryKeyMicroseconds` median 227 -> 116 ms (#0), 225 -> 114 ms (ClickHouse#1), 227 -> 114 ms (ClickHouse#2). Related: ClickHouse#127480 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove the comment narrating why the cache key is built on the stack; the commit message of the change carries it. 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; 0 findings) plus my own cold review (1 nit).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261022-222500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261022-213300 |
|
cc @nihalzp @CurtizJ, could you review this and add the |
|
Workflow [PR], commit [8600f71] Summary: ✅
AI ReviewSummaryPR #127495, head Final VerdictStatus: ✅ Approve |
alexey-milovidov
left a comment
There was a problem hiding this comment.
The change itself is clear, but using a pointer is weird.
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, 23 s compile time in total, 7 of them have a recent master baseline. |
|
The address stands for the identity of the function here. Before #68354 the key was |
|
@groeneai, yes, send a follow-up PR. |
|
Acknowledged. I will send a follow-up PR that numbers the monotonic chain links when the |
c8e0e06
|
The follow-up is #127658. The cache key is now |
Related: #127480
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Faster primary and partition key analysis for conditions on a monotonic function of a key column, for example
toDate(t)on a table ordered byt: the analysis no longer formats a string for every range it checks. The index analysis of thelong_primary_keyperformance test is about two times faster on x86.Description
In
tests/performance/long_primary_key.xmlthe aliastoDate(dt) AS dtputstoDateon the key inWHERE, so for each range the analysis checks,applyMonotonicFunctionsChainToRangecallsapplyFunctionfor both bounds of bothtoDateatoms, about a million calls per query.applyFunctioncaches the function result over the index column under the name_0x<function pointer>_<column index>, but it built that name on every call with aWriteBufferFromOwnString,writePointerHexand"_" + toString(...), i.e. several heap allocations per call.Now the name is formatted into a stack buffer (the same text) and looked up as a
std::string_view. AStringis created only when a cache entry is added.Validation (x86, RelWithDebInfo,
clickhouse local, the test's tables with one part each, 16 interleaved launches, 3 runs per query each): medianFilteringMarksWithPrimaryKeyMicroseconds227 -> 116 ms, 225 -> 114 ms and 227 -> 114 ms for queries 0, 1 and 2 (-49%), query time 0.256 -> 0.143 s. Inperf,applyFunctiongoes from 56% to 22% of the index analysis, and no allocation remains under it.EXPLAIN indexes = 1output and query results are unchanged; the key condition, monotonicity and index analysis stateless tests pass.This PR does not claim to fix the instability of
long_primary_keyon arm reported in #127480; I will check it on the arm builds after merge.Workflow [PR]
Sync PR [sync-upstream/pr/127495]
Version info
26.11.1.677-master(included in26.11and later)