Skip to content

Per-call cache of non-constant replaceRegexpAll/replaceRegexpOne patterns - #122850

Closed
NIKTONIKTO717 wants to merge 1 commit into
masterfrom
small-perf-wins-from-lowlevel-pattern-audit-2026-09-9
Closed

NIKTONIKTO717 wants to merge 1 commit into
masterfrom
small-perf-wins-from-lowlevel-pattern-audit-2026-09-9

Conversation

@NIKTONIKTO717

@NIKTONIKTO717 NIKTONIKTO717 commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

replaceRegexpAll and replaceRegexpOne with a non-constant pattern cache the compiled patterns for the duration of a call. 1 thread, 1M rows, 4 distinct patterns: 9.3 s → 1.1 s (8x faster).

Up to 256 buckets by hash plus a scratch slot, with byte-exact hits. A new pattern enters its bucket only when it comes back, so all-distinct patterns are unchanged (±1.3%).

Performance test: new replaceRegexp_nonconst_needle.xml.

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Much faster replaceRegexpAll and replaceRegexpOne when the pattern is a non-constant column with repeating values.


Workflow [PR]
Sync PR [sync-upstream/pr/122850]

@clickhouse-gh

clickhouse-gh Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1cc5e46]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_llvm_coverage, 1/8) FAIL
test_storage_iceberg_no_spark/test_iceberg_inverted_manifest_bounds.py::test_iceberg_narrow_manifest_bounds FAIL cidb
Performance Comparison (amd_release, master_head, 4/6) FAIL Performance dashboard
parallel_hash_build_phase #0 unstable query history
Performance Comparison (arm_release, master_head, 1/6) FAIL Performance dashboard
Check Results FAIL

AI Review

Summary

This PR adds a per-call cache for compiled non-constant replaceRegexpAll and replaceRegexpOne patterns, plus functional and performance coverage for the repeated-pattern case. I reviewed the current diff, the full modified implementation, prior PR discussion, and the green CI surface, and I did not find a correctness, safety, or contract issue that warrants a review finding.

Final Verdict

✅ No findings.

LLVM Coverage Report

Measured on commit 1cc5e46.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.60% 79.60% +0.00%

Changed lines: Changed C/C++ lines covered: 46/46 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Sep 29, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... label Sep 29, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 1cc5e46a3 with master d3e425115 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 3ec5eca8d; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

3 object files changed (+46.52 KiB total), 0 added.

Object file Master PR Δ
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/replaceRegexpOne.cpp.o 319.00 KiB 343.82 KiB +24.83 KiB (+7.78%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/replaceRegexpAll.cpp.o 399.82 KiB 421.53 KiB +21.71 KiB (+5.43%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

8 translation units recompiled, 14 s compile time in total, 8 of them have a recent master baseline.

Job report

@clickhouse-gh

clickhouse-gh Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 36 queries analysed

This PR adds a per-block cache of compiled regular expressions to replaceRegexpOne/replaceRegexpAll, but only when the pattern argument is a non-constant column. The constant-pattern path is unchanged. The only flagged query, clickbench Q23 (-40.0%), does not call these functions with a per-row pattern, so it is downgraded to not_sure. Master's timings for this query are very noisy (its recent history is flaky), and the PR's 51 ms median falls inside master's current range of 48–87 ms. No tpch_adapted_1_official queries were flagged. The workload this PR targets is measured by the new replaceRegexp_nonconst_needle performance test, not by these benchmarks.

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 23 not_sure 85 51 -40.0% <0.0001 This PR only changes replaceRegexpOne/All when the pattern is a non-constant column, and this query does not use that path. The -40.0% change is not attributable to this PR.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: a05ce21a-0dc1-4a11-ac3e-bb8861ff5cda
  • MIRAI run: 3ec95fc0-e3a9-4cc5-b999-db880590519f
  • PR check IDs:
    • clickbench_2389654_1790689488
    • clickbench_2389667_1790689487
    • clickbench_2389859_1790689505
    • tpch_adapted_1_official_2390016_1790689556
    • tpch_adapted_1_official_2390148_1790689562
    • tpch_adapted_1_official_2390240_1790689564

@rschu1ze

Copy link
Copy Markdown
Member

We are optimizing a corner case. Consider abandoning this PR.

return regexp_options;
}

class LocalRegexpCache

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is virtually the same class in src/Functions/Regexps.h, let's unify or reuse but not copypaste code without good reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants