Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize comparision for collation UTF8_BIN and UTF8MB4_BIN #5299

Merged
merged 19 commits into from
Jul 7, 2022

Conversation

solotzg
Copy link
Contributor

@solotzg solotzg commented Jul 6, 2022

What problem does this PR solve?

Issue Number: ref #5294

Problem Summary:

What is changed and how it works?

  • Reduce virtual function call when handling collaction UTF8_BIN and UTF8MB4_BIN
  • Check tailf space first to reduce branches.
  • Optimize comparison related to check equality.

Benchmark

Env:

  • TPCH 10
  • 1 Node
  • limit cpu usage up to 200% by cgroup

SQL

select count(1) from lineitem where l_comment = 'zzle? slyly regular instruc       ';
MySQL [tpch_10]> select count(1) from lineitem where l_comment = 'zzle? slyly regular instruc       ';
+----------+
| count(1) |
+----------+
|        1 |
+----------+
Time(s) Original Optimized NoCollation    
  2.46 1.56 1.48    
  2.41 1.56 1.38    
  2.36 1.47 1.43    
  2.35 1.55 1.4 NoCollation : Original 69.30%
  2.44 1.49 1.41 Optimized : Original 57.54%
AVG 2.404 1.526 1.42 NoCollation : Optimized 7.46%

SQL

select count(1) from lineitem where l_comment != l_comment;
Time(s) Original Optimized NoCollation    
  2.37 1.66 1.96    
  2.25 1.52 1.9    
  2.32 1.66 1.94    
  2.24 1.63 1.94 NoCollation : Original 19.27%
  2.33 1.54 1.91 Optimized : Original 43.70%
AVG 2.302 1.602 1.93 NoCollation : Optimized -16.99%

SQL

select count(1) from lineitem where l_comment < l_comment;
Time(s) Original Optimized NoCollation    
  2.32 2.07 1.91    
  2.27 2.06 1.95    
  2.33 2.07 2.05    
  2.33 2.09 1.99 NoCollation : Original 15.49%
  2.23 2.08 2.04 Optimized : Original 10.70%
AVG 2.296 2.074 1.988 NoCollation : Optimized 4.33%

Analyze

When only checking equality,

__attribute__((always_inline, pure)) inline bool memoryEqual(const char * p1, const char * p2, size_t size) noexcept
performs better than std::memcmp if most parts of source and target string are equal.

  • maybe because libc is not very new.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

None

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Jul 6, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • windtalker
  • yibin87

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jul 6, 2022
@solotzg
Copy link
Contributor Author

solotzg commented Jul 6, 2022

/run-all-tests

@solotzg solotzg added the type/enhancement The issue or PR belongs to an enhancement. label Jul 6, 2022
@solotzg
Copy link
Contributor Author

solotzg commented Jul 6, 2022

/run-all-tests

@sre-bot
Copy link
Collaborator

sre-bot commented Jul 6, 2022

Coverage for changed files

Filename                                   Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Columns/ColumnConst.h                           46                30    34.78%          43                27    37.21%         113                74    34.51%           2                 2     0.00%
Functions/CollationOperatorOptimized.h          60                47    21.67%          14                 8    42.86%         170               139    18.24%          32                24    25.00%
Functions/FunctionsComparison.h                612               374    38.89%          62                33    46.77%         958               605    36.85%         482               339    29.67%
Storages/Transaction/Collator.cpp              200                54    73.00%          32                 5    84.38%         427               124    70.96%         168                70    58.33%
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                          918               505    44.99%         151                73    51.66%        1668               942    43.53%         684               435    36.40%

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
18340      9664             47.31%    205822  96867        52.94%

full coverage report (for internal network access only)

@windtalker
Copy link
Contributor

Can you also add some benchmark result for TiDB cluster with new collation disabled?

// Remove tail space
__attribute__((flatten, always_inline, pure)) inline std::string_view RightTrim(const std::string_view & v)
{
size_t end = v.find_last_not_of(' ');
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if other space characters need to be handled here.

Copy link
Contributor Author

@solotzg solotzg Jul 6, 2022

Choose a reason for hiding this comment

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

According to MySQL::Doc::string-comparison-functions.

  • In particular, trailing spaces are always significant. This differs from comparisons performed with the = operator, for which the significance of trailing spaces in nonbinary strings (CHAR, VARCHAR, and TEXT values) depends on the pad attribute of the collation used for the comparison. For more information, see Trailing Space Handling in Comparisons.
mysql> SET NAMES utf8mb4 COLLATE utf8mb4_bin;
mysql> SELECT 'a ' = 'a';
+------------+
| 'a ' = 'a' |
+------------+
|          1 |
+------------+
mysql> SET NAMES utf8mb4 COLLATE utf8mb4_0900_bin;
mysql> SELECT 'a ' = 'a';
+------------+
| 'a ' = 'a' |
+------------+
|          0 |
+------------+

@solotzg
Copy link
Contributor Author

solotzg commented Jul 6, 2022

/run-all-tests

@sre-bot
Copy link
Collaborator

sre-bot commented Jul 6, 2022

Coverage for changed files

Filename                                   Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Columns/ColumnConst.h                           46                30    34.78%          43                27    37.21%         113                74    34.51%           2                 2     0.00%
Functions/CollationOperatorOptimized.h          42                25    40.48%          11                 5    54.55%         109                76    30.28%          26                13    50.00%
Functions/FunctionsComparison.h                612               374    38.89%          62                33    46.77%         958               605    36.85%         482               339    29.67%
Storages/Transaction/Collator.cpp              200                54    73.00%          32                 5    84.38%         427               124    70.96%         168                70    58.33%
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                          900               483    46.33%         148                70    52.70%        1607               879    45.30%         678               424    37.46%

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
18337      9661             47.31%    205761  96823        52.94%

full coverage report (for internal network access only)

@solotzg
Copy link
Contributor Author

solotzg commented Jul 7, 2022

Can you also add some benchmark result for TiDB cluster with new collation disabled?

Done.

Copy link
Contributor

@windtalker windtalker left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Jul 7, 2022
@sre-bot
Copy link
Collaborator

sre-bot commented Jul 7, 2022

Coverage for changed files

Filename                                   Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Columns/ColumnConst.h                           46                29    36.96%          43                26    39.53%         113                73    35.40%           2                 2     0.00%
Functions/CollationOperatorOptimized.h          42                25    40.48%          11                 5    54.55%         109                76    30.28%          26                13    50.00%
Functions/FunctionsComparison.h                604               298    50.66%          63                28    55.56%         949               511    46.15%         476               276    42.02%
Storages/Transaction/Collator.cpp              200                54    73.00%          32                 5    84.38%         426               124    70.89%         168                70    58.33%
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                          892               406    54.48%         149                64    57.05%        1597               784    50.91%         672               361    46.28%

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
18441      9622             47.82%    207703  96612        53.49%

full coverage report (for internal network access only)

@solotzg solotzg requested a review from zanmato1984 July 7, 2022 09:44
@ti-chi-bot ti-chi-bot removed the status/can-merge Indicates a PR has been approved by a committer. label Jul 7, 2022
@solotzg
Copy link
Contributor Author

solotzg commented Jul 7, 2022

/merge

@ti-chi-bot
Copy link
Member

@solotzg: It seems you want to merge this PR, I will help you trigger all the tests:

/run-all-tests

You only need to trigger /merge once, and if the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

If you have any questions about the PR merge process, please refer to pr process.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 6a1f4dd

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Jul 7, 2022
Copy link
Contributor

@zanmato1984 zanmato1984 left a comment

Choose a reason for hiding this comment

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

LGTM

@solotzg
Copy link
Contributor Author

solotzg commented Jul 7, 2022

/unhold

@ti-chi-bot ti-chi-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jul 7, 2022
@sre-bot
Copy link
Collaborator

sre-bot commented Jul 7, 2022

Coverage for changed files

Filename                                   Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Columns/ColumnConst.h                           46                29    36.96%          43                26    39.53%         113                73    35.40%           2                 2     0.00%
Functions/CollationOperatorOptimized.h          42                25    40.48%          11                 5    54.55%         109                76    30.28%          26                13    50.00%
Functions/FunctionsComparison.h                604               300    50.33%          63                28    55.56%         949               520    45.21%         476               281    40.97%
Storages/Transaction/Collator.cpp              200                54    73.00%          32                 5    84.38%         426               124    70.89%         168                70    58.33%
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                          892               408    54.26%         149                64    57.05%        1597               793    50.34%         672               366    45.54%

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
18446      9624             47.83%    207747  96634        53.48%

full coverage report (for internal network access only)

@sre-bot
Copy link
Collaborator

sre-bot commented Jul 7, 2022

Coverage for changed files

Filename                                   Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Columns/ColumnConst.h                           46                29    36.96%          43                26    39.53%         113                73    35.40%           2                 2     0.00%
Functions/CollationOperatorOptimized.h          42                25    40.48%          11                 5    54.55%         109                76    30.28%          26                13    50.00%
Functions/FunctionsComparison.h                604               298    50.66%          63                28    55.56%         949               511    46.15%         476               279    41.39%
Storages/Transaction/Collator.cpp              200                54    73.00%          32                 5    84.38%         426               124    70.89%         168                70    58.33%
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                          892               406    54.48%         149                64    57.05%        1597               784    50.91%         672               364    45.83%

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
18447      9625             47.82%    207759  96631        53.49%

full coverage report (for internal network access only)

@ti-chi-bot
Copy link
Member

@solotzg: Your PR was out of date, I have automatically updated it for you.

At the same time I will also trigger all tests for you:

/run-all-tests

If the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@sre-bot
Copy link
Collaborator

sre-bot commented Jul 7, 2022

Coverage for changed files

Filename                                   Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Columns/ColumnConst.h                           46                29    36.96%          43                26    39.53%         113                73    35.40%           2                 2     0.00%
Functions/CollationOperatorOptimized.h          42                25    40.48%          11                 5    54.55%         109                76    30.28%          26                13    50.00%
Functions/FunctionsComparison.h                604               297    50.83%          63                28    55.56%         949               510    46.26%         476               274    42.44%
Storages/Transaction/Collator.cpp              200                54    73.00%          32                 5    84.38%         426               124    70.89%         168                70    58.33%
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                          892               405    54.60%         149                64    57.05%        1597               783    50.97%         672               359    46.58%

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
18452      9618             47.88%    207820  96578        53.53%

full coverage report (for internal network access only)

@ti-chi-bot ti-chi-bot merged commit 97342db into pingcap:master Jul 7, 2022
@solotzg solotzg deleted the optimize-collation-comp branch July 7, 2022 11:57
solotzg added a commit to solotzg/tiflash that referenced this pull request Jul 12, 2022
windtalker pushed a commit that referenced this pull request Jul 12, 2022
Lloyd-Pottiger pushed a commit to Lloyd-Pottiger/tiflash that referenced this pull request Jul 12, 2022
…s in README (pingcap#5182)

close pingcap#5172, ref pingcap#5178

Enhancement: add a integrated test on DDL module (pingcap#5130)

ref pingcap#5129

Revert "Revise default background threads size" (pingcap#5176)

close pingcap#5177

chore: remove extra dyn cast (pingcap#5186)

close pingcap#5185

Add MPPReceiverSet, which includes ExchangeReceiver and CoprocessorReader (pingcap#5175)

ref pingcap#5095

DDL: Use Column Name Instead of Offset to Find the common handle cluster index (pingcap#5166)

close pingcap#5154

Add random failpoint in critical paths (pingcap#4876)

close pingcap#4807

Segment test framework (pingcap#5150)

close pingcap#5151

optimize ps v3 restore (pingcap#5163)

ref pingcap#4914

Fix build failed (pingcap#5196)

close pingcap#5195

feat: delta tree dispatching (pingcap#5199)

close pingcap#5200

feat: introduce specialized API to write fixed length data rapidly (pingcap#5181)

close pingcap#5183

Add gtest for Limit, TopN, Projection (pingcap#5187) (pingcap#5188)

close pingcap#5187

add `MPPTask::handleError()` (pingcap#5202)

ref pingcap#5095

Check result of starting grpc server (pingcap#5257)

close pingcap#5255

feat: add optimized routines for aarch64 (pingcap#5231)

close pingcap#5240

fix: aarch64-quick-fix (pingcap#5259)

close pingcap#5260

Update client-c to support ipv6 (pingcap#5270)

close pingcap#5247

upgrade prometheus-cpp to v1.0.1 (pingcap#5279)

ref pingcap#2103, close pingcap#5278

Fix README type error (pingcap#5273)

ref pingcap#5178

fix(cmake): make sure libc++ is utilized by tiflash-proxy (pingcap#5281)

close pingcap#5282

fix the wrong order of execution summary for list based executors (pingcap#5242)

close pingcap#5241

Schema: allow loading empty schema diff when the version grows up. (pingcap#5245)

close pingcap#5244

Optimize apply speed under heavy write pressure (pingcap#4883)

ref pingcap#4728

update proxy to raftstore-proxy-6.2 (pingcap#5287)

ref pingcap#4982

Flush segment cache when doing the compaction (pingcap#5284)

close pingcap#5179

metrics: Fix incorrect metrics for delta_merge tasks (pingcap#5061)

close pingcap#5055

dep: upgrade jemalloc (pingcap#5197)

close pingcap#5258

*: TiFlash pagectl/dttool use only-decryption mode (pingcap#5271)

close pingcap#5122

suppresion false positive report from tsan (pingcap#5303)

close pingcap#5088

Refine test framework code and tests (pingcap#5261)

close pingcap#5262

feat: add logical cpu cores and memory into grafana (pingcap#5124)

close pingcap#3821

Implement TimeToSec function push down (pingcap#5235)

close pingcap#5116

feat: implement shiftRight function push down (pingcap#5156)

close pingcap#5100

schema : make update to partition tables when 'set tiflash replica' (pingcap#5267)

close pingcap#5266

Replace initializer_list with vector for planner test framework (pingcap#5307)

close pingcap#5295

KVStore: decouple flush region and CompactLog with a new FFI fn_try_flush_data (pingcap#5283)

ref pingcap#5170

refine error message in mpptask (pingcap#5304)

ref pingcap#5095

Implement ReverseUTF8/Reverse function push down (pingcap#5233)

close pingcap#5111

Optimize comparision for collation `UTF8_BIN` and `UTF8MB4_BIN` (pingcap#5299)

ref pingcap#5294

feat : support set tiflash mode ddl action (pingcap#5256)

ref pingcap#5252

Add non-blocking functions for MPMCQueue (pingcap#5311)

close pingcap#5310

add random segment test for CI weekly (pingcap#5300)

close pingcap#5301

*: tidy FunctionString.cpp (pingcap#5312)

close pingcap#5313

ci: fix check-license github action (pingcap#5318)

close pingcap#5317

update proxy to raftstore-proxy-6.2 (pingcap#5316)

ref pingcap#4982

Change one `additional_input_at_end` to many streams in `ParallelInputsProcessor`  (pingcap#5274)

close pingcap#4856, close pingcap#5263

support fine grained shuffle for window function (pingcap#5048)

close pingcap#5142

feat: pushdown get_format into TiFlash (pingcap#5269)

close pingcap#5115

fix: format throw data truncated error (pingcap#5272)

close pingcap#4891

Print content of columns for gtest (pingcap#5243)

close pingcap#5203

*: also enable O3 for aarch64 (pingcap#5338)

close pingcap#5342

Add debug image build target for CentOS7 (pingcap#5344)

close pingcap#5343

*: mini refactor (pingcap#5326)

close pingcap#4739

Refactor initialize of background pool (pingcap#5190)

close pingcap#5189

delete copy/move ctor of MPMCQueue explicitly (pingcap#5328)

close pingcap#5329

Introduce proxy_server and new-mock-engine-store (pingcap#5319)

ref pingcap#5170

fix: incorrect uptime in grafana panel

Signed-off-by: Lloyd-Pottiger <[email protected]>
Lloyd-Pottiger pushed a commit to Lloyd-Pottiger/tiflash that referenced this pull request Jul 19, 2022
solotzg added a commit to solotzg/tiflash that referenced this pull request Sep 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2. type/enhancement The issue or PR belongs to an enhancement. type/performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants