Skip to content

Commit

Permalink
refactoring: easier size reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
beling committed Mar 10, 2024
1 parent 2484cc2 commit 548f9a9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 48 deletions.
13 changes: 6 additions & 7 deletions cseq_benchmark/src/bitm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use aligned_vec::ABox;
use bitm::{RankSelect101111, BinaryRankSearch, BitAccess, BitVec, CombinedSampling, Rank, Select, Select0};
use dyn_size_of::GetSize;
use crate::{percent_of, percent_of_diff, Conf, Tester};
use crate::{Conf, Tester};

pub fn build_bit_vec(conf: &Conf) -> (ABox<[u64]>, Tester) {
let mut content = ABox::with_zeroed_bits(conf.universe);
Expand All @@ -17,24 +17,23 @@ pub fn benchmark_rank_select(conf: &super::Conf) {
let (rs, _) = RankSelect101111::<BinaryRankSearch, BinaryRankSearch, _>::build(content);
//assert_eq!(ones, conf.num);

tester.raport_rank("bitm RankSelect101111",
percent_of_diff(rs.size_bytes(), rs.content.size_bytes()),
tester.raport_rank("bitm RankSelect101111", rs.size_bytes(),
|index| unsafe{rs.rank_unchecked(index)});

println!(" select by binary search over ranks (no extra space overhead):");
tester.raport_select1("bitm RankSelect101111 binary search over ranks",
0.0, |index| unsafe{rs.select_unchecked(index)});
0, |index| unsafe{rs.select_unchecked(index)});
tester.raport_select0("bitm RankSelect101111 binary search over ranks",
0.0, |index| unsafe{rs.select0_unchecked(index)});
0, |index| unsafe{rs.select0_unchecked(index)});

let (rs, _) = RankSelect101111::<CombinedSampling, CombinedSampling, _>::build(rs.content);

println!(" select by combined sampling:");
tester.raport_select1("bitm RankSelect101111 combined sampling",
percent_of(rs.select_support().size_bytes(), rs.content.size_bytes()),
rs.select_support().size_bytes(),
|index| unsafe{rs.select_unchecked(index)});
tester.raport_select0("bitm RankSelect101111 combined sampling",
percent_of(rs.select0_support().size_bytes(), rs.content.size_bytes()),
rs.select0_support().size_bytes(),
|index| unsafe{rs.select0_unchecked(index)});
}

6 changes: 2 additions & 4 deletions cseq_benchmark/src/elias_fano.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ pub fn benchmark(conf: &super::Conf) {
let ef = builder.finish();
println!(" size: {:.2} bits/item {} bits/lo entry", 8.0*ef.size_bytes() as f64/tester.number_of_ones as f64, ef.bits_per_lo());

let space_overhead = 100.0 * (8.0*ef.size_bytes() as f64 - conf.universe as f64) / conf.universe as f64;

tester.raport_rank("cseq Elias-Fano", space_overhead, |i| ef.rank(i));
tester.raport_select1("cseq Elias-Fano", space_overhead, |i| ef.select(i));
tester.raport_rank("cseq Elias-Fano", ef.size_bytes(), |i| ef.rank(i));
tester.raport_select1("cseq Elias-Fano", 0, |i| ef.select(i));

/*let start_moment = Instant::now();
for index in 0..data.len() {
Expand Down
30 changes: 22 additions & 8 deletions cseq_benchmark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ fn check<R: Into<Option<usize>>>(structure_name: &str, operation_name: &str, arg

impl<'c> Tester<'c> {
/// Tests function answering `rank` queries. Reports its speed and potentially validates.
#[inline(always)] pub fn raport_rank<R: Into<Option<usize>>, F>(&self, method_name: &str, space_overhead: f64, rank: F)
#[inline(always)] pub fn raport_rank<R: Into<Option<usize>>, F>(&self, method_name: &str, size_bytes: usize, rank: F)
where F: Fn(usize) -> R
{
print!(" rank: space overhead {:.2}%", space_overhead);
print!(" rank: space overhead {:.2}%", self.conf.space_overhead(size_bytes));
let time = self.conf.queries_measure(&self.conf.rand_queries(self.conf.universe), &rank).as_nanos();
println!(" time/query {:.2}ns", time);
self.conf.save_rank(method_name, space_overhead, time);
self.conf.save_rank(method_name, self.conf.space_overhead(size_bytes), time);
self.verify_rank(method_name, rank);
}

Expand All @@ -158,15 +158,16 @@ impl<'c> Tester<'c> {
}

/// Tests function answering `select` (one) queries. Reports its speed and potentially validates.
#[inline(always)] pub fn raport_select1<R: Into<Option<usize>>, F>(&self, method_name: &str, space_overhead: f64, select: F)
#[inline(always)] pub fn raport_select1<R: Into<Option<usize>>, F>(&self, method_name: &str, extra_size_bytes: usize, select: F)
where F: Fn(usize) -> R
{
if self.number_of_ones == 0 {
//println!("skipping select1 test as there are no ones");
return;
}
print!(" select1:");
if space_overhead != 0.0 { print!(" space overhead {:.2}%", space_overhead); }
let space_overhead = self.conf.extra_space_overhead(extra_size_bytes);
if extra_size_bytes != 0 { print!(" space overhead {:.2}%", space_overhead); }
let time = self.conf.queries_measure(&self.conf.rand_queries(self.number_of_ones), &select).as_nanos();
println!(" time/query {:.2}ns", time);
self.conf.save_select1(method_name, space_overhead, time);
Expand All @@ -185,15 +186,16 @@ impl<'c> Tester<'c> {
}

/// Tests function answering `select0` queries. Reports its speed and potentially validates.
#[inline(always)] pub fn raport_select0<R: Into<Option<usize>>, F>(&self, method_name: &str, space_overhead: f64, select0: F)
#[inline(always)] pub fn raport_select0<R: Into<Option<usize>>, F>(&self, method_name: &str, extra_size_bytes: usize, select0: F)
where F: Fn(usize) -> R
{
if self.conf.universe == self.number_of_ones {
//println!("skipping select0 test as there are no zeros");
return;
}
print!(" select0:");
if space_overhead != 0.0 { print!(" space overhead {:.2}%", space_overhead); }
let space_overhead = self.conf.extra_space_overhead(extra_size_bytes);
if extra_size_bytes != 0 { print!(" space overhead {:.2}%", space_overhead); }
let time = self.conf.queries_measure(
&self.conf.rand_queries(self.conf.universe-self.number_of_ones),
&select0).as_nanos();
Expand Down Expand Up @@ -237,6 +239,18 @@ impl Conf {
}
}

/// Returns space overhead [%] over raw bitmap introduced by given size (in bytes).
fn extra_space_overhead(&self, extra_size_bytes: usize) -> f64 {
let raw_space = (self.universe + 7) / 8;
(100 * extra_size_bytes) as f64 / raw_space as f64
}

/// Returns space overhead [%] over raw bitmap of structure of given size (in bytes).
fn space_overhead(&self, size_bytes: usize) -> f64 {
let raw_space = (self.universe + 7) / 8;
(100 * (size_bytes as isize - raw_space as isize)) as f64 / raw_space as f64
}

/// Iterates over universe and for each its point calls `f` with the following arguments (in order):
/// - index in universe in range [0..`self.universe`),
/// - number of items (ones) before the current position,
Expand Down Expand Up @@ -415,7 +429,7 @@ impl Conf {
}

fn percent_of(overhead: usize, whole: usize) -> f64 { (overhead*100) as f64 / whole as f64 }
fn percent_of_diff(with_overhead: usize, whole: usize) -> f64 { percent_of(with_overhead-whole, whole) }
//fn percent_of_diff(with_overhead: usize, whole: usize) -> f64 { percent_of(with_overhead-whole, whole) }

fn main() {
let conf: Conf = Conf::parse();
Expand Down
16 changes: 7 additions & 9 deletions cseq_benchmark/src/succinct.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
use succinct::{rank, select::{Select0Support, Select1Support}, BinSearchSelect, BitRankSupport, BitVecMut, BitVector, SpaceUsage};
use crate::{percent_of_diff, Conf, Tester};
use crate::{Conf, Tester};

pub fn build_bit_vec(conf: &Conf) -> (BitVector::<u64>, Tester) {
let mut content = BitVector::with_fill(conf.universe as u64, false);
let tester = conf.fill_data(|bit_nr, value| {content.set_bit(bit_nr as u64, value)});
(content, tester)
}

fn benchmark(mut tester: super::Tester, method_name: &str, content_size: usize, rank: impl BitRankSupport+SpaceUsage) {
fn benchmark(mut tester: super::Tester, method_name: &str, rank: impl BitRankSupport+SpaceUsage) {
tester.rank_includes_current = true;
tester.raport_rank(method_name, percent_of_diff(rank.total_bytes(), content_size),
tester.raport_rank(method_name, rank.total_bytes(),
|index| rank.rank1(index as u64) as usize);
println!(" select by binary search over ranks (no extra space overhead):");
let select = BinSearchSelect::new(rank);
tester.raport_select1(method_name, 0.0, |index| select.select1(index as u64).map(|v| v as usize));
tester.raport_select0(method_name, 0.0, |index| select.select0(index as u64).map(|v| v as usize));
tester.raport_select1(method_name, 0, |index| select.select1(index as u64).map(|v| v as usize));
tester.raport_select0(method_name, 0, |index| select.select0(index as u64).map(|v| v as usize));
}

pub fn benchmark_rank9(conf: &super::Conf) {
println!("succinct Rank9:");
let (content, tester) = build_bit_vec(conf);
benchmark(tester, "succinct Rank9", content.total_bytes(),
rank::Rank9::new(content));
benchmark(tester, "succinct Rank9", rank::Rank9::new(content));
}

pub fn benchmark_jacobson(conf: &super::Conf) {
println!("succinct JacobsonRank:");
let (content, tester) = build_bit_vec(conf);
benchmark(tester, "succinct JacobsonRank", content.total_bytes(),
rank::JacobsonRank::new(content));
benchmark(tester, "succinct JacobsonRank", rank::JacobsonRank::new(content));
}
16 changes: 6 additions & 10 deletions cseq_benchmark/src/sucds.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use sucds::{bit_vectors::{Rank9Sel, BitVector, Rank, Select}, Serializable};
use crate::{percent_of, percent_of_diff, Conf};
use crate::Conf;

pub fn benchmark_rank9_select(conf: &Conf) {
println!("sucds Rank9Sel:");
Expand All @@ -9,26 +9,22 @@ pub fn benchmark_rank9_select(conf: &Conf) {
if value { content.set_bit(pos, value).unwrap(); }
);

let content_size = content.size_in_bytes();
let mut rs = Rank9Sel::new(content);
let rs_size_without_hints = rs.size_in_bytes();
tester.raport_rank("sucds Rank9Sel",
percent_of_diff(rs_size_without_hints, content_size),
tester.raport_rank("sucds Rank9Sel", rs_size_without_hints,
|index| rs.rank1(index));

println!(" select without hints (no extra space overhead):");
tester.raport_select1("sucds Rank9Sel", 0.0, |index| rs.select1(index));
tester.raport_select0("sucds Rank9Sel", 0.0, |index| rs.select0(index));
tester.raport_select1("sucds Rank9Sel", 0, |index| rs.select1(index));
tester.raport_select0("sucds Rank9Sel", 0, |index| rs.select0(index));

println!(" select with hints:");
rs = rs.select1_hints();
let rs_select1_size = rs.size_in_bytes() - rs_size_without_hints;
rs = rs.select0_hints();
let rs_select0_size = rs.size_in_bytes() - rs_size_without_hints - rs_select1_size;
tester.raport_select1("sucds Rank9Sel + hints",
percent_of(rs_select1_size, content_size),
tester.raport_select1("sucds Rank9Sel + hints", rs_select1_size,
|index| rs.select1(index));
tester.raport_select0("sucds Rank9Sel + hints",
percent_of(rs_select0_size, content_size),
tester.raport_select0("sucds Rank9Sel + hints", rs_select0_size,
|index| rs.select0(index));
}
10 changes: 5 additions & 5 deletions cseq_benchmark/src/sux.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use mem_dbg::MemSize;
use sux::{bits::BitVec, rank_sel::{SelectFixed1, SelectFixed2, SelectZeroFixed1, SelectZeroFixed2}, traits::{ConvertTo, Select, SelectZero}};
use crate::{percent_of_diff, Conf, Tester};
use crate::{Conf, Tester};

pub fn build_bit_vec(conf: &Conf) -> (BitVec, Tester) {
let mut content = BitVec::new(conf.universe);
Expand All @@ -17,14 +17,14 @@ pub fn benchmark_select_fixed2(conf: &Conf) {
let rs = SelectFixed2::<_, _, 10, 2>::new(content);
//rs.mem_dbg(Default::default()).unwrap();
tester.raport_select1("sux SelectFixed2",
percent_of_diff(rs.mem_size(Default::default()), content_size),
rs.mem_size(Default::default()) - content_size,
|rank| unsafe { rs.select_unchecked(rank) });

content = rs.convert_to().unwrap();
let rs = SelectZeroFixed2::<_, _, 10, 2>::new(content);
//rs.mem_dbg(Default::default()).unwrap();
tester.raport_select0("sux SelectFixed2",
percent_of_diff(rs.mem_size(Default::default()), content_size),
rs.mem_size(Default::default()) - content_size,
|rank| unsafe { rs.select_zero_unchecked(rank) });
}

Expand All @@ -37,13 +37,13 @@ pub fn benchmark_select_fixed1(conf: &Conf) {
let rs = SelectFixed1::<_, _, 8>::new(content, tester.number_of_ones);
//rs.mem_dbg(Default::default()).unwrap();
tester.raport_select1("sux SelectFixed1",
percent_of_diff(rs.mem_size(Default::default()), content_size),
rs.mem_size(Default::default()) - content_size,
|rank| unsafe { rs.select_unchecked(rank) });

content = rs.convert_to().unwrap();
let rs = SelectZeroFixed1::<_, _, 8>::new(content);
//rs.mem_dbg(Default::default()).unwrap();
tester.raport_select0("sux SelectFixed1",
percent_of_diff(rs.mem_size(Default::default()), content_size),
rs.mem_size(Default::default()) - content_size,
|rank| unsafe { rs.select_zero_unchecked(rank) });
}
8 changes: 3 additions & 5 deletions cseq_benchmark/src/vers.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use vers_vecs::{BitVec, RsVec};
use crate::percent_of_diff;

pub fn benchmark_rank_select(conf: &super::Conf) {
println!("vers:");
let mut content = BitVec::from_zeros(conf.universe);
let tester = conf.fill_data(|pos, value| if value { content.flip_bit(pos); });

let raw_size = content.heap_size();
let rs = RsVec::from_bit_vec(content);
tester.raport_rank("vers RsVec", percent_of_diff(rs.heap_size(), raw_size), |index| rs.rank1(index));
tester.raport_select1("vers RsVec", 0.0, |index| rs.select1(index));
tester.raport_select0("vers RsVec", 0.0, |index| rs.select0(index));
tester.raport_rank("vers RsVec", rs.heap_size(), |index| rs.rank1(index));
tester.raport_select1("vers RsVec", 0, |index| rs.select1(index));
tester.raport_select0("vers RsVec", 0, |index| rs.select0(index));
}

0 comments on commit 548f9a9

Please sign in to comment.