Skip to content

Process string functions XXXUTF8 'asciily' if input strings are all ascii chars - #61632

Merged
al13n321 merged 15 commits into
ClickHouse:masterfrom
bigo-sg:opt_string_ascii
May 13, 2024
Merged

al13n321 merged 15 commits into
ClickHouse:masterfrom
bigo-sg:opt_string_ascii

Conversation

@taiyang-li

@taiyang-li taiyang-li commented Mar 20, 2024 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Performance Improvement

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

Process string functions XXXUTF8 'asciily' if input strings are all ascii chars. Inspired by apache/doris#29799. Overall speed up by 1.07x~1.62x. Notice that peak memory usage had been decreased in some cases.

Q1(1.27x for substringUTF8): 
select substringUTF8(materialize('hello world'), 2, 5) from numbers(100000000) format Null;
Before:
0 rows in set. Elapsed: 3.052 sec. Processed 100.00 million rows, 800.00 MB (32.77 million rows/s., 262.16 MB/s.)
Peak memory usage: 4.76 MiB.
After:
0 rows in set. Elapsed: 2.409 sec. Processed 100.00 million rows, 800.00 MB (41.52 million rows/s., 332.13 MB/s.)
Peak memory usage: 4.26 MiB.



Q2(1.24x for substringIndexUTF8): 
select substringIndexUTF8(materialize('www.clickhouse.com'), '.', 2) from numbers(100000000) format Null;
Before:
0 rows in set. Elapsed: 4.784 sec. Processed 100.00 million rows, 800.00 MB (20.90 million rows/s., 167.23 MB/s.)
Peak memory usage: 9.76 MiB.
After:
0 rows in set. Elapsed: 3.861 sec. Processed 100.00 million rows, 800.00 MB (25.90 million rows/s., 207.21 MB/s.)
Peak memory usage: 5.26 MiB. 


Q3(1.62x for reverseUTF8): 
select reverseUTF8(materialize('hello world')) from numbers(100000000) format Null; 
Before: 
0 rows in set. Elapsed: 3.044 sec. Processed 100.00 million rows, 800.00 MB (32.85 million rows/s., 262.80 MB/s.)
Peak memory usage: 4.51 MiB.
After: 
0 rows in set. Elapsed: 1.882 sec. Processed 100.00 million rows, 800.00 MB (53.14 million rows/s., 425.10 MB/s.)
Peak memory usage: 4.51 MiB. 


Q4(1.07x for lowerUTF8):
select lowerUTF8(materialize('hello world')) from numbers(100000000) format Null; 
Before: 
0 rows in set. Elapsed: 1.128 sec. Processed 100.00 million rows, 800.00 MB (88.64 million rows/s., 709.12 MB/s.)
Peak memory usage: 4.51 MiB.
After: 
0 rows in set. Elapsed: 1.073 sec. Processed 100.00 million rows, 800.00 MB (93.19 million rows/s., 745.55 MB/s.)
Peak memory usage: 4.50 MiB.


Q5(1.07x for upperUTF8):
select upperUTF8(materialize('hello world')) from numbers(100000000) format Null;
Before: 
0 rows in set. Elapsed: 1.137 sec. Processed 100.00 million rows, 800.00 MB (87.92 million rows/s., 703.37 MB/s.)
Peak memory usage: 4.51 MiB. 
After: 
0 rows in set. Elapsed: 1.067 sec. Processed 100.00 million rows, 800.00 MB (93.71 million rows/s., 749.65 MB/s.)
Peak memory usage: 4.50 MiB. 


Q6(1.23x for leftPadUTF8): 
select leftPadUTF8(materialize('hello '), 10, ',') from numbers(100000000) format Null; 
Before: 
0 rows in set. Elapsed: 4.322 sec. Processed 100.00 million rows, 800.00 MB (23.14 million rows/s., 185.11 MB/s.)
Peak memory usage: 5.01 MiB.
0 rows in set. Elapsed: 3.503 sec. Processed 100.00 million rows, 800.00 MB (28.55 million rows/s., 228.37 MB/s.)
Peak memory usage: 4.65 MiB.


Q7(1.28x for rightPadUTF8): 
Before:
select rightPadUTF8(materialize('hello '), 10, ',') from numbers(100000000) format Null; 
0 rows in set. Elapsed: 4.522 sec. Processed 100.00 million rows, 800.00 MB (22.11 million rows/s., 176.90 MB/s.)
After:
Peak memory usage: 5.01 MiB.
0 rows in set. Elapsed: 3.539 sec. Processed 100.00 million rows, 800.00 MB (28.26 million rows/s., 226.06 MB/s.)
Peak memory usage: 4.65 MiB.

@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-performance Pull request with some performance improvements label Mar 20, 2024
@robot-ch-test-poll4

robot-ch-test-poll4 commented Mar 20, 2024 •

Copy link
Copy Markdown
Contributor

This is an automated comment for commit dd6774e 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
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
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests✅ 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

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Mar 20, 2024
@evillique

Copy link
Copy Markdown
Member

How does this optimization influence execution time if the string contains a non-ASCII character at the end of it, to check the worst-case scenario?

@taiyang-li

Copy link
Copy Markdown
Contributor Author

How does this optimization influence execution time if the string contains a non-ASCII character at the end of it, to check the worst-case scenario?

I understand your worry totally. But the time cost of isAllASCII is trivial. Let's add some cases of non-ASCII character to see how much they have been slowed down.

@taiyang-li

Copy link
Copy Markdown
Contributor Author

@evillique the ci seems not stable. Could you review code changes first?

@taiyang-li

Copy link
Copy Markdown
Contributor Author

@evillique Any comments?

@al13n321 al13n321 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks reasonable, thanks for the improvement!


Wouldn't it be more efficient to do this as part of the function's inner loop instead? I.e. (1) load a block of characters into a simd register, (2) check if all ascii, (3a) use fast ascii path if yes, (3b) use slow utf8 path if no. The (1)+(2) can be identical to the isAllASCII implementation. The (3a) can be reused between the utf8 and the non-utf8 versions of the function. Overall it should be faster because it iterates only once and works for mixed strings.

Some of these functions already do this, but without AVX2/AVX512. Adding AVX2 and AVX512 implementations for these functions would be strictly better than the isAllASCII trick, but is more work, right? Specifically (from cursory reading, expect ~30% to be wrong):

  • LowerUpperUTF8Impl - just needs AVX2 and AVX512.
  • padString:
    • UTF8::countCodePoints needs AVX2 and AVX512
    • skipCodePointsBackward is not even simd
  • substring - skipCodePointsForward and skipCodePointsBackward need simd.
  • reverse - not obvious how to do better than isAllASCII.
  • substringIndex - I don't understand why this needs to be UTF8-aware at all; UTF8 is specifically designed such that regular memmem-like search works correctly (as long as needle is valid UTF8).

But the isAllASCII trick seems like a good default behavior for cases when when we don't have efficient utf8 implementation yet.

Comment thread src/Functions/GatherUtils/Sinks.h Resolved
Comment thread src/Functions/trim.cpp Resolved
Comment thread src/Common/UTF8Helpers.cpp Resolved
Comment thread src/Functions/padString.cpp Resolved
@al13n321 al13n321 self-assigned this May 6, 2024
@al13n321

Copy link
Copy Markdown
Contributor

@al13n321
al13n321 added this pull request to the merge queue May 13, 2024
Merged via the queue into ClickHouse:master with commit 0fd52c3 May 13, 2024
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label May 13, 2024
huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request Nov 13, 2026
The 14 queries of tests/performance/ascii.xml return 10M rows to perf.py's
Python client. On master CI they take 1.0-1.9 s each, and the server spends
82-96% of that time waiting on the network send, with only 75-309 ms of CPU,
so the test mostly measures clickhouse-driver decoding rather than the
ASCII fast paths of the UTF-8 string functions that ClickHouse#61632 added.

Mark the 14 queries disabled (their text and numbers are unchanged) and
append the same 14 calls with format Null as queries 14-27. The row count of
each copy is picked from a 10M..1000M ladder so that its projected CI median
is in 0.2-0.75 s on amd and arm (local server CPU scaled by the CIDB/local
UserTime ratio of the old query).

Related: ClickHouse#129258

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants