Skip to content

Commit

Permalink
refactor: Consistently use criterion in benchmarks
Browse files Browse the repository at this point in the history
Rewrite the (only) divan benchmark to use criterion.

changelog: ignore
  • Loading branch information
jan-ferdinand committed Jan 22, 2025
1 parent ec1994e commit 872d42c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 96 deletions.
17 changes: 2 additions & 15 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,8 @@ jobs:
- name: Run clippy
run: cargo clippy --all-targets -- -D warnings

- name: Test triton-isa
run: cargo nextest run -p triton-isa --no-fail-fast --all-targets

- name: Test triton-constraint-circuit
run: cargo nextest run -p triton-constraint-circuit --no-fail-fast --all-targets

- name: Test triton-air
run: cargo nextest run -p triton-air --no-fail-fast --all-targets

- name: Test triton-constraint-builder
# without --all-targets because the divan benchmark doesn't play ball with nextest
run: cargo nextest run -p triton-constraint-builder --no-fail-fast

- name: Test triton-vm
run: cargo nextest run -p triton-vm --no-fail-fast --all-targets
- name: Run tests
run: cargo nextest run --no-fail-fast --all-targets

# doctests are special [^1] but this step does not incur a performance penalty [^2]
#
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ test-strategy = "0.4.0"
thiserror = "2.0"
twenty-first = "0.45.0"
unicode-width = "0.2"
divan = "0.1.17"

[workspace.lints.rust]
let_underscore_drop = "warn"
Expand Down
2 changes: 1 addition & 1 deletion triton-constraint-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ syn.workspace = true
twenty-first.workspace = true

[dev-dependencies]
criterion.workspace = true
insta.workspace = true
proptest.workspace = true
proptest-arbitrary-interop.workspace = true
test-strategy.workspace = true
divan.workspace = true

[lints]
workspace = true
Expand Down
127 changes: 48 additions & 79 deletions triton-constraint-builder/benches/degree_lowering.rs
Original file line number Diff line number Diff line change
@@ -1,91 +1,60 @@
use constraint_circuit::ConstraintCircuitMonad;
use divan::black_box;
use constraint_circuit::InputIndicator;
use criterion::black_box;
use criterion::criterion_group;
use criterion::criterion_main;
use criterion::measurement::WallTime;
use criterion::BatchSize::SmallInput;
use criterion::BenchmarkGroup;
use criterion::Criterion;
use triton_constraint_builder::Constraints;

fn main() {
divan::main();
criterion_main!(benches);
criterion_group! {
name = benches;
config = Criterion::default().sample_size(10);
targets = assemble_constraints,
degree_lower_constraint_types,
degree_lower_all,
}

#[divan::bench(sample_count = 10)]
fn assemble_constraints() {
black_box(Constraints::all());
fn assemble_constraints(criterion: &mut Criterion) {
criterion.bench_function("assemble all constraints", |bencher| {
bencher.iter(|| black_box(Constraints::all()))
});
}

#[divan::bench(sample_count = 10)]
fn initial_constraints(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let degree_lowering_info = Constraints::default_degree_lowering_info();
let constraints = Constraints::initial_constraints();
(degree_lowering_info, constraints)
})
.bench_values(|(degree_lowering_info, mut constraints)| {
black_box(ConstraintCircuitMonad::lower_to_degree(
&mut constraints,
degree_lowering_info,
));
});
fn degree_lower_constraint_types(c: &mut Criterion) {
let mut group = c.benchmark_group("lower to target degree");
degree_lower_constraints(&mut group, "init", Constraints::initial_constraints());
degree_lower_constraints(&mut group, "cons", Constraints::consistency_constraints());
degree_lower_constraints(&mut group, "tran", Constraints::transition_constraints());
degree_lower_constraints(&mut group, "term", Constraints::terminal_constraints());
group.finish();
}

#[divan::bench(sample_count = 10)]
fn consistency_constraints(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let degree_lowering_info = Constraints::default_degree_lowering_info();
let constraints = Constraints::consistency_constraints();
(degree_lowering_info, constraints)
})
.bench_values(|(degree_lowering_info, mut constraints)| {
black_box(ConstraintCircuitMonad::lower_to_degree(
&mut constraints,
degree_lowering_info,
));
});
fn degree_lower_constraints<II: InputIndicator>(
group: &mut BenchmarkGroup<WallTime>,
bench_name: &str,
constraints: Vec<ConstraintCircuitMonad<II>>,
) {
let info = Constraints::default_degree_lowering_info();
group.bench_function(bench_name, |bencher| {
bencher.iter_batched(
|| constraints.clone(),
|mut constr| black_box(ConstraintCircuitMonad::lower_to_degree(&mut constr, info)),
SmallInput,
)
});
}

#[divan::bench(sample_count = 1)]
fn transition_constraints(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let degree_lowering_info = Constraints::default_degree_lowering_info();
let constraints = Constraints::transition_constraints();
(degree_lowering_info, constraints)
})
.bench_values(|(degree_lowering_info, mut constraints)| {
black_box(ConstraintCircuitMonad::lower_to_degree(
&mut constraints,
degree_lowering_info,
));
});
}

#[divan::bench(sample_count = 10)]
fn terminal_constraints(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let degree_lowering_info = Constraints::default_degree_lowering_info();
let constraints = Constraints::terminal_constraints();
(degree_lowering_info, constraints)
})
.bench_values(|(degree_lowering_info, mut constraints)| {
black_box(ConstraintCircuitMonad::lower_to_degree(
&mut constraints,
degree_lowering_info,
));
});
}

#[divan::bench(sample_count = 1)]
fn degree_lower_all(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let constraints = Constraints::all();
let degree_lowering_info = Constraints::default_degree_lowering_info();
(degree_lowering_info, constraints)
})
.bench_values(|(degree_lowering_info, mut constraints)| {
black_box(
constraints.lower_to_target_degree_through_substitutions(degree_lowering_info),
);
});
fn degree_lower_all(criterion: &mut Criterion) {
let info = Constraints::default_degree_lowering_info();
criterion.bench_function("degree lower all constraints", |bencher| {
bencher.iter_batched(
Constraints::all,
|mut c| black_box(c.lower_to_target_degree_through_substitutions(info)),
SmallInput,
)
});
}

0 comments on commit 872d42c

Please sign in to comment.