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

Branchless array-bitmap operations #232

Merged
merged 1 commit into from
Aug 15, 2022

Conversation

RaduBerinde
Copy link
Contributor

@RaduBerinde RaduBerinde commented Aug 11, 2022

This commit implements a faster version of Vec::retain for array
stores.

Fixes #206.

@RaduBerinde
Copy link
Contributor Author

pairwise_and benchmarks (ran on a laptop, there might be some variability)

group                                             and-after                               and-before
-----                                             ---------                               ----------
pairwise_and/assign_own/census-income             1.00  1547.3±176.40µs        ? ?/sec    1.18  1832.2±83.92µs        ? ?/sec
pairwise_and/assign_own/census-income_srt         1.00  746.6±105.25µs        ? ?/sec     1.09  815.0±131.75µs        ? ?/sec
pairwise_and/assign_own/census1881                1.00   196.4±34.79µs        ? ?/sec     1.15   225.7±86.00µs        ? ?/sec
pairwise_and/assign_own/census1881_srt            1.00   138.6±19.81µs        ? ?/sec     1.18   164.2±69.28µs        ? ?/sec
pairwise_and/assign_own/weather_sept_85           1.00      6.3±0.46ms        ? ?/sec     1.25      7.8±1.06ms        ? ?/sec
pairwise_and/assign_own/weather_sept_85_srt       1.64      2.1±0.33ms        ? ?/sec     1.00  1298.5±80.51µs        ? ?/sec
pairwise_and/assign_own/wikileaks-noquotes        1.00   344.7±35.29µs        ? ?/sec     1.03   355.5±58.25µs        ? ?/sec
pairwise_and/assign_own/wikileaks-noquotes_srt    1.04   155.3±20.47µs        ? ?/sec     1.00   150.0±22.53µs        ? ?/sec
pairwise_and/assign_ref/census-income             1.00  1507.4±82.84µs        ? ?/sec     1.30  1953.1±169.93µs        ? ?/sec
pairwise_and/assign_ref/census-income_srt         1.00   765.1±73.44µs        ? ?/sec     1.07   820.3±83.75µs        ? ?/sec
pairwise_and/assign_ref/census1881                1.00   199.3±47.05µs        ? ?/sec     1.20   239.5±77.28µs        ? ?/sec
pairwise_and/assign_ref/census1881_srt            1.00   136.3±17.61µs        ? ?/sec     1.47  200.9±193.46µs        ? ?/sec
pairwise_and/assign_ref/weather_sept_85           1.00      6.4±0.23ms        ? ?/sec     1.13      7.3±0.34ms        ? ?/sec
pairwise_and/assign_ref/weather_sept_85_srt       1.00  1480.8±72.30µs        ? ?/sec     1.08  1597.3±319.51µs        ? ?/sec
pairwise_and/assign_ref/wikileaks-noquotes        1.00   359.5±13.45µs        ? ?/sec     1.07   384.2±60.08µs        ? ?/sec
pairwise_and/assign_ref/wikileaks-noquotes_srt    1.09   166.1±21.28µs        ? ?/sec     1.00   152.6±21.00µs        ? ?/sec
pairwise_and/len/census-income                    1.00  1276.6±10.52µs        ? ?/sec     1.05  1342.2±46.28µs        ? ?/sec
pairwise_and/len/census-income_srt                1.00    497.1±6.21µs        ? ?/sec     1.07   531.3±75.90µs        ? ?/sec
pairwise_and/len/census1881                       1.00     72.3±2.55µs        ? ?/sec     1.05     75.9±6.18µs        ? ?/sec
pairwise_and/len/census1881_srt                   1.00     23.7±0.10µs        ? ?/sec     1.05     24.8±1.90µs        ? ?/sec
pairwise_and/len/weather_sept_85                  1.02      6.0±0.34ms        ? ?/sec     1.00      5.9±0.15ms        ? ?/sec
pairwise_and/len/weather_sept_85_srt              1.00   930.8±33.81µs        ? ?/sec     1.03  960.8±102.87µs        ? ?/sec
pairwise_and/len/wikileaks-noquotes               1.00    206.8±7.62µs        ? ?/sec     1.02   210.5±12.79µs        ? ?/sec
pairwise_and/len/wikileaks-noquotes_srt           1.07     65.0±0.33µs        ? ?/sec     1.00     60.9±0.40µs        ? ?/sec
pairwise_and/own_own/census-income                1.00  1364.0±72.86µs        ? ?/sec     1.37  1874.1±223.03µs        ? ?/sec
pairwise_and/own_own/census-income_srt            1.00   725.6±48.50µs        ? ?/sec     1.00   722.9±28.92µs        ? ?/sec
pairwise_and/own_own/census1881                   1.15   158.6±51.05µs        ? ?/sec     1.00   137.6±19.75µs        ? ?/sec
pairwise_and/own_own/census1881_srt               1.00   118.9±12.39µs        ? ?/sec     1.11   132.0±23.11µs        ? ?/sec
pairwise_and/own_own/weather_sept_85              1.00      5.9±0.09ms        ? ?/sec     1.19      7.1±0.39ms        ? ?/sec
pairwise_and/own_own/weather_sept_85_srt          1.01  1287.4±66.76µs        ? ?/sec     1.00  1278.1±114.94µs        ? ?/sec
pairwise_and/own_own/wikileaks-noquotes           1.03   331.6±22.24µs        ? ?/sec     1.00   322.1±30.90µs        ? ?/sec
pairwise_and/own_own/wikileaks-noquotes_srt       1.00   148.9±18.09µs        ? ?/sec     1.03   153.9±49.09µs        ? ?/sec
pairwise_and/own_ref/census-income                1.00  1477.2±460.99µs        ? ?/sec    1.31  1937.5±185.26µs        ? ?/sec
pairwise_and/own_ref/census-income_srt            1.00  774.7±246.16µs        ? ?/sec     1.11  860.3±562.70µs        ? ?/sec
pairwise_and/own_ref/census1881                   1.00  225.9±473.53µs        ? ?/sec     1.54  347.9±1538.57µs        ? ?/sec
pairwise_and/own_ref/census1881_srt               1.00   124.7±23.87µs        ? ?/sec     1.66  206.8±150.97µs        ? ?/sec
pairwise_and/own_ref/weather_sept_85              1.00      6.2±0.09ms        ? ?/sec     1.16      7.2±0.32ms        ? ?/sec
pairwise_and/own_ref/weather_sept_85_srt          1.00  1373.7±73.57µs        ? ?/sec     1.00  1369.2±253.72µs        ? ?/sec
pairwise_and/own_ref/wikileaks-noquotes           1.02   346.4±29.93µs        ? ?/sec     1.00   341.1±38.03µs        ? ?/sec
pairwise_and/own_ref/wikileaks-noquotes_srt       1.00   139.9±13.89µs        ? ?/sec     1.16   162.0±38.45µs        ? ?/sec
pairwise_and/ref_own/census-income                1.00  1509.3±231.43µs        ? ?/sec    1.22  1836.1±147.68µs        ? ?/sec
pairwise_and/ref_own/census-income_srt            1.00  775.2±260.99µs        ? ?/sec     1.07  825.7±302.85µs        ? ?/sec
pairwise_and/ref_own/census1881                   1.49  345.3±1393.00µs        ? ?/sec    1.00  231.8±283.64µs        ? ?/sec
pairwise_and/ref_own/census1881_srt               1.00   131.6±29.47µs        ? ?/sec     1.28   168.0±77.96µs        ? ?/sec
pairwise_and/ref_own/weather_sept_85              1.00      6.1±0.07ms        ? ?/sec     1.25      7.6±0.64ms        ? ?/sec
pairwise_and/ref_own/weather_sept_85_srt          1.02  1393.5±89.71µs        ? ?/sec     1.00  1366.1±248.30µs        ? ?/sec
pairwise_and/ref_own/wikileaks-noquotes           1.00   335.8±25.25µs        ? ?/sec     1.10   368.5±65.79µs        ? ?/sec
pairwise_and/ref_own/wikileaks-noquotes_srt       1.11   153.6±20.55µs        ? ?/sec     1.00   138.3±19.79µs        ? ?/sec
pairwise_and/ref_ref/census-income                1.00  1716.7±50.54µs        ? ?/sec     1.19      2.0±0.01ms        ? ?/sec
pairwise_and/ref_ref/census-income_srt            1.00   787.1±79.25µs        ? ?/sec     1.19  936.6±266.40µs        ? ?/sec
pairwise_and/ref_ref/census1881                   1.00   166.1±13.16µs        ? ?/sec     1.14   188.6±49.73µs        ? ?/sec
pairwise_and/ref_ref/census1881_srt               1.00    126.3±5.87µs        ? ?/sec     1.19   150.6±33.74µs        ? ?/sec
pairwise_and/ref_ref/weather_sept_85              1.00      6.9±0.09ms        ? ?/sec     1.16      8.0±0.42ms        ? ?/sec
pairwise_and/ref_ref/weather_sept_85_srt          1.00  1535.8±87.81µs        ? ?/sec     1.00  1541.5±250.87µs        ? ?/sec
pairwise_and/ref_ref/wikileaks-noquotes           1.07   376.8±22.25µs        ? ?/sec     1.00   353.6±36.09µs        ? ?/sec
pairwise_and/ref_ref/wikileaks-noquotes_srt       1.01   149.6±12.77µs        ? ?/sec     1.00   147.4±16.13µs        ? ?/sec

This commit implements a faster version of `Vec::retain` for array
stores.

Fixes RoaringBitmap#206.
@RaduBerinde RaduBerinde force-pushed the no-branch-array-bitmap branch from 9bcc178 to 5d8627c Compare August 11, 2022 18:21
Copy link
Member

@Kerollmops Kerollmops left a comment

Choose a reason for hiding this comment

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

This looks very good! Thank you! It's impressive how a simple impl like that can be faster than the standard impl...

bors merge

@bors
Copy link
Contributor

bors bot commented Aug 15, 2022

Build succeeded:

@bors bors bot merged commit 365a6b6 into RoaringBitmap:master Aug 15, 2022
not-jan pushed a commit to not-jan/roaring-rs that referenced this pull request Aug 31, 2022
232: Branchless array-bitmap operations r=Kerollmops a=RaduBerinde

This commit implements a faster version of `Vec::retain` for array
stores.

Fixes RoaringBitmap#206.

Co-authored-by: Radu Berinde <[email protected]>
@@ -205,6 +205,23 @@ impl ArrayStore {
pub fn as_slice(&self) -> &[u16] {
&self.vec
}

/// Retains only the elements specified by the predicate.
pub fn retain(&mut self, mut f: impl FnMut(u16) -> bool) {
Copy link

Choose a reason for hiding this comment

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

Should this be unsafe? If the supplied f doesn't uphold the invariant

// SAFETY: pos is always at most i because f(val) as usize is at most 1.

I suspect this will lead to undefined behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

f(val) is bool

Copy link

Choose a reason for hiding this comment

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

Ooops didn't look at the return type in the signature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Branchless array-bitmap scalar operations
3 participants