Skip to content

Speed up replaceRegexpAll and replaceAll by replacing underlying regexp library#62436

Closed
taiyang-li wants to merge 8 commits intoClickHouse:masterfrom
bigo-sg:opt_replace
Closed

Speed up replaceRegexpAll and replaceAll by replacing underlying regexp library#62436
taiyang-li wants to merge 8 commits intoClickHouse:masterfrom
bigo-sg:opt_replace

Conversation

@taiyang-li
Copy link
Copy Markdown
Contributor

@taiyang-li taiyang-li commented Apr 9, 2024

  • Transform replaceRegexp(One|All) to replace(One|All) if pattern is trivial and replacement doesn't contain any captured substitutions.
  • Replace underlying regexp library from re2 to OptimizedRegularExpression.
  • Add a new interface unsigned match(std::string_view text, const char * subject, size_t subject_size, MatchVec & matches, unsigned limit) const in OptimizedRegularExpression to process cases like: select replaceRegexpAll('Hello, World!', '^', 'here: '), making it compatiable with original re2 library.
  • Replace offset value std::string::npos with its actual value. It makes sense because even when captured pieces are empty, we still want to know its position for later replacement.

Why we need to add a new interface unsigned match(std::string_view text, const char * subject, size_t subject_size, MatchVec & matches, unsigned limit) const?

We have to add a new interface to make sure OptimizedRegularExpression have the same functionality with previous regexp library re2.

When invoking unsigned match(std::string_view text, const char * subject, size_t subject_size, MatchVec & matches, unsigned limit) const.

  • text: the whole input: 'Hello, World!'
  • subject: pointer to suffix of text after we have processed some prefix of text. In function regexpRegexpAll, above match method maybe called multiple times. Each time we have the same text but different subject.
  • subject_size: suffix length

Consider case: select replaceRegexpAll('Hello, World!', '^', 'here: ') . The pattern ^ refers to the beginning of text 'Hello, World!', it should be matched only once. But old interface in OptimizedRegularExpression doesn't meet the the requirement.

If we use the new interface above, valid match will only happend once, which makes replacement happen only once. The output matches are:

  • First time: subject = haystack_data, length(matches) = 1 and matches[0]: offset 0, length 0
  • Second time: subject = haystack_data + 1, length(matches) = 1, length(matches) = 0.

And the sql will outcome correct output.

If we use the old interface unsigned match(const char * subject, size_t subject_size, MatchVec & matches, unsigned limit) const, it will be invoked multiple times, which makes replacement happen multiple times. The output matches are:

  • First time: subject = haystack_data, length(matches) = 1 and matches[0]: offset 0, length 0
  • Second time: subject = haystack_data + 1, length(matches) = 1 and matches[0]: offset 0, length 0
  • Multiple loops until subject reaches end of haystack_data

It is obvious that the sql generate wrong output under current condition.

Changelog category (leave one):

  • Performance Improvement

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

Speed up replaceRegexp by 1.8x (4.41x especially when needle and replacement are trivail ) and replaceAll by 1.10x

@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-performance Pull request with some performance improvements label Apr 9, 2024
@robot-ch-test-poll2
Copy link
Copy Markdown
Contributor

robot-ch-test-poll2 commented Apr 9, 2024

This is an automated comment for commit 9ac4fed with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Check nameDescriptionStatus
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR⏳ pending
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests❌ failure
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ failure
Successful checks
Check nameDescriptionStatus
A SyncThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help✅ success
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integrational tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests✅ success
Mergeable CheckChecks if all other necessary checks are successful✅ success
PR CheckThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts✅ success

@taiyang-li taiyang-li changed the title Speed up replaceRegexpAll Speed up replaceRegexpAll and replaceAll Apr 9, 2024
@taiyang-li
Copy link
Copy Markdown
Contributor Author

taiyang-li commented Apr 9, 2024

replaceAll

with 'Many years later as he faced the firing squad, Colonel Aureliano Buendia was to remember that distant afternoon when his father took him to discover ice.' as s select replaceAll(materialize(s), ', ', '. ') as w from numbers(10000000) format Null; 

Old: 
localhost:9001, queries: 10, QPS: 1.064, RPS: 10638950.453, MiB/s: 81.169, result RPS: 0.000, result MiB/s: 0.000.

0.000%          0.867 sec.
10.000%         0.875 sec.
20.000%         0.876 sec.
30.000%         0.887 sec.
40.000%         0.888 sec.
50.000%         0.899 sec.
60.000%         0.899 sec.
70.000%         0.909 sec.
80.000%         0.928 sec.
90.000%         0.985 sec.
95.000%         1.177 sec.
99.000%         1.177 sec.
99.900%         1.177 sec.
99.990%         1.177 sec.


New: 
localhost:9001, queries: 10, QPS: 1.169, RPS: 11685467.795, MiB/s: 89.153, result RPS: 0.000, result MiB/s: 0.000.

0.000%          0.827 sec.
10.000%         0.840 sec.
20.000%         0.842 sec.
30.000%         0.842 sec.
40.000%         0.842 sec.
50.000%         0.842 sec.
60.000%         0.842 sec.
70.000%         0.844 sec.
80.000%         0.853 sec.
90.000%         0.856 sec.
95.000%         0.862 sec.
99.000%         0.862 sec.
99.900%         0.862 sec.
99.990%         0.862 sec.

@rschu1ze rschu1ze self-assigned this Apr 9, 2024
@rschu1ze rschu1ze changed the title Speed up replaceRegexpAll and replaceAll Speed up replaceRegexpAll and replaceAll Apr 9, 2024
@taiyang-li
Copy link
Copy Markdown
Contributor Author

@taiyang-li
Copy link
Copy Markdown
Contributor Author

taiyang-li commented Apr 11, 2024

@taiyang-li taiyang-li changed the title Speed up replaceRegexpAll and replaceAll Speed up replaceRegexpAll and replaceAll by replacing underlying regexp lib. Apr 11, 2024
@taiyang-li taiyang-li changed the title Speed up replaceRegexpAll and replaceAll by replacing underlying regexp lib. Speed up replaceRegexpAll and replaceAll by replacing underlying regexp library Apr 11, 2024
@taiyang-li
Copy link
Copy Markdown
Contributor Author

@rschu1ze hope for your reviews, thanks very much.

@rschu1ze

This comment was marked as outdated.

@rschu1ze

This comment was marked as outdated.

@taiyang-li

This comment was marked as outdated.

@taiyang-li
Copy link
Copy Markdown
Contributor Author

@rschu1ze any progress recently ?

@taiyang-li
Copy link
Copy Markdown
Contributor Author

Anybody help to review it ?

@rschu1ze
Copy link
Copy Markdown
Member

rschu1ze commented Jul 5, 2024

Sorry for the delay, I'll check today.

@rschu1ze
Copy link
Copy Markdown
Member

rschu1ze commented Jul 7, 2024

CLosed in favor of #66185 - thanks!

@rschu1ze rschu1ze closed this Jul 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants