Closed CrockAgile closed 8 months ago
Hi, I noticed this PR and wanted to suggest a few improvements if I may 😄
parse_postal
can instead be written as:
#[divan::bench]
fn parse_postal(bencher: divan::Bencher) {
let input = divan::black_box(
include_str!("../tests/fixtures/postal_address.terminated.input.bnf")
);
bencher.bench(|| {
input.parse::<bnf::Grammar>().unwrap();
});
}
This ensures each iteration gets an opaque input without the cost of storing duplicate inputs (Bencher::with_inputs
allocates storage for all inputs in a sample).
Similarly, parse_infinite_nullable_grammar
could be written as:
#[divan::bench]
fn parse_infinite_nullable_grammar(bencher: divan::Bencher) {
let infinite_grammar: bnf::Grammar = "
<a> ::= '' | <b>
<b> ::= <a>"
.parse()
.unwrap();
let parse_count: usize = {
use rand::Rng;
let mut rng: rand::rngs::StdRng = rand::SeedableRng::seed_from_u64(0);
rng.gen_range(1..100);
};
bencher
.counter(parse_count)
.bench(|| {
let _: Vec<_> = infinite_grammar.parse_input("").take(parse_count).collect();
});
}
However, you probably meant to have each iteration use a random parse_count
, in which case it could be rewritten to:
#[divan::bench]
fn parse_infinite_nullable_grammar(bencher: divan::Bencher) {
use rand::Rng;
let infinite_grammar: bnf::Grammar = "
<a> ::= '' | <b>
<b> ::= <a>"
.parse()
.unwrap();
let mut rng: rand::rngs::StdRng = rand::SeedableRng::seed_from_u64(0);
bencher
.with_inputs(|| rng.gen_range(1..100))
.count_inputs_as::<divan::counter::ItemsCount>()
.bench_local_values(|parse_count| {
let _: Vec<_> = infinite_grammar.parse_input("").take(parse_count).collect();
});
}
Note that Bencher::count_inputs_as
is in 0.1.4
.
@nvzqz wow thank you for the great notes! I applied your suggestions. thank you!
Closes #138
I ported the existing benchmarks to Divan as described in #138.
TODO