Repository navigation
Count regexp cache hits once per block in LIKE and match with a non-constant pattern - #122764
Algunenano merged 3 commits into
Conversation
`Regexps::LocalCacheTable::getOrSet` incremented `RegexpLocalCacheHit` or `RegexpLocalCacheMiss` on every lookup, and `MatchImpl::matchOneRow` does one lookup per row whenever the pattern of `LIKE`/`match` is not a constant. `ProfileEvents::increment` does an atomic add at every level of the counter chain. The user and global levels are per-CPU since ClickHouse#105056, but the thread group level is a single row, so every thread of the query wrote the same cache line on every row. The query was bound by that line: `re2_regex_caching` `ClickHouse#3` took 91 ms at both 8 and 32 threads, and its cost differed between server processes, which made `ClickHouse#3`, `ClickHouse#5` and `ClickHouse#6` of that test noisy on the amd performance shards. The table now counts hits and misses in two members and adds them to ProfileEvents once, in its destructor. A table is a local of `vectorVector`/`vectorFixedVector`/`constantVector`, so it lives for one block on one thread: the totals per query are unchanged (a miss is still counted before `createRegexp`, so a failed compile still reports it), and the shared counter is written twice per block instead of once per row. Tables that did no lookup report nothing, as before. With `trace_profile_events = 1` these two events now give one `trace_log` row per block (with the block total) instead of one per row. Release build, 8 threads pinned to 8 physical cores, 10 server processes per binary: `ClickHouse#3` and `ClickHouse#5` 90 -> 18 ms, `ClickHouse#6` 94 -> 34 ms, the other queries of the test unchanged; user CPU at 8 threads over 1 thread for `ClickHouse#3` goes from 5.2 to 1.0. At 32 threads `ClickHouse#3` goes from 91 to 15 ms. The new test checks the exact hit and miss totals for the three callers (String, FixedString and constant haystacks). Related: ClickHouse#121795, ClickHouse#100759 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reword the comment on the batched hit/miss counters: 'contend on' instead of 'hit', which read as a cache hit next to the hits member. 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 cold review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20260928-205300 |
|
Workflow [PR], commit [49c0193] Summary: ✅
AI ReviewSummaryThis PR batches Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 49c0193.
Changed lines: Changed C/C++ lines covered: 12/12 (100.00%) · Uncovered code |
| @@ -0,0 +1,15 @@ | |||
| -- Regexp cache hits and misses of LIKE and match with a non-constant pattern are counted exactly. | |||
There was a problem hiding this comment.
Added tests/performance/like_match_nonconst_pattern.xml in 49c0193: the String, FixedString and constant haystacks of this test, each with a non-constant pattern of 10 distinct values over 20M rows. Release build, perf.py with both servers pinned to the same 8 cores: 1.12-1.23 s before, 0.15-0.17 s after.
I kept 05292: with the flush in the destructor removed it reports 0 hits and 0 misses, which the performance test cannot see (the query only gets faster). I can drop it if you prefer.
The three haystack kinds of 05292 (String, FixedString and a constant), each with a non-constant pattern of 10 distinct values over 20M rows. Release build, perf.py with both servers pinned to the same 8 physical cores: 1.12-1.23 s before this change, 0.15-0.17 s after it (-86% to -88%, p < 1e-8). Four fix-vs-fix runs with fresh servers stay within 3.1%, with a stat_threshold of at most 0.123. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units46 translation units recompiled, 192 s compile time in total, 46 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
|
Algunenano
left a comment
There was a problem hiding this comment.
Nice finding. @groeneai Could you please look if other functions might have similar issues where they update profile events per row? If it does, could you address them in a PR and ping me / assign it to me please? No need of performance tests in that one
|
Acknowledged. I'm auditing the other functions for ProfileEvents updated once per row; I'll follow up here with a separate PR (requesting your review on it), or with my findings if there is nothing else to fix. |
|
@Algunenano The follow-up is #122920, with your review requested. It fixes three paths that updated profile events for every row: |
…eedles `MultiRegexps::getOrSet` incremented `RegexpWithMultipleNeedlesGlobalCacheHit` or `...Miss` on every call, inside the critical section of the process-wide cache mutex. With a non-constant array of needles, `vectorVector` calls it once per row, so `multiMatchAny`, `multiMatchAnyIndex`, `multiMatchAllIndices`, `multiFuzzyMatchAny`, `multiFuzzyMatchAnyIndex` and `multiFuzzyMatchAllIndices` updated a profile event per row. The profile event counters of a thread group are shared by all threads of the query (26.9, 400K rows with `reject_expensive_hyperscan_regexps = 0`: 0.079 s with 1 thread, 0.213 s with 8). This is the shape `LocalCacheTable` had before ClickHouse#122764. Count hits and misses in a `GlobalCacheCounters` owned by the caller and add them to ProfileEvents once, in its destructor, which also keeps the counts when the loop throws. The two per-row callers keep one for the whole block; the callers that look up once (constant needles, text index analysis) use a two-argument overload that adds them after the lookup. The totals are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #121795 (comment)
Related: #100759
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
LIKE,ILIKE,matchand their variants are up to 5 times faster with 8 threads when the pattern is not a constant. TheRegexpLocalCacheHitandRegexpLocalCacheMissprofile events are now added once per block instead of once per row, which removes contention on a counter shared by all threads of the query.Description
re2_regex_caching #3(likewith 10 distinct non-constant patterns) was red inPerformance Comparison (amd_release, master_head, 3/6)on #121795, which does not touch this code: job, 0.0742 -> 0.1096 s. Over the last 14 days of amd runs (801),#3,#5and#6moved by 30% or more in 198, 189 and 120 runs; the constant-pattern queries of the same test in 0 or 1.Cause. With a non-constant pattern,
Regexps::LocalCacheTable::getOrSetruns once per row and incrementedRegexpLocalCacheHitorRegexpLocalCacheMisseach time. Since #105056 the user and global levels of ProfileEvents are per-CPU, but the thread group level is a single row, so every thread of the query did an atomic add on the same cache line per row. In the CI CPU profiles that increment is 53-71% of the samples of#3and#5, and in the red run the whole difference between the two servers is in it (1210 vs 2328 samples, the rest 1058 vs 986).Change. The table counts hits and misses in two members and adds them once, in its destructor. It is a local of the per-block function, so per-query totals are unchanged.
Validation. Release build, 8 threads on 8 physical cores, 10 server processes per binary:
#3and#590 -> 18 ms,#694 -> 34 ms, others unchanged; user CPU at 8 threads over 1 thread goes from 5.2 to 1.0 for#3. The new performance testlike_match_nonconst_patternmeasuresString,FixedStringand constant haystacks: 1.12-1.23 -> 0.15-0.17 s withperf.py. The stateless test checks the exact hit and miss totals for the same three.multiMatch*with non-constant needles uses a different, mutex-protected global cache and is not changed.Workflow [PR]
Sync PR [sync-upstream/pr/122764]
Version info
26.10.1.987-master(included in26.10and later)