Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use binggan for stacker bench #2492

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions stacker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ common = { version = "0.7", path = "../common/", package = "tantivy-common" }
ahash = { version = "0.8.11", default-features = false, optional = true }
rand_distr = "0.4.3"


[[bench]]
harness = false
name = "crit_bench"
path = "benches/crit_bench.rs"
name = "bench"
path = "benches/bench.rs"

[[example]]
name = "hashmap"
Expand All @@ -25,9 +26,9 @@ path = "example/hashmap.rs"
[dev-dependencies]
rand = "0.8.5"
zipf = "7.0.0"
criterion = { git = "https://github.com/PSeitz/criterion.rs/", rev = "e6f98ee"} # This fork includes stack randomization to reduce caching effects
rustc-hash = "1.1.0"
proptest = "1.2.0"
binggan = { version = "0.10.0" }

[features]
compare_hash_only = ["ahash"] # Compare hash only, not the key in the Hashmap
Expand Down
204 changes: 204 additions & 0 deletions stacker/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
use binggan::{black_box, BenchRunner, PeakMemAlloc, INSTRUMENTED_SYSTEM};
use rand::SeedableRng;
use rustc_hash::FxHashMap;
use tantivy_stacker::{ArenaHashMap, ExpUnrolledLinkedList, MemoryArena};

const ALICE: &str = include_str!("../../benches/alice.txt");

#[global_allocator]
pub static GLOBAL: &PeakMemAlloc<std::alloc::System> = &INSTRUMENTED_SYSTEM;

fn bench_vint() {
let mut runner = BenchRunner::new();
runner.set_alloc(GLOBAL); // Set the peak mem allocator. This will enable peak memory reporting.

{
let input_bytes = ALICE.len();

let alice_terms_as_bytes: Vec<&[u8]> = ALICE
.split_ascii_whitespace()
.map(|el| el.as_bytes())
.collect();

let alice_terms_as_bytes_with_docid: Vec<(u32, &[u8])> = ALICE
.split_ascii_whitespace()
.map(|el| el.as_bytes())
.enumerate()
.map(|(docid, el)| (docid as u32, el))
.collect();

// Alice benchmark
let mut group = runner.new_group();
group.set_name(format!("alice (num terms: {})", ALICE.len()));
group.set_input_size(input_bytes);
group.register_with_input("hashmap", &alice_terms_as_bytes, move |data| {
black_box(create_hash_map(data.iter()));
None
});
group.register_with_input(
"hasmap with postings",
&alice_terms_as_bytes_with_docid,
move |data| {
black_box(create_hash_map_with_expull(data.iter().cloned()));
None
},
);
group.register_with_input(
"fxhashmap ref postings",
&alice_terms_as_bytes,
move |data| {
black_box(create_fx_hash_ref_map_with_expull(data.iter().cloned()));
None
},
);
group.register_with_input(
"fxhasmap owned postings",
&alice_terms_as_bytes,
move |data| {
black_box(create_fx_hash_owned_map_with_expull(data.iter().cloned()));
None
},
);
group.run();
}

{
for (num_numbers, num_numbers_label) in [
(100_000u64, "100k"),
(1_000_000, "1mio"),
(2_000_000, "2mio"),
(5_000_000, "5mio"),
] {
// benchmark unique numbers
{
let numbers: Vec<[u8; 8]> = (0..num_numbers).map(|el| el.to_le_bytes()).collect();
let numbers_with_doc: Vec<_> = numbers
.iter()
.enumerate()
.map(|(docid, el)| (docid as u32, el))
.collect();

let input_bytes = numbers.len() * 8;
let mut group = runner.new_group();
group.set_name(format!("numbers unique {}", num_numbers_label));
group.set_input_size(input_bytes);
group.register_with_input("hashmap", &numbers, move |data| {
black_box(create_hash_map(data.iter()));
None
});
group.register_with_input("hasmap with postings", &numbers_with_doc, move |data| {
black_box(create_hash_map_with_expull(data.iter().cloned()));
None
});
group.run();
}
// benchmark zipfs distribution numbers
{
use rand::distributions::Distribution;
use rand::rngs::StdRng;
let mut rng = StdRng::from_seed([3u8; 32]);
let zipf = zipf::ZipfDistribution::new(10_000, 1.03).unwrap();
let numbers: Vec<[u8; 8]> = (0..num_numbers)
.map(|_| zipf.sample(&mut rng).to_le_bytes())
.collect();
let numbers_with_doc: Vec<_> = numbers
.iter()
.enumerate()
.map(|(docid, el)| (docid as u32, el))
.collect();

let input_bytes = numbers.len() * 8;
let mut group = runner.new_group();
group.set_name(format!("zipfs numbers {}", num_numbers_label));
group.set_input_size(input_bytes);
group.register_with_input("hashmap", &numbers, move |data| {
black_box(create_hash_map(data.iter()));
None
});
group.register_with_input("hasmap with postings", &numbers_with_doc, move |data| {
black_box(create_hash_map_with_expull(data.iter().cloned()));
None
});
group.run();
}
}
}
}

fn main() {
bench_vint();
}

const HASHMAP_SIZE: usize = 1 << 15;

/// Only records the doc ids
#[derive(Clone, Default, Copy)]
pub struct DocIdRecorder {
stack: ExpUnrolledLinkedList,
}
impl DocIdRecorder {
fn new_doc(&mut self, doc: u32, arena: &mut MemoryArena) {
self.stack.writer(arena).write_u32_vint(doc);
}
}

fn create_hash_map<T: AsRef<[u8]>>(terms: impl Iterator<Item = T>) -> ArenaHashMap {
let mut map = ArenaHashMap::with_capacity(HASHMAP_SIZE);
for term in terms {
map.mutate_or_create(term.as_ref(), |val| {
if let Some(mut val) = val {
val += 1;
val
} else {
1u64
}
});
}

map
}

fn create_hash_map_with_expull<T: AsRef<[u8]>>(
terms: impl Iterator<Item = (u32, T)>,
) -> ArenaHashMap {
let mut memory_arena = MemoryArena::default();
let mut map = ArenaHashMap::with_capacity(HASHMAP_SIZE);
for (i, term) in terms {
map.mutate_or_create(term.as_ref(), |val: Option<DocIdRecorder>| {
if let Some(mut rec) = val {
rec.new_doc(i, &mut memory_arena);
rec
} else {
DocIdRecorder::default()
}
});
}

map
}

fn create_fx_hash_ref_map_with_expull(
terms: impl Iterator<Item = &'static [u8]>,
) -> FxHashMap<&'static [u8], Vec<u32>> {
let terms = terms.enumerate();
let mut map = FxHashMap::with_capacity_and_hasher(HASHMAP_SIZE, Default::default());
for (i, term) in terms {
map.entry(term.as_ref())
.or_insert_with(Vec::new)
.push(i as u32);
}
map
}

fn create_fx_hash_owned_map_with_expull(
terms: impl Iterator<Item = &'static [u8]>,
) -> FxHashMap<Vec<u8>, Vec<u32>> {
let terms = terms.enumerate();
let mut map = FxHashMap::with_capacity_and_hasher(HASHMAP_SIZE, Default::default());
for (i, term) in terms {
map.entry(term.as_ref().to_vec())
.or_insert_with(Vec::new)
.push(i as u32);
}
map
}
Loading
Loading