Small performance boost.

This commit is contained in:
2018-01-12 18:17:59 +01:00
parent f37cc5efa6
commit ae6e292c9d
3 changed files with 49 additions and 25 deletions

View File

@@ -17,5 +17,8 @@ serde_test = "1.0"
name = "bench" name = "bench"
harness = false harness = false
[lib]
bench = false
[profile.release] [profile.release]
lto = true lto = true

View File

@@ -17,16 +17,27 @@ fn default_set<C: FromIterator<u64>>(n: usize) -> C {
(0..n).map(|_| rng.next_u64()).collect() (0..n).map(|_| rng.next_u64()).collect()
} }
fn contains(data: &[u64]) {
let set = BitSet::new();
for v in data {
set.contains(v);
}
}
fn insert(data: &[u64]) {
let mut set = BitSet::new();
for v in data {
set.insert(*v);
}
}
fn criterion_benchmark(c: &mut Criterion) { fn criterion_benchmark(c: &mut Criterion) {
let data: Vec<_> = default_set(1000); let data: Vec<_> = default_set(1000);
c.bench_function("insert", |b| b.iter(|| { c.bench_function("contains", |b| b.iter(|| contains(&data)));
let mut set = BitSet::new(); c.bench_function("insert", |b| b.iter(|| insert(&data)));
for v in &data {
set.insert(*v);
}
}));
} }
criterion_group!(benches, criterion_benchmark); criterion_group!(benches, criterion_benchmark);

View File

@@ -76,10 +76,12 @@ impl BitSet {
} }
} }
#[inline]
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.nbits self.nbits
} }
#[inline]
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.nbits == 0 self.nbits == 0
} }
@@ -89,6 +91,7 @@ impl BitSet {
self.nbits = 0; self.nbits = 0;
} }
#[inline]
pub fn contains(&self, value: &u64) -> bool { pub fn contains(&self, value: &u64) -> bool {
let (block, bit) = block_bit(value, &BITS); let (block, bit) = block_bit(value, &BITS);
@@ -98,6 +101,7 @@ impl BitSet {
} }
} }
#[inline]
pub fn is_subset(&self, other: &BitSet) -> bool { pub fn is_subset(&self, other: &BitSet) -> bool {
if self.nbits > other.nbits { if self.nbits > other.nbits {
false false
@@ -116,6 +120,7 @@ impl BitSet {
other.is_subset(self) other.is_subset(self)
} }
#[inline]
pub fn insert(&mut self, value: u64) -> bool { pub fn insert(&mut self, value: u64) -> bool {
let (block, bit) = block_bit(&value, &BITS); let (block, bit) = block_bit(&value, &BITS);
let block = self.blocks.entry(block).or_insert(0); let block = self.blocks.entry(block).or_insert(0);
@@ -132,6 +137,7 @@ impl BitSet {
} }
} }
#[inline]
pub fn remove(&mut self, value: &u64) -> bool { pub fn remove(&mut self, value: &u64) -> bool {
let (block, bit) = block_bit(value, &BITS); let (block, bit) = block_bit(value, &BITS);
let block = self.blocks.entry(block).or_insert(0); let block = self.blocks.entry(block).or_insert(0);
@@ -148,15 +154,17 @@ impl BitSet {
} }
} }
#[inline]
pub fn union_with(&mut self, other: &Self) { pub fn union_with(&mut self, other: &Self) {
for (key, value) in &other.blocks { for (key, value) in &other.blocks {
*self.blocks.entry(*key).or_insert(0) |= value; let block = self.blocks.entry(*key).or_insert(0);
}
self.nbits = self.blocks let n = block.count_ones();
.values()
.map(|block| block.count_ones() as usize) *block |= value;
.sum();
self.nbits += (block.count_ones() - n) as usize;
}
} }
} }
@@ -206,13 +214,14 @@ impl ops::BitOr for BitSet {
#[inline] #[inline]
fn bitor(mut self, rhs: Self) -> Self { fn bitor(mut self, rhs: Self) -> Self {
for (key, value) in &rhs.blocks { for (key, value) in &rhs.blocks {
*self.blocks.entry(*key).or_insert(0) |= value; let block = self.blocks.entry(*key).or_insert(0);
}
self.nbits = self.blocks let n = block.count_ones();
.values()
.map(|block| block.count_ones() as usize) *block |= value;
.sum();
self.nbits += (block.count_ones() - n) as usize;
}
self self
} }
@@ -224,13 +233,14 @@ impl<'a> ops::BitOr<&'a Self> for BitSet {
#[inline] #[inline]
fn bitor(mut self, rhs: &'a Self) -> Self { fn bitor(mut self, rhs: &'a Self) -> Self {
for (key, value) in &rhs.blocks { for (key, value) in &rhs.blocks {
*self.blocks.entry(*key).or_insert(0) |= value; let block = self.blocks.entry(*key).or_insert(0);
}
self.nbits = self.blocks let n = block.count_ones();
.values()
.map(|block| block.count_ones() as usize) *block |= value;
.sum();
self.nbits += (block.count_ones() - n) as usize;
}
self self
} }