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

perf(common): optimize BitmapIter #8848

Merged
merged 8 commits into from
Mar 29, 2023
Merged

perf(common): optimize BitmapIter #8848

merged 8 commits into from
Mar 29, 2023

Conversation

kwannoel
Copy link
Contributor

@kwannoel kwannoel commented Mar 29, 2023

I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.

What's changed and what's your intention?

  • Before this PR, bitmap iter is shown to be expensive: Tracking: optimize HashAggExecutor #7792 (comment)
  • In this PR, we call it once for each usize, copy it into the iterator, and reference that instead.
  • Save division calls by a factor of usize.
  • Save calls to get_unchecked by a factor of usize.
  • Incur cost of copy usize on to stack for caching reads from memory
  • Incur cost of conditional check of whether to fetch usize from memory or use the one cached.
  • Also fix benchmark inaccuracies.

Overall maybe save about 1% 🤣
Saves about 17% for bitmap iter itself. See below: #8848 (comment).

Checklist For Contributors

  • I have written necessary rustdoc comments
  • I have added necessary unit tests and integration tests
  • I have added fuzzing tests or opened an issue to track them. (Optional, recommended for new SQL features Sqlsmith: Sql feature generation #7934).
  • I have demonstrated that backward compatibility is not broken by breaking changes and created issues to track deprecated features to be removed in the future. (Please refer to the issue)
  • All checks passed in ./risedev check (or alias, ./risedev c)

Checklist For Reviewers

  • I have requested macro/micro-benchmarks as this PR can affect performance substantially, and the results are shown.

Documentation

  • My PR DOES NOT contain user-facing changes.
Click here for Documentation

Types of user-facing changes

Please keep the types that apply to your changes, and remove the others.

  • Installation and deployment
  • Connector (sources & sinks)
  • SQL commands, functions, and operators
  • RisingWave cluster configuration changes
  • Other (please specify in the release note below)

Release note

@kwannoel
Copy link
Contributor Author

kwannoel commented Mar 29, 2023

Before:

noelkwan@Noels-MacBook-Pro benches % cargo bench --bench bitmap -- zeros_iter
   Compiling risingwave_common v0.2.0-alpha (/Users/noelkwan/projects/risingwave/src/common)
    Finished bench [optimized] target(s) in 22.16s
     Running benches/bitmap.rs (/Users/noelkwan/projects/risingwave/target/release/deps/bitmap-1b7d7d1feebf7e97)
zeros_iter              time:   [5.9332 ms 5.9379 ms 5.9430 ms]
                        change: [-0.5865% -0.4578% -0.3226%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 15 outliers among 100 measurements (15.00%)
  11 (11.00%) high mild
  4 (4.00%) high severe


After:

noelkwan@Noels-MacBook-Pro benches % cargo bench --bench bitmap -- zeros_iter
   Compiling risingwave_common v0.2.0-alpha (/Users/noelkwan/projects/risingwave/src/common)
    Finished bench [optimized] target(s) in 22.14s
     Running benches/bitmap.rs (/Users/noelkwan/projects/risingwave/target/release/deps/bitmap-1b7d7d1feebf7e97)
zeros_iter              time:   [4.9172 ms 4.9247 ms 4.9327 ms]
                        change: [-18.040% -17.891% -17.733%] (p = 0.00 < 0.05)
                        Performance has improved.

* Add various granularity: 1, 1000, 1000_000
@kwannoel kwannoel marked this pull request as ready for review March 29, 2023 05:18
@kwannoel kwannoel requested review from wangrunji0408 and lmatz March 29, 2023 05:19
Copy link
Contributor

@wangrunji0408 wangrunji0408 left a comment

Choose a reason for hiding this comment

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

LGTM!

@wangrunji0408
Copy link
Contributor

The code looks correct to me. I can't tell why the test failed. 🥵

Copy link
Contributor

@wangrunji0408 wangrunji0408 left a comment

Choose a reason for hiding this comment

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

We should reload current_usize here, otherwise the cache would be stale.

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.idx += n;
        self.next()
    }

@codecov
Copy link

codecov bot commented Mar 29, 2023

Codecov Report

Merging #8848 (c1bf0a1) into main (a4a46fd) will decrease coverage by 0.01%.
The diff coverage is 96.87%.

@@            Coverage Diff             @@
##             main    #8848      +/-   ##
==========================================
- Coverage   70.80%   70.80%   -0.01%     
==========================================
  Files        1172     1172              
  Lines      194525   194554      +29     
==========================================
+ Hits       137733   137747      +14     
- Misses      56792    56807      +15     
Flag Coverage Δ
rust 70.80% <96.87%> (-0.01%) ⬇️

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

Impacted Files Coverage Δ
src/common/src/buffer/bitmap.rs 96.66% <96.87%> (-0.01%) ⬇️

... and 4 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@kwannoel kwannoel added this pull request to the merge queue Mar 29, 2023
@kwannoel kwannoel changed the title perf(common): reduce calls to get_unchecked perf(common): optimize BitmapIter Mar 29, 2023
Merged via the queue into main with commit 087a415 Mar 29, 2023
@kwannoel kwannoel deleted the kwannoel/opt-bitmap branch March 29, 2023 07:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants