petgraph / fixedbitset

A simple bitset container for Rust
https://docs.rs/fixedbitset/
Apache License 2.0
126 stars 47 forks source link

Adding an unchecked version of `union_with` #94

Closed noamteyssier closed 8 months ago

noamteyssier commented 1 year ago

Hey all,

Love the library and am using it in a project where I am performing a very high number of union_with calls on FixedBitSets of equal length of fairly small size (i.e. N $\approx$ 1000).

I know that they are the same length every time so the extra branching in the if statement checking if they are equal sizes is adding overhead that could be cut. Not sure if you all are interested in including, but I found it to be very helpful in my project and made a fairly substantial optimization.

The effect is fairly small for larger bitsets (1M) but is significant once the size gets much smaller.

Here are the benchmarks:

N = 1k

union_with/1k time:   [6.9713 ns 6.9860 ns 7.0021 ns]
Found 3 outliers among 100 measurements (3.00%)
  1 (1.00%) high mild
  2 (2.00%) high severe

union_with_unchecked/1k time:   [4.4989 ns 4.5232 ns 4.5489 ns]
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high severe

N = 1M

union_with/1m           time:   [4.5301 µs 4.5315 µs 4.5329 µs]
Found 12 outliers among 100 measurements (12.00%)
  3 (3.00%) low severe
  4 (4.00%) low mild
  3 (3.00%) high mild
  2 (2.00%) high severe

union_with_unchecked/1m time:   [4.5138 µs 4.5161 µs 4.5184 µs]
Found 7 outliers among 100 measurements (7.00%)
  1 (1.00%) low severe
  4 (4.00%) high mild
  2 (2.00%) high severe
james7132 commented 1 year ago

Looks like rustfmt also failed, can you please run cargo fmt?

james7132 commented 8 months ago

Thinking a bit more on this, I don't think it's a good idea to have this in terms of API consistency, and this can be implemented via FixedBitset::as_mut_slice and FixedBitset::as_slice.