Skip to content

Commit a997f50

Browse files
authored
Merge pull request #1 from nvzqz/bench-ergonomics
Improve benchmark ergonomics
2 parents 15213ed + 096658a commit a997f50

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

benchmarks/benches/generate.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
use std::{cell::RefCell, iter::repeat_with};
1+
use std::iter::repeat_with;
22

33
use quickphf_codegen::phf::generate_phf;
44

55
use benchmarks::SIZES;
66

77
const SEED: u64 = 42;
88

9-
#[divan::bench(consts = SIZES, max_time = std::time::Duration::from_secs(10))]
9+
#[divan::bench(consts = SIZES, max_time = 10)]
1010
fn quickphf<const S: usize>(bencher: divan::Bencher) {
11-
let rng = RefCell::new(fastrand::Rng::with_seed(SEED));
11+
let mut rng = fastrand::Rng::with_seed(SEED);
1212

1313
bencher
14-
.with_inputs(|| repeat_with(|| rng.borrow_mut().u64(..)).take(S).collect())
14+
.with_inputs(|| repeat_with(|| rng.u64(..)).take(S).collect())
1515
.bench_local_refs(|keys: &mut Vec<u64>| generate_phf(keys));
1616
}
1717

18-
#[divan::bench(consts = SIZES, max_time = std::time::Duration::from_secs(10))]
18+
#[divan::bench(consts = SIZES, max_time = 10)]
1919
fn phf<const S: usize>(bencher: divan::Bencher) {
20-
let rng = RefCell::new(fastrand::Rng::with_seed(SEED));
20+
let mut rng = fastrand::Rng::with_seed(SEED);
2121

2222
bencher
23-
.with_inputs(|| repeat_with(|| rng.borrow_mut().u64(..)).take(S).collect())
23+
.with_inputs(|| repeat_with(|| rng.u64(..)).take(S).collect())
2424
.bench_local_refs(|keys: &mut Vec<u64>| phf_generator::generate_hash(keys));
2525
}
2626

benchmarks/benches/lookup.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
use std::cell::RefCell;
2-
31
use benchmarks::{PHF_MAPS, QUICKPHF_MAPS, QUICKPHF_RAW_MAPS, SIZES};
42

53
const BATCH_SIZE: usize = 1000;
64
const SEED: u64 = 42;
75

86
#[divan::bench(consts = SIZES, sample_size = 1)]
97
fn phf_map<const S: usize>(bencher: divan::Bencher) {
10-
let rng = RefCell::new(fastrand::Rng::with_seed(SEED));
8+
let mut rng = fastrand::Rng::with_seed(SEED)
119

1210
let index = SIZES.iter().position(|&s| s == S).unwrap();
1311
let map = &PHF_MAPS[index];
1412
let keys = map.keys().copied().collect::<Vec<_>>();
1513

1614
bencher
17-
.with_inputs(|| (0..BATCH_SIZE).map(|_| keys[rng.borrow_mut().usize(0..S)]))
15+
.with_inputs(|| (0..BATCH_SIZE).map(|_| keys[rng.usize(0..S)]))
1816
.bench_local_refs(|queries| {
1917
for query in queries {
2018
divan::black_box(map.get(&query).unwrap());
@@ -24,14 +22,14 @@ fn phf_map<const S: usize>(bencher: divan::Bencher) {
2422

2523
#[divan::bench(consts = SIZES, sample_size = 1)]
2624
fn quickphf_map<const S: usize>(bencher: divan::Bencher) {
27-
let rng = RefCell::new(fastrand::Rng::with_seed(SEED));
25+
let mut rng = fastrand::Rng::with_seed(SEED)
2826

2927
let index = SIZES.iter().position(|&s| s == S).unwrap();
3028
let map = &QUICKPHF_MAPS[index];
3129
let keys = map.keys().copied().collect::<Vec<_>>();
3230

3331
bencher
34-
.with_inputs(|| (0..BATCH_SIZE).map(|_| keys[rng.borrow_mut().usize(0..S)]))
32+
.with_inputs(|| (0..BATCH_SIZE).map(|_| keys[rng.usize(0..S)]))
3533
.bench_local_refs(|queries| {
3634
for query in queries {
3735
divan::black_box(map.get(&query).unwrap());
@@ -41,14 +39,14 @@ fn quickphf_map<const S: usize>(bencher: divan::Bencher) {
4139

4240
#[divan::bench(consts = SIZES, sample_size = 1)]
4341
fn quickphf_raw_map<const S: usize>(bencher: divan::Bencher) {
44-
let rng = RefCell::new(fastrand::Rng::with_seed(SEED));
42+
let mut rng = fastrand::Rng::with_seed(SEED)
4543

4644
let index = SIZES.iter().position(|&s| s == S).unwrap();
4745
let map = &QUICKPHF_RAW_MAPS[index];
4846
let keys = &QUICKPHF_MAPS[index].keys().copied().collect::<Vec<_>>();
4947

5048
bencher
51-
.with_inputs(|| (0..BATCH_SIZE).map(|_| keys[rng.borrow_mut().usize(0..S)]))
49+
.with_inputs(|| (0..BATCH_SIZE).map(|_| keys[rng.usize(0..S)]))
5250
.bench_local_refs(|queries| {
5351
for query in queries {
5452
divan::black_box(map.get(&query));

0 commit comments

Comments
 (0)