Skip to content

Commit

Permalink
perf(bitmap): change the buffer unit from u8 to usize (#7030)
Browse files Browse the repository at this point in the history
* bitmap: use pointer-sized element for underlying buffer

Signed-off-by: Runji Wang <wangrunji0408@163.com>

* use `Box<[usize]>` to save 8 bytes

Signed-off-by: Runji Wang <wangrunji0408@163.com>

* rename functions and add docs

Signed-off-by: Runji Wang <wangrunji0408@163.com>

* add bench for bitmap

Signed-off-by: Runji Wang <wangrunji0408@163.com>

Signed-off-by: Runji Wang <wangrunji0408@163.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and lmatz committed Jan 3, 2023
1 parent efbe7dc commit 9ed56ac
Show file tree
Hide file tree
Showing 30 changed files with 252 additions and 580 deletions.
4 changes: 4 additions & 0 deletions src/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,7 @@ tempfile = "3"
[[bench]]
name = "bench_encoding"
harness = false

[[bench]]
name = "bitmap"
harness = false
32 changes: 32 additions & 0 deletions src/common/benches/bitmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2022 Singularity Data
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use criterion::{criterion_group, criterion_main, Criterion};
use risingwave_common::buffer::Bitmap;

fn bench_bitmap(c: &mut Criterion) {
const CHUNK_SIZE: usize = 1024;
let x = Bitmap::zeros(CHUNK_SIZE);
let y = Bitmap::ones(CHUNK_SIZE);
let i = 0x123;
c.bench_function("zeros", |b| b.iter(|| Bitmap::zeros(CHUNK_SIZE)));
c.bench_function("ones", |b| b.iter(|| Bitmap::ones(CHUNK_SIZE)));
c.bench_function("get", |b| b.iter(|| x.is_set(i)));
c.bench_function("and", |b| b.iter(|| &x & &y));
c.bench_function("or", |b| b.iter(|| &x | &y));
c.bench_function("not", |b| b.iter(|| !&x));
}

criterion_group!(benches, bench_bitmap);
criterion_main!(benches);
2 changes: 1 addition & 1 deletion src/common/src/array/bool_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl FromIterator<bool> for BoolArray {
fn from_iter<I: IntoIterator<Item = bool>>(iter: I) -> Self {
let data: Bitmap = iter.into_iter().collect();
BoolArray {
bitmap: Bitmap::all_high_bits(data.len()),
bitmap: Bitmap::ones(data.len()),
data,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/src/array/data_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl DataChunk {
/// `cardinality` returns the number of visible tuples
pub fn cardinality(&self) -> usize {
match &self.vis2 {
Vis::Bitmap(b) => b.num_high_bits(),
Vis::Bitmap(b) => b.count_ones(),
Vis::Compact(len) => *len,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/src/array/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl<T: PrimitiveArrayItemType> FromIterator<T> for PrimitiveArray<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let data: Vec<T> = iter.into_iter().collect();
PrimitiveArray {
bitmap: Bitmap::all_high_bits(data.len()),
bitmap: Bitmap::ones(data.len()),
data,
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/common/src/array/vis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl Vis {
self.as_ref().iter()
}

pub fn ones(&self) -> impl Iterator<Item = usize> + '_ {
self.as_ref().ones()
pub fn iter_ones(&self) -> impl Iterator<Item = usize> + '_ {
self.as_ref().iter_ones()
}

#[inline(always)]
Expand Down Expand Up @@ -145,9 +145,9 @@ impl<'a> VisRef<'a> {
}

#[auto_enum(Iterator)]
pub fn ones(self) -> impl Iterator<Item = usize> + 'a {
pub fn iter_ones(self) -> impl Iterator<Item = usize> + 'a {
match self {
VisRef::Bitmap(b) => b.ones(),
VisRef::Bitmap(b) => b.iter_ones(),
VisRef::Compact(c) => 0..c,
}
}
Expand Down
Loading

0 comments on commit 9ed56ac

Please sign in to comment.