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

Storages: Use std::vector<UInt8> in BitmapFilter #9552

Merged
merged 6 commits into from
Oct 28, 2024

Conversation

JinheLin
Copy link
Contributor

@JinheLin JinheLin commented Oct 23, 2024

What problem does this PR solve?

Issue Number: ref #6834

Problem Summary:

std::vector<bool> is slower than std::vector<UInt8> in most cases, because it cannot be vectorized.

What is changed and how it works?

Benchmark results of std::vector<bool> and std::vector<UInt8>:

./dbms/bench_dbms --benchmark_filter="bitmap*"
2024-10-25T11:38:41+08:00
Running ./dbms/bench_dbms
Run on (72 X 3293.35 MHz CPU s)
CPU Caches:
  L1 Data 32 KiB (x36)
  L1 Instruction 32 KiB (x36)
  L2 Unified 1024 KiB (x36)
  L3 Unified 25344 KiB (x2)
Load Average: 10.61, 9.76, 9.55
--------------------------------------------------------------
Benchmark                    Time             CPU   Iterations
--------------------------------------------------------------
bitmapAndBool           103100 ns       102751 ns         6865
bitmapAndUInt8            3522 ns         3510 ns       215219
bitmapSetRowIDBool       43899 ns        43757 ns        16324
bitmapSetRowIDUInt8      52205 ns        52036 ns        13673
bitmapSetRangeBool       36101 ns        35990 ns        19422
bitmapSetRangeUInt8       1735 ns         1729 ns       344027
bitmapGetRangeBool       24767 ns        24687 ns        28194
bitmapGetRangeUInt8        108 ns          108 ns      7176808

  • Only in the case of bitmapSetRowID* that vector<UInt8> is slightly slower than vector<bool>.

    • vector<bool> is about 1.2 times faster than vector<UInt8>.
    • This is because std::vector<UInt8> cannot be vectorized by the compiler since the memory access is discrete.
    • And vector<bool> is smaller than vector<UInt8>, so it is more cache friendly.
  • In bitmapAnd*, vector<UInt8> is more than 31 times faster than vector<bool>.

  • In bitmapSetRange*, vector<UInt8> is more than 17 times faster than vector<bool>.

  • In bitmapGetRange*, vector<UInt8> is more than 254 times faster than vector<bool>.

  • Memory usage of vector<UInt8> is 8 times higher than vector<bool>. But only about 1MB for a segment with 1 million rows, I think it is acceptable.

Test results of a certain PoC:

Query Using vector<bool> Using vector<UInt8>
1 13.148 11.58
2 10.904 10.244
3 2.709 2.092


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 ti-chi-bot bot added do-not-merge/needs-linked-issue release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 23, 2024
@ti-chi-bot ti-chi-bot bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. do-not-merge/needs-linked-issue labels Oct 25, 2024
@JinheLin JinheLin changed the title WIP: Storages: Use std::vector<UInt8> in BitmapFilter Storages: Use std::vector<UInt8> in BitmapFilter Oct 25, 2024
@ti-chi-bot ti-chi-bot bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 25, 2024
Copy link
Contributor

@Lloyd-Pottiger Lloyd-Pottiger left a comment

Choose a reason for hiding this comment

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

@JinheLin I have pushed a commit to refine other functions and fixed the build of bench_dbms

@ti-chi-bot ti-chi-bot bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Oct 28, 2024
Copy link
Contributor

@JaySon-Huang JaySon-Huang left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

ti-chi-bot bot commented Oct 28, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: JaySon-Huang, Lloyd-Pottiger

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

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [JaySon-Huang,Lloyd-Pottiger]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Oct 28, 2024
Copy link
Contributor

ti-chi-bot bot commented Oct 28, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-10-28 03:58:01.438641081 +0000 UTC m=+235794.277796615: ☑️ agreed by Lloyd-Pottiger.
  • 2024-10-28 06:06:30.871927104 +0000 UTC m=+243503.711082650: ☑️ agreed by JaySon-Huang.

@ti-chi-bot ti-chi-bot bot merged commit b36199e into pingcap:master Oct 28, 2024
5 checks passed
@wuhuizuo
Copy link
Contributor

build failure:

[ 98%] Building CXX object dbms/src/Storages/DeltaMerge/CMakeFiles/delta_merge.dir/ColumnFile/ColumnFile_V3.cpp.o

/Users/pingcap/workspace/bp-tiflash-release-darwin-arm64-qbx9j-build-binaries/source/tiflash/dbms/src/Storages/DeltaMerge/ColumnFile/ColumnFileTinyVectorIndexWriter.cpp:108:29: error: reference to local binding 'col_id' declared in enclosing function 'DB::DM::ColumnFileTinyVectorIndexWriter::buildIndexForFile'

            return [cd.id](http://cd.id/) == col_id;

                            ^

/Users/pingcap/workspace/bp-tiflash-release-darwin-arm64-qbx9j-build-binaries/source/tiflash/dbms/src/Storages/DeltaMerge/ColumnFile/ColumnFileTinyVectorIndexWriter.cpp:104:24: note: 'col_id' declared here

    for (const auto & [col_id, index_infos] : col_indexes)

                       ^

1 error generated.

make[3]: *** [dbms/src/Storages/DeltaMerge/CMakeFiles/delta_merge.dir/ColumnFile/ColumnFileTinyVectorIndexWriter.cpp.o] Error 1

make[3]: *** Waiting for unfinished jobs....

[ 98%] Building CXX object dbms/src/Functions/CMakeFiles/tiflash_functions.dir/bitShiftLeft.cpp.o

make[2]: *** [dbms/src/Storages/DeltaMerge/CMakeFiles/delta_merge.dir/all] Error 2

make[2]: *** Waiting for unfinished jobs....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants