Skip to content

Commit

Permalink
refactor: switch to more lightweight bench crate
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 committed Oct 6, 2023
1 parent d616e92 commit 3870a6c
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 411 deletions.
380 changes: 66 additions & 314 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ license = "MIT"

[workspace.dependencies]
color-eyre = { version = "0.6.2", default-features = false }
eyre = "0.6.8"
miette = "5.10.0"
serde = { version = "1.0.188", features = ["derive"] }

[profile.release]
lto = true
Expand Down
3 changes: 1 addition & 2 deletions crates/stef-benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ mimalloc = "0.1.39"
stef-parser = { path = "../stef-parser" }

[dev-dependencies]
criterion = "0.5.1"
divan = "0.1.0"
indoc = "2.0.4"
serde = "1.0.188"
48 changes: 23 additions & 25 deletions crates/stef-benches/benches/parser.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use std::{fmt::Write, hint::black_box};
use std::fmt::Write;

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use divan::{black_box, Bencher};
use indoc::indoc;

#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;

fn parser(c: &mut Criterion) {
c.bench_function("basic", |b| {
let input = indoc! {r#"
fn main() {
divan::main();
}

#[divan::bench]
fn basic(bencher: Bencher) {
let input = indoc! {r#"
use other::one::Type1;
use other::two;
Expand Down Expand Up @@ -39,28 +43,25 @@ fn parser(c: &mut Criterion) {
type SampleTyped = SampleStruct<bool, string>;
"#};

stef_parser::Schema::parse(input).unwrap();
stef_parser::Schema::parse(input).unwrap();

b.iter(|| stef_parser::Schema::parse(black_box(input)))
});
bencher.bench(|| stef_parser::Schema::parse(black_box(input)));
}

for count in [1, 10, 100, 1000] {
let schema = generate_schema(count);
stef_parser::Schema::parse(&schema).unwrap();
#[divan::bench(consts = [1, 10, 100, 1000])]
fn large_schema<const N: usize>(bencher: Bencher) {
let schema = generate_schema(N);
stef_parser::Schema::parse(&schema).unwrap();

c.bench_with_input(BenchmarkId::new("large_schema", count), &count, |b, _| {
b.iter(|| stef_parser::Schema::parse(black_box(&schema)))
});
}
bencher.bench(|| stef_parser::Schema::parse(black_box(&schema)))
}

for count in [1, 10, 100, 1000] {
let schema = generate_schema(count);
let schema = stef_parser::Schema::parse(&schema).unwrap();
#[divan::bench(consts = [1, 10, 100, 1000])]
fn print<const N: usize>(bencher: Bencher) {
let schema = generate_schema(N);
let schema = stef_parser::Schema::parse(&schema).unwrap();

c.bench_with_input(BenchmarkId::new("print", count), &count, |b, _| {
b.iter(|| black_box(&schema).to_string())
});
}
bencher.bench(|| black_box(&schema).to_string())
}

fn generate_schema(count: usize) -> String {
Expand Down Expand Up @@ -150,6 +151,3 @@ fn generate_schema(count: usize) -> String {

input
}

criterion_group!(benches, parser);
criterion_main!(benches);
133 changes: 68 additions & 65 deletions crates/stef-benches/benches/varint.rs
Original file line number Diff line number Diff line change
@@ -1,75 +1,78 @@
use std::hint::black_box;

use criterion::{
criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion, PlotConfiguration,
};
use divan::black_box;
use stef_benches::varint;

fn varint(c: &mut Criterion) {
let mut g = c.benchmark_group("varint");
g.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
fn main() {
divan::main();
}

for i in [
1,
u8::MAX.into(),
u16::MAX.into(),
u32::MAX.into(),
u64::MAX.into(),
u128::MAX,
] {
g.bench_with_input(BenchmarkId::new("leb128", i), &i, |b, i| {
let mut buf = [0; 19];
let value = *i;
b.iter(|| {
varint::postcard::encode(black_box(value), black_box(&mut buf));
varint::postcard::decode(black_box(&buf))
});
});
g.bench_with_input(BenchmarkId::new("bincode", i), &i, |b, i| {
let mut buf = [0; 19];
let value = *i;
b.iter(|| {
varint::bincode::encode_u128(black_box(value), black_box(&mut buf));
varint::bincode::decode_u128(black_box(&buf))
});
});
trait Unsigned {
fn run(value: u128, buf: &mut [u8]) -> u128;
}

trait Signed {
fn run(value: i128, buf: &mut [u8]) -> i128;
}

struct Leb128;

impl Unsigned for Leb128 {
fn run(value: u128, buf: &mut [u8]) -> u128 {
varint::postcard::encode(value, buf);
varint::postcard::decode(buf)
}
}

impl Signed for Leb128 {
fn run(value: i128, buf: &mut [u8]) -> i128 {
varint::postcard::encode_i128(value, buf);
varint::postcard::decode_i128(buf)
}
g.finish();
}

let mut g = c.benchmark_group("varint_signed");
g.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
struct Bincode;

for i in [
-1,
i8::MIN.into(),
i16::MIN.into(),
i32::MIN.into(),
i64::MIN.into(),
i128::MIN,
] {
g.bench_with_input(BenchmarkId::new("leb128", i), &i, |b, i| {
let mut buf = [0; 19];
let value = *i;
b.iter(|| {
varint::postcard::encode_i128(black_box(value), black_box(&mut buf));
varint::postcard::decode_i128(black_box(&buf))
});
});
g.bench_with_input(BenchmarkId::new("bincode", i), &i, |b, i| {
let mut buf = [0; 19];
let value = *i;
b.iter(|| {
varint::bincode::encode_i128(black_box(value), black_box(&mut buf));
varint::bincode::decode_i128(black_box(&buf))
});
});
impl Unsigned for Bincode {
fn run(value: u128, buf: &mut [u8]) -> u128 {
varint::bincode::encode_u128(value, buf);
varint::bincode::decode_u128(buf)
}
}

impl Signed for Bincode {
fn run(value: i128, buf: &mut [u8]) -> i128 {
varint::bincode::encode_i128(value, buf);
varint::bincode::decode_i128(buf)
}
g.finish();
}

criterion_group! {
name = benches;
config = Criterion::default().plotting_backend(criterion::PlottingBackend::Plotters);
targets = varint
#[divan::bench(
types = [Leb128, Bincode],
consts = [
1,
u8::MAX as u128,
u16::MAX as u128,
u32::MAX as u128,
u64::MAX as u128,
u128::MAX,
],
)]
fn unsigned<const N: u128, T: Unsigned>() -> u128 {
let mut buf = [0; 19];
T::run(black_box(N), black_box(&mut buf))
}

#[divan::bench(
types = [Leb128, Bincode],
consts = [
-1,
i8::MIN as i128,
i16::MIN as i128,
i32::MIN as i128,
i64::MIN as i128,
i128::MIN,
],
)]
fn signed<const N: i128, T: Signed>() -> i128 {
let mut buf = [0; 19];
T::run(black_box(N), black_box(&mut buf))
}
criterion_main!(benches);
2 changes: 1 addition & 1 deletion crates/stef-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ prettyplease = "0.2.15"
proc-macro2 = { version = "1.0.67", default-features = false }
quote = { version = "1.0.33", default-features = false }
stef-parser = { path = "../stef-parser" }
syn = "2.0.37"
syn = "2.0.38"
thiserror = "1.0.49"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/stef-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0.67"
quote = "1.0.33"
syn = "2.0.37"
syn = "2.0.38"
2 changes: 1 addition & 1 deletion crates/stef-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rustc-args = ["--cfg", "docsrs"]
miette.workspace = true
owo-colors = { version = "3.5.0", features = ["supports-colors"] }
stef-derive = { path = "../stef-derive" }
winnow = "0.5.15"
winnow = "0.5.16"

[dev-dependencies]
indoc = "2.0.4"
Expand Down

0 comments on commit 3870a6c

Please sign in to comment.