Skip to content

Commit

Permalink
re
Browse files Browse the repository at this point in the history
  • Loading branch information
rsk0315 committed Feb 24, 2024
1 parent deb27af commit 00a391c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 155 deletions.
191 changes: 44 additions & 147 deletions nekolib-src/ds/rs01_dict/benches/bench_selects.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(long_running_const_eval)]

use bit_vector::{Rs01DictNLl, Rs01DictNlC};
use criterion::{
black_box, criterion_group, criterion_main, BenchmarkId, Criterion,
Expand Down Expand Up @@ -34,7 +36,7 @@ fn bench_selects(c: &mut Criterion) {
0xF5, 0xDD, 0x07, 0x06, 0xAE, 0xE4, 0x5A, 0xDC,
]);
let len = 1 << 20;
let p = 0.5;
let p = 1.0e-3;
let dist = Bernoulli::new(p).unwrap();
let a: Vec<_> = (0..len).map(|_| dist.sample(&mut rng)).collect();

Expand Down Expand Up @@ -91,8 +93,6 @@ fn bench_selects(c: &mut Criterion) {
let select0_query = rand_seq(0..count0, &mut rng);
let select1_query = rand_seq(0..count1, &mut rng);

let rep = 1;

group
.bench_function(BenchmarkId::new("succinct", "preprocess"), |b| {
b.iter(|| black_box(Rs01Dict::new(&a)))
Expand All @@ -102,151 +102,48 @@ fn bench_selects(c: &mut Criterion) {
})
.bench_function(BenchmarkId::new("compact", "preprocess"), |b| {
b.iter(|| black_box(Rs01DictNLl::new(&a)))
})
.bench_function(BenchmarkId::new("succinct", "rank-seq"), |b| {
b.iter(|| {
for _ in 0..rep {
for i in 0..a.len() {
black_box(rs.rank0(i));
}
for i in 0..a.len() {
black_box(rs.rank1(i));
}
}
})
})
.bench_function(BenchmarkId::new("naive", "rank-seq"), |b| {
b.iter(|| {
for _ in 0..rep {
for i in 0..a.len() {
black_box(rs_nlc.rank0(i));
}
for i in 0..a.len() {
black_box(rs_nlc.rank1(i));
}
}
})
})
.bench_function(BenchmarkId::new("compact", "rank-seq"), |b| {
b.iter(|| {
for _ in 0..rep {
for i in 0..a.len() {
black_box(rs_nll.rank0(i));
}
for i in 0..a.len() {
black_box(rs_nll.rank1(i));
}
}
})
})
.bench_function(BenchmarkId::new("succinct", "select-seq"), |b| {
b.iter(|| {
for _ in 0..rep {
for i in 0..count0 {
black_box(rs.select0(i));
}
for i in 0..count1 {
black_box(rs.select1(i));
}
}
})
})
.bench_function(BenchmarkId::new("naive", "select-seq"), |b| {
b.iter(|| {
for _ in 0..rep {
for i in 0..count0 {
black_box(rs_nlc.select0(i));
}
for i in 0..count1 {
black_box(rs_nlc.select1(i));
}
}
})
})
.bench_function(BenchmarkId::new("compact", "select-seq"), |b| {
b.iter(|| {
for _ in 0..rep {
for i in 0..count0 {
black_box(rs_nll.select0(i));
}
for i in 0..count1 {
black_box(rs_nll.select1(i));
}
}
})
})
.bench_function(BenchmarkId::new("succinct", "rank-rand"), |b| {
b.iter(|| {
for _ in 0..rep {
for &i in &rank_query {
black_box(rs.rank0(i));
}
for &i in &rank_query {
black_box(rs.rank1(i));
}
}
})
})
.bench_function(BenchmarkId::new("naive", "rank-rand"), |b| {
b.iter(|| {
for _ in 0..rep {
for &i in &rank_query {
black_box(rs_nlc.rank0(i));
}
for &i in &rank_query {
black_box(rs_nlc.rank1(i));
}
}
})
})
.bench_function(BenchmarkId::new("compact", "rank-rand"), |b| {
b.iter(|| {
for _ in 0..rep {
for &i in &rank_query {
black_box(rs_nll.rank0(i));
}
for &i in &rank_query {
black_box(rs_nll.rank1(i));
}
}
})
})
.bench_function(BenchmarkId::new("succinct", "select-rand"), |b| {
b.iter(|| {
for _ in 0..rep {
for &i in &select0_query {
black_box(rs.select0(i));
}
for &i in &select1_query {
black_box(rs.select1(i));
}
}
})
})
.bench_function(BenchmarkId::new("naive", "select-rand"), |b| {
b.iter(|| {
for _ in 0..rep {
for &i in &select0_query {
black_box(rs_nlc.select0(i));
}
for &i in &select1_query {
black_box(rs_nlc.select1(i));
}
}
})
})
.bench_function(BenchmarkId::new("compact", "select-rand"), |b| {
b.iter(|| {
for _ in 0..rep {
for &i in &select0_query {
black_box(rs_nll.select0(i));
}
for &i in &select1_query {
black_box(rs_nll.select1(i));
}
}
})
});

macro_rules! bench_fn {
($g:ident, $name:ident, $fn:ident, $fst:literal, $snd:literal, $iter:expr) => {
$g.bench_function(BenchmarkId::new($fst, $snd), |b| {
b.iter(|| {
for i in $iter {
black_box($name.$fn(i));
}
})
});
};
}

bench_fn! { group, rs, rank0, "succinct", "rank0-seq", 0..a.len() }
bench_fn! { group, rs, rank1, "succinct", "rank1-seq", 0..a.len() }
bench_fn! { group, rs, rank0, "succinct", "rank0-rand", rank_query.iter().copied() }
bench_fn! { group, rs, rank1, "succinct", "rank1-rand", rank_query.iter().copied() }
bench_fn! { group, rs, select0, "succinct", "select0-seq", 0..count0 }
bench_fn! { group, rs, select1, "succinct", "select1-seq", 0..count1 }
bench_fn! { group, rs, select0, "succinct", "select0-rand", select0_query.iter().copied() }
bench_fn! { group, rs, select1, "succinct", "select1-rand", select1_query.iter().copied() }

bench_fn! { group, rs_nll, rank0, "compact", "rank0-seq", 0..a.len() }
bench_fn! { group, rs_nll, rank1, "compact", "rank1-seq", 0..a.len() }
bench_fn! { group, rs_nll, rank0, "compact", "rank0-rand", rank_query.iter().copied() }
bench_fn! { group, rs_nll, rank1, "compact", "rank1-rand", rank_query.iter().copied() }
bench_fn! { group, rs_nll, select0, "compact", "select0-seq", 0..count0 }
bench_fn! { group, rs_nll, select1, "compact", "select1-seq", 0..count1 }
bench_fn! { group, rs_nll, select0, "compact", "select0-rand", select0_query.iter().copied() }
bench_fn! { group, rs_nll, select1, "compact", "select1-rand", select1_query.iter().copied() }

bench_fn! { group, rs_nlc, rank0, "naive", "rank0-seq", 0..a.len() }
bench_fn! { group, rs_nlc, rank1, "naive", "rank1-seq", 0..a.len() }
bench_fn! { group, rs_nlc, rank0, "naive", "rank0-rand", rank_query.iter().copied() }
bench_fn! { group, rs_nlc, rank1, "naive", "rank1-rand", rank_query.iter().copied() }
bench_fn! { group, rs_nlc, select0, "naive", "select0-seq", 0..count0 }
bench_fn! { group, rs_nlc, select1, "naive", "select1-seq", 0..count1 }
bench_fn! { group, rs_nlc, select0, "naive", "select0-rand", select0_query.iter().copied() }
bench_fn! { group, rs_nlc, select1, "naive", "select1-rand", select1_query.iter().copied() }

group.finish();
}

criterion_group!(benches, bench_selects);
Expand Down
23 changes: 15 additions & 8 deletions nekolib-src/ds/rs01_dict/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const RANK_BIT_PATTERNS: usize = 1 << RANK_SMALL_LEN;

const SELECT_SMALL_LEN: usize = 15; // (1/2) log(n)/2
const SELECT_LARGE_SPARSE_LEN: usize = 12946;
const SELECT_LARGE_POPCNT: usize = 15;
const SELECT_LARGE_POPCNT: usize = 17;
const SELECT_LARGE_NODE_LEN: usize = 4;
const SELECT_LARGE_BRANCH: usize = 4;
const SELECT_WORD_BIT_PATTERNS: usize = 1 << SELECT_SMALL_LEN;
Expand Down Expand Up @@ -84,7 +84,7 @@ struct RankIndex<
const BIT_PATTERNS: usize,
> {
large: Vec<u32>,
small: Vec<u8>,
small: Vec<u16>,
}

struct SelectIndex<
Expand Down Expand Up @@ -154,6 +154,7 @@ const fn rank_lookup<const SMALL_LEN: usize, const BIT_PATTERNS: usize>()
table
}

#[warn(long_running_const_eval)]
const fn select_tree_lookup<
const NODE_LEN: usize,
const POPCNT: usize,
Expand Down Expand Up @@ -378,15 +379,11 @@ impl<const LARGE_LEN: usize, const SMALL_LEN: usize, const BIT_PATTERNS: usize>
let per = LARGE_LEN / SMALL_LEN;
for (c, i) in a
.chunks::<true>(SMALL_LEN)
.map(|ai| Self::WORD[ai as usize][SMALL_LEN - 1])
.map(|ai| Self::WORD[ai as usize][SMALL_LEN - 1] as u16)
.zip((0..per).cycle())
{
small.push(small_acc);
if i == per - 1 {
small_acc = 0;
} else {
small_acc += c;
}
small_acc = if i < per - 1 { small_acc + c } else { 0 };

if i == 0 {
large.push(large_acc);
Expand Down Expand Up @@ -546,12 +543,22 @@ impl<
let tmp = last;
{
let mut it = tmp.chunks::<true>(LARGE_NODE_LEN);
let mut trunc = None;
while let Some(mut sum) = it.next() {
sum += (1..LARGE_BRANCH)
.filter_map(|_| it.next())
.sum::<u64>();
if sum & (!0 << LARGE_NODE_LEN) != 0 {
trunc = Some(sum);
sum &= !(!0 << LARGE_NODE_LEN);
}
cur.push(sum, LARGE_NODE_LEN);
}
if let Some(sum) = trunc {
if cur.len() > LARGE_NODE_LEN {
panic!("invalid popcount, {}", sum);
}
}
}
tree.push(tmp);
last = cur;
Expand Down

0 comments on commit 00a391c

Please sign in to comment.