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

core/region: optimize the logic of randomly selecting regions within a single range #8211

Merged
merged 4 commits into from
May 23, 2024

Conversation

JmPotato
Copy link
Member

@JmPotato JmPotato commented May 23, 2024

What problem does this PR solve?

Issue Number: ref #7897.

What is changed and how does it work?

In the case of only one key range, follow the fast path logic to avoid unnecessary random and loop operations.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
> benchstat master-6b8d22779.txt new-7c6603926.txt
goos: darwin
goarch: arm64
pkg: github.com/tikv/pd/pkg/core
                                                              │ master-6b8d22779.txt │           new-7c6603926.txt           │
                                                              │        sec/op        │     sec/op      vs base               │
RandomRegion/random_region_whole_range_with_size_10-8                   283.2n ±  2%    244.4n ±   4%  -13.73% (p=0.002 n=6)
RandomRegion/random_regions_whole_range_with_size_10-8                  460.8n ±  5%    404.5n ±   0%  -12.22% (p=0.002 n=6)
RandomRegion/random_region_single_range_with_size_10-8                  364.5n ±  2%    329.6n ±   2%   -9.55% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_10-8                1532.0n ±  1%    560.4n ±   4%  -63.42% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_100-8                  319.5n ±  5%    238.8n ±   4%  -25.27% (p=0.002 n=6)
RandomRegion/random_regions_whole_range_with_size_100-8                 490.0n ±  0%    407.5n ±   2%  -16.83% (p=0.002 n=6)
RandomRegion/random_region_single_range_with_size_100-8                 447.2n ±  1%    404.6n ±   1%   -9.54% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_100-8               2241.5n ±  2%    578.3n ±   0%  -74.20% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_1000-8                 385.2n ±  1%    274.5n ±   3%  -28.72% (p=0.002 n=6)
RandomRegion/random_regions_whole_range_with_size_1000-8                732.0n ±  0%    592.4n ±   1%  -19.08% (p=0.002 n=6)
RandomRegion/random_region_single_range_with_size_1000-8                564.7n ±  2%    516.6n ±   0%   -8.53% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_1000-8              3371.0n ±  0%    892.4n ±   1%  -73.53% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_10000-8                514.0n ±  5%    364.0n ±   5%  -29.19% (p=0.002 n=6)
RandomRegion/random_regions_whole_range_with_size_10000-8               1.250µ ±  3%    1.005µ ±   0%  -19.60% (p=0.002 n=6)
RandomRegion/random_region_single_range_with_size_10000-8               750.0n ±  0%    692.2n ±   1%   -7.71% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_10000-8              4.884µ ±  1%    1.495µ ±   0%  -69.39% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_100000-8               801.8n ±  0%    609.5n ±   2%  -23.99% (p=0.002 n=6)
RandomRegion/random_regions_whole_range_with_size_100000-8              3.711µ ±  1%    3.185µ ±   5%  -14.18% (p=0.002 n=6)
RandomRegion/random_region_single_range_with_size_100000-8              1.130µ ±  5%    1.067µ ±   2%   -5.58% (p=0.030 n=6)
RandomRegion/random_regions_single_range_with_size_100000-8             7.902µ ±  6%    3.614µ ±   5%  -54.26% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_1000000-8             1032.5n ±  4%    821.7n ±   3%  -20.42% (p=0.002 n=6)
RandomRegion/random_regions_whole_range_with_size_1000000-8             6.144µ ±  3%    5.815µ ±   2%   -5.36% (p=0.002 n=6)
RandomRegion/random_region_single_range_with_size_1000000-8             1.518µ ±  1%    1.433µ ±   2%   -5.57% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_1000000-8           12.495µ ±  3%    7.188µ ±   2%  -42.47% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_10000000-8             9.834µ ± 31%   11.163µ ±   7%        ~ (p=0.093 n=6)
RandomRegion/random_regions_whole_range_with_size_10000000-8            92.83µ ± 13%   102.33µ ±   6%  +10.23% (p=0.004 n=6)
RandomRegion/random_region_single_range_with_size_10000000-8            7.853µ ± 22%   10.106µ ± 114%        ~ (p=0.132 n=6)
RandomRegion/random_regions_single_range_with_size_10000000-8           65.62µ ±  7%    48.39µ ±  46%        ~ (p=0.065 n=6)
geomean                                                                 1.828µ          1.321µ         -27.75%

                                                              │ master-6b8d22779.txt │          new-7c6603926.txt          │
                                                              │         B/op         │    B/op     vs base                 │
RandomRegion/random_region_whole_range_with_size_10-8                     416.0 ± 0%   416.0 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_regions_whole_range_with_size_10-8                    488.0 ± 0%   488.0 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_region_single_range_with_size_10-8                    424.0 ± 0%   416.0 ± 0%   -1.89% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_10-8                   568.0 ± 0%   488.0 ± 0%  -14.08% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_100-8                    416.0 ± 0%   416.0 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_regions_whole_range_with_size_100-8                   488.0 ± 0%   488.0 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_region_single_range_with_size_100-8                   424.0 ± 0%   416.0 ± 0%   -1.89% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_100-8                  568.0 ± 0%   488.0 ± 0%  -14.08% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_1000-8                   416.0 ± 0%   416.0 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_regions_whole_range_with_size_1000-8                  488.0 ± 0%   488.0 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_region_single_range_with_size_1000-8                  424.0 ± 0%   416.0 ± 0%   -1.89% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_1000-8                 568.0 ± 0%   488.0 ± 0%  -14.08% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_10000-8                  416.0 ± 0%   416.0 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_regions_whole_range_with_size_10000-8                 488.0 ± 0%   488.0 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_region_single_range_with_size_10000-8                 424.0 ± 0%   416.0 ± 0%   -1.89% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_10000-8                568.0 ± 0%   488.0 ± 0%  -14.08% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_100000-8                 416.0 ± 0%   416.0 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_regions_whole_range_with_size_100000-8                488.0 ± 0%   488.0 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_region_single_range_with_size_100000-8                424.0 ± 0%   416.0 ± 0%   -1.89% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_100000-8               568.0 ± 0%   488.0 ± 0%  -14.08% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_1000000-8                416.0 ± 0%   416.0 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_regions_whole_range_with_size_1000000-8               488.0 ± 0%   488.0 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_region_single_range_with_size_1000000-8               424.0 ± 0%   416.0 ± 0%   -1.89% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_1000000-8              568.0 ± 0%   488.0 ± 0%  -14.08% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_10000000-8               416.0 ± 0%   416.0 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_regions_whole_range_with_size_10000000-8              488.0 ± 0%   488.0 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_region_single_range_with_size_10000000-8              424.0 ± 0%   416.0 ± 0%   -1.89% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_10000000-8             568.0 ± 0%   488.0 ± 0%  -14.08% (p=0.002 n=6)
geomean                                                                   470.2        450.6        -4.18%
¹ all samples are equal

                                                              │ master-6b8d22779.txt │          new-7c6603926.txt          │
                                                              │      allocs/op       │ allocs/op   vs base                 │
RandomRegion/random_region_whole_range_with_size_10-8                     4.000 ± 0%   4.000 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_regions_whole_range_with_size_10-8                    4.000 ± 0%   4.000 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_region_single_range_with_size_10-8                    5.000 ± 0%   4.000 ± 0%  -20.00% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_10-8                  14.000 ± 0%   4.000 ± 0%  -71.43% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_100-8                    4.000 ± 0%   4.000 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_regions_whole_range_with_size_100-8                   4.000 ± 0%   4.000 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_region_single_range_with_size_100-8                   5.000 ± 0%   4.000 ± 0%  -20.00% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_100-8                 14.000 ± 0%   4.000 ± 0%  -71.43% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_1000-8                   4.000 ± 0%   4.000 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_regions_whole_range_with_size_1000-8                  4.000 ± 0%   4.000 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_region_single_range_with_size_1000-8                  5.000 ± 0%   4.000 ± 0%  -20.00% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_1000-8                14.000 ± 0%   4.000 ± 0%  -71.43% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_10000-8                  4.000 ± 0%   4.000 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_regions_whole_range_with_size_10000-8                 4.000 ± 0%   4.000 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_region_single_range_with_size_10000-8                 5.000 ± 0%   4.000 ± 0%  -20.00% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_10000-8               14.000 ± 0%   4.000 ± 0%  -71.43% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_100000-8                 4.000 ± 0%   4.000 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_regions_whole_range_with_size_100000-8                4.000 ± 0%   4.000 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_region_single_range_with_size_100000-8                5.000 ± 0%   4.000 ± 0%  -20.00% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_100000-8              14.000 ± 0%   4.000 ± 0%  -71.43% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_1000000-8                4.000 ± 0%   4.000 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_regions_whole_range_with_size_1000000-8               4.000 ± 0%   4.000 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_region_single_range_with_size_1000000-8               5.000 ± 0%   4.000 ± 0%  -20.00% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_1000000-8             14.000 ± 0%   4.000 ± 0%  -71.43% (p=0.002 n=6)
RandomRegion/random_region_whole_range_with_size_10000000-8               4.000 ± 0%   4.000 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_regions_whole_range_with_size_10000000-8              4.000 ± 0%   4.000 ± 0%        ~ (p=1.000 n=6) ¹
RandomRegion/random_region_single_range_with_size_10000000-8              5.000 ± 0%   4.000 ± 0%  -20.00% (p=0.002 n=6)
RandomRegion/random_regions_single_range_with_size_10000000-8            14.000 ± 0%   4.000 ± 0%  -71.43% (p=0.002 n=6)
geomean                                                                   5.785        4.000       -30.86%
¹ all samples are equal

Release note

None.

@JmPotato JmPotato requested review from nolouch and HuSharp May 23, 2024 04:52
Copy link
Contributor

ti-chi-bot bot commented May 23, 2024

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • HuSharp
  • nolouch

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 bot added release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 23, 2024
@JmPotato JmPotato force-pushed the refine_randome_regions branch 2 times, most recently from f40396d to f3c16af Compare May 23, 2024 05:01
pkg/core/region_tree.go Outdated Show resolved Hide resolved
@JmPotato JmPotato requested a review from HuSharp May 23, 2024 05:09
Signed-off-by: JmPotato <[email protected]>
Copy link

codecov bot commented May 23, 2024

Codecov Report

Attention: Patch coverage is 82.14286% with 5 lines in your changes are missing coverage. Please review.

Project coverage is 77.29%. Comparing base (6b8d227) to head (7c66039).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8211      +/-   ##
==========================================
- Coverage   77.29%   77.29%   -0.01%     
==========================================
  Files         471      471              
  Lines       61436    61445       +9     
==========================================
+ Hits        47486    47492       +6     
+ Misses      10392    10387       -5     
- Partials     3558     3566       +8     
Flag Coverage Δ
unittests 77.29% <82.14%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@ti-chi-bot ti-chi-bot bot added the status/LGT1 Indicates that a PR has LGTM 1. label May 23, 2024
@ti-chi-bot ti-chi-bot bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels May 23, 2024
@JmPotato
Copy link
Member Author

/merge

Copy link
Contributor

ti-chi-bot bot commented May 23, 2024

@JmPotato: 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.

Copy link
Contributor

ti-chi-bot bot commented May 23, 2024

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

Commit hash: 7c66039

@ti-chi-bot ti-chi-bot bot added the status/can-merge Indicates a PR has been approved by a committer. label May 23, 2024
@ti-chi-bot ti-chi-bot bot merged commit 4cd42b3 into tikv:master May 23, 2024
19 checks passed
@JmPotato JmPotato deleted the refine_randome_regions branch May 23, 2024 09:42
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/M Denotes a PR that changes 30-99 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants