Skip to content

Commit 291cb31

Browse files
Overhaul bezier curve benchmarks (#17016)
# Objective - Part of #16647. - The benchmarks for bezier curves have several issues and do not yet use the new `bench!` naming scheme. ## Solution - Make all `bevy_math` benchmarks use the `bench!` macro for their name. - Delete the `build_accel_cubic()` benchmark, since it was an exact duplicate of `build_pos_cubic()`. - Remove `collect::<Vec<_>>()` call in `build_pos_cubic()` and replace it with a `for` loop. - Combine all of the benchmarks that measure `curve.position()` under a single group, `curve_position`, and extract the common bench routine into a helper function. - Move the time calculation for the `curve.ease()` benchmark into the setup closure so it is not tracked. - Rename the benchmarks to be more descriptive on what they do. - `easing_1000` -> `segment_ease` - `cubic_position_Vec2` -> `curve_position/vec2` - `cubic_position_Vec3A` -> `curve_position/vec3a` - `cubic_position_Vec3` -> `curve_position/vec3` - `build_pos_cubic_100_points` -> `curve_iter_positions` ## Testing - `cargo test -p benches --bench math` - `cargo bench -p benches --bench math` - Then open `./target/criterion/report/index.html` to see the report! --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
1 parent 8c34f00 commit 291cb31

File tree

1 file changed

+60
-57
lines changed

1 file changed

+60
-57
lines changed

benches/benches/bevy_math/bezier.rs

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,99 @@
1+
use benches::bench;
2+
use bevy_math::{prelude::*, VectorSpace};
13
use core::hint::black_box;
4+
use criterion::{
5+
criterion_group, measurement::Measurement, BatchSize, BenchmarkGroup, BenchmarkId, Criterion,
6+
};
27

3-
use criterion::{criterion_group, Criterion};
8+
criterion_group!(benches, segment_ease, curve_position, curve_iter_positions);
49

5-
use bevy_math::prelude::*;
10+
fn segment_ease(c: &mut Criterion) {
11+
let segment = black_box(CubicSegment::new_bezier(vec2(0.25, 0.1), vec2(0.25, 1.0)));
612

7-
fn easing(c: &mut Criterion) {
8-
let cubic_bezier = CubicSegment::new_bezier(vec2(0.25, 0.1), vec2(0.25, 1.0));
9-
c.bench_function("easing_1000", |b| {
10-
b.iter(|| {
11-
(0..1000).map(|i| i as f32 / 1000.0).for_each(|t| {
12-
black_box(cubic_bezier.ease(black_box(t)));
13-
});
14-
});
13+
c.bench_function(bench!("segment_ease"), |b| {
14+
let mut t = 0;
15+
16+
b.iter_batched(
17+
|| {
18+
// Increment `t` by 1, but use modulo to constrain it to `0..=1000`.
19+
t = (t + 1) % 1001;
20+
21+
// Return time as a decimal between 0 and 1, inclusive.
22+
t as f32 / 1000.0
23+
},
24+
|t| segment.ease(t),
25+
BatchSize::SmallInput,
26+
);
1527
});
1628
}
1729

18-
fn cubic_2d(c: &mut Criterion) {
19-
let bezier = CubicBezier::new([[
30+
fn curve_position(c: &mut Criterion) {
31+
/// A helper function that benchmarks calling [`CubicCurve::position()`] over a generic [`VectorSpace`].
32+
fn bench_curve<M: Measurement, P: VectorSpace>(
33+
group: &mut BenchmarkGroup<M>,
34+
name: &str,
35+
curve: CubicCurve<P>,
36+
) {
37+
group.bench_with_input(BenchmarkId::from_parameter(name), &curve, |b, curve| {
38+
b.iter(|| curve.position(black_box(0.5)));
39+
});
40+
}
41+
42+
let mut group = c.benchmark_group(bench!("curve_position"));
43+
44+
let bezier_2 = CubicBezier::new([[
2045
vec2(0.0, 0.0),
2146
vec2(0.0, 1.0),
2247
vec2(1.0, 0.0),
2348
vec2(1.0, 1.0),
2449
]])
2550
.to_curve()
26-
.expect("Unable to build a curve from this data");
27-
c.bench_function("cubic_position_Vec2", |b| {
28-
b.iter(|| black_box(bezier.position(black_box(0.5))));
29-
});
30-
}
51+
.unwrap();
3152

32-
fn cubic(c: &mut Criterion) {
33-
let bezier = CubicBezier::new([[
34-
vec3a(0.0, 0.0, 0.0),
35-
vec3a(0.0, 1.0, 0.0),
36-
vec3a(1.0, 0.0, 0.0),
37-
vec3a(1.0, 1.0, 1.0),
38-
]])
39-
.to_curve()
40-
.expect("Unable to build a curve from this data");
41-
c.bench_function("cubic_position_Vec3A", |b| {
42-
b.iter(|| black_box(bezier.position(black_box(0.5))));
43-
});
44-
}
53+
bench_curve(&mut group, "vec2", bezier_2);
4554

46-
fn cubic_vec3(c: &mut Criterion) {
47-
let bezier = CubicBezier::new([[
55+
let bezier_3 = CubicBezier::new([[
4856
vec3(0.0, 0.0, 0.0),
4957
vec3(0.0, 1.0, 0.0),
5058
vec3(1.0, 0.0, 0.0),
5159
vec3(1.0, 1.0, 1.0),
5260
]])
5361
.to_curve()
54-
.expect("Unable to build a curve from this data");
55-
c.bench_function("cubic_position_Vec3", |b| {
56-
b.iter(|| black_box(bezier.position(black_box(0.5))));
57-
});
58-
}
62+
.unwrap();
5963

60-
fn build_pos_cubic(c: &mut Criterion) {
61-
let bezier = CubicBezier::new([[
64+
bench_curve(&mut group, "vec3", bezier_3);
65+
66+
let bezier_3a = CubicBezier::new([[
6267
vec3a(0.0, 0.0, 0.0),
6368
vec3a(0.0, 1.0, 0.0),
6469
vec3a(1.0, 0.0, 0.0),
6570
vec3a(1.0, 1.0, 1.0),
6671
]])
6772
.to_curve()
68-
.expect("Unable to build a curve from this data");
69-
c.bench_function("build_pos_cubic_100_points", |b| {
70-
b.iter(|| black_box(bezier.iter_positions(black_box(100)).collect::<Vec<_>>()));
71-
});
73+
.unwrap();
74+
75+
bench_curve(&mut group, "vec3a", bezier_3a);
76+
77+
group.finish();
7278
}
7379

74-
fn build_accel_cubic(c: &mut Criterion) {
80+
fn curve_iter_positions(c: &mut Criterion) {
7581
let bezier = CubicBezier::new([[
7682
vec3a(0.0, 0.0, 0.0),
7783
vec3a(0.0, 1.0, 0.0),
7884
vec3a(1.0, 0.0, 0.0),
7985
vec3a(1.0, 1.0, 1.0),
8086
]])
8187
.to_curve()
82-
.expect("Unable to build a curve from this data");
83-
c.bench_function("build_accel_cubic_100_points", |b| {
84-
b.iter(|| black_box(bezier.iter_positions(black_box(100)).collect::<Vec<_>>()));
88+
.unwrap();
89+
90+
c.bench_function(bench!("curve_iter_positions"), |b| {
91+
b.iter(|| {
92+
for x in bezier.iter_positions(black_box(100)) {
93+
// Discard `x`, since we just care about `iter_positions()` being consumed, but make
94+
// the compiler believe `x` is being used so it doesn't eliminate the iterator.
95+
black_box(x);
96+
}
97+
});
8598
});
8699
}
87-
88-
criterion_group!(
89-
benches,
90-
easing,
91-
cubic_2d,
92-
cubic_vec3,
93-
cubic,
94-
build_pos_cubic,
95-
build_accel_cubic,
96-
);

0 commit comments

Comments
 (0)