Skip to content
Merged
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
524 changes: 198 additions & 326 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[workspace]
resolver = "3"
members = [
"ghash",
"poly1305",
"polyval"
]
resolver = "2"

[patch.crates-io]
polyval = { path = "./polyval" }
24 changes: 24 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "benches"
version = "0.0.0"
authors = ["RustCrypto Developers"]
license = "Apache-2.0 OR MIT"
description = "Criterion benchmarks of the universal-hashes crates"
edition = "2021"
publish = false

[workspace]

[dependencies]
polyval = { path = "../polyval/" }

criterion = "0.8"
criterion-cycles-per-byte = { version = "0.8", optional = true }

[features]
cpb = ["dep:criterion-cycles-per-byte"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should adapt this approach to other benchmark crates as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? It can be completely automatic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it looks really weird to switch measurements based on your target. Additionally, proper CPB measurement often requires additional setup as documented in the crate docs, so I think it's better to make this switch explicit.

Copy link
Member

@tarcieri tarcieri Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's weird to opportunistically measure CPB where the feature is available. I think it's weird to have to turn it on when it could be automatic. Again, it's not something that's easily discoverable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default you are likely to get low-quality data or failing bench. For example, on AMD x86 you should use RDPRU, which requires passing a separate flag and pinning your benchmark. On ARM you may need a whole separate dance.


[[bench]]
name = "polyval"
path = "src/polyval.rs"
harness = false
10 changes: 10 additions & 0 deletions benches/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Criterion Benchmarks

This crate contains [`criterion`]-based benchmarks of the universal-hashes crates.

Supports cycles-per-byte benchmarks behind the `cpb` crate feature.
Read the [`criterion-cycles-per-byte`] docs for additional information
about supported targets and proper configuration of such measurements.

[`criterion`]: https://github.com/criterion-rs/criterion.rs
[`criterion-cycles-per-byte`]: https://github.com/criterion-rs/criterion-cycles-per-byte
31 changes: 31 additions & 0 deletions benches/src/polyval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! POLYVAL benchmarks.
use criterion::{criterion_group, criterion_main, BenchmarkId, Throughput};
use polyval::{universal_hash::UniversalHash, Polyval};

mod utils;
use utils::{config, Benchmarker};

fn bench(c: &mut Benchmarker) {
let mut group = c.benchmark_group("polyval");

for size in &[10, 100, 1000, 10000] {
let buf = vec![0u8; *size];

group.throughput(Throughput::Bytes(*size as u64));

group.bench_function(BenchmarkId::new("update_padded", size), |b| {
let mut polyval = Polyval::new(&Default::default());
b.iter(|| polyval.update_padded(&buf));
});
}

group.finish();
}

criterion_group!(
name = benches;
config = config();
targets = bench
);

criterion_main!(benches);
17 changes: 17 additions & 0 deletions benches/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use criterion::Criterion;

#[cfg(not(feature = "cpb"))]
pub type Benchmarker = Criterion;

#[cfg(feature = "cpb")]
pub type Benchmarker = Criterion<criterion_cycles_per_byte::CyclesPerByte>;

#[cfg(not(feature = "cpb"))]
pub fn config() -> Benchmarker {
Criterion::default()
}

#[cfg(feature = "cpb")]
pub fn config() -> Benchmarker {
Criterion::default().with_measurement(criterion_cycles_per_byte::CyclesPerByte)
}
8 changes: 4 additions & 4 deletions ghash/benches/ghash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ macro_rules! bench {
};
}

bench!(bench1_10, 10);
bench!(bench2_100, 100);
bench!(bench3_1000, 1000);
bench!(bench3_10000, 10000);
bench!(bench_ghash_1_10, 10);
bench!(bench_ghash_2_100, 100);
bench!(bench_ghash_3_1000, 1000);
bench!(bench_ghash_3_10000, 10000);
8 changes: 4 additions & 4 deletions poly1305/benches/poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ macro_rules! bench {
};
}

bench!(bench1_10, 10);
bench!(bench2_100, 100);
bench!(bench3_1000, 1000);
bench!(bench3_10000, 10000);
bench!(bench_poly1305_1_10, 10);
bench!(bench_poly1305_2_100, 100);
bench!(bench_poly1305_3_1000, 1000);
bench!(bench_poly1305_4_10000, 10000);
6 changes: 0 additions & 6 deletions polyval/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,8 @@ cpufeatures = "0.3"
hex-literal = "1"

[target.'cfg(any(unix, windows))'.dev-dependencies]
criterion = "0.7"
proptest = "1.9"

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

[lints.rust]
missing_copy_implementations = "warn"
missing_debug_implementations = "warn"
Expand Down
50 changes: 25 additions & 25 deletions polyval/benches/polyval.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
//! POLYVAL benchmarks.

#![allow(missing_docs)]
#![feature(test)]

use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use polyval::{Polyval, universal_hash::UniversalHash};

fn bench(c: &mut Criterion) {
let mut group = c.benchmark_group("polyval");

for size in &[10, 100, 1000, 10000] {
let buf = vec![0u8; *size];

group.throughput(Throughput::Bytes(*size as u64));
extern crate test;

group.bench_function(BenchmarkId::new("update_padded", size), |b| {
let mut polyval = Polyval::new(&Default::default());
b.iter(|| polyval.update_padded(&buf));
});
}

group.finish();
use polyval::{Polyval, universal_hash::UniversalHash};
use test::Bencher;

// TODO(tarcieri): move this into the `universal-hash` crate
macro_rules! bench {
($name:ident, $bs:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
let key = Default::default();
let mut m = Polyval::new(&key);
let data = [0; $bs];

b.iter(|| {
m.update_padded(&data);
});

b.bytes = $bs;
}
};
}

criterion_group!(
name = benches;
config = Criterion::default();
targets = bench
);

criterion_main!(benches);
bench!(bench_polyval_1_10, 10);
bench!(bench_polyval_2_100, 100);
bench!(bench_polyval_3_1000, 1000);
bench!(bench_polyval_4_10000, 10000);