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
6 changes: 5 additions & 1 deletion benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ license = "MIT OR Apache-2.0"
# Do not automatically discover benchmarks, we specify them manually instead.
autobenches = false

[dependencies]
# The primary crate that runs and analyzes our benchmarks. This is a regular dependency because the
# `bench!` macro refers to it in its documentation.
criterion = { version = "0.5.1", features = ["html_reports"] }

[dev-dependencies]
# Bevy crates
bevy_app = { path = "../crates/bevy_app" }
Expand All @@ -22,7 +27,6 @@ bevy_tasks = { path = "../crates/bevy_tasks" }
bevy_utils = { path = "../crates/bevy_utils" }

# Other crates
criterion = { version = "0.5.1", features = ["html_reports"] }
glam = "0.29"
rand = "0.8"
rand_chacha = "0.3"
Expand Down
205 changes: 110 additions & 95 deletions benches/benches/bevy_reflect/function.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
use core::hint::black_box;

use benches::bench;
use bevy_reflect::func::{ArgList, IntoFunction, IntoFunctionMut, TypedFunction};
use criterion::{criterion_group, BatchSize, Criterion};
use criterion::{criterion_group, BatchSize, BenchmarkId, Criterion};

criterion_group!(benches, typed, into, call, overload, clone);
criterion_group!(
benches,
typed,
into,
call,
clone,
with_overload,
call_overload,
);

fn add(a: i32, b: i32) -> i32 {
a + b
}

fn typed(c: &mut Criterion) {
c.benchmark_group("typed")
c.benchmark_group(bench!("typed"))
.bench_function("function", |b| {
b.iter(|| add.get_function_info());
})
Expand All @@ -25,7 +36,7 @@ fn typed(c: &mut Criterion) {
}

fn into(c: &mut Criterion) {
c.benchmark_group("into")
c.benchmark_group(bench!("into"))
.bench_function("function", |b| {
b.iter(|| add.into_function());
})
Expand All @@ -36,17 +47,18 @@ fn into(c: &mut Criterion) {
})
.bench_function("closure_mut", |b| {
let mut _capture = 25;
// `move` is required here because `into_function_mut()` takes ownership of `self`.
let closure = move |a: i32| _capture += a;
b.iter(|| closure.into_function_mut());
});
}

fn call(c: &mut Criterion) {
c.benchmark_group("call")
c.benchmark_group(bench!("call"))
.bench_function("trait_object", |b| {
b.iter_batched(
|| Box::new(add) as Box<dyn Fn(i32, i32) -> i32>,
|func| func(75, 25),
|func| func(black_box(75), black_box(25)),
BatchSize::SmallInput,
);
})
Expand Down Expand Up @@ -78,35 +90,43 @@ fn call(c: &mut Criterion) {
});
}

fn overload(c: &mut Criterion) {
fn add<T: std::ops::Add<Output = T>>(a: T, b: T) -> T {
a + b
}
fn clone(c: &mut Criterion) {
c.benchmark_group(bench!("clone"))
.bench_function("function", |b| {
let add = add.into_function();
b.iter(|| add.clone());
});
}

fn simple<T: std::ops::Add<Output = T>>(a: T, b: T) -> T {
a + b
}

#[expect(clippy::too_many_arguments)]
fn complex<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(
_: T0,
_: T1,
_: T2,
_: T3,
_: T4,
_: T5,
_: T6,
_: T7,
_: T8,
_: T9,
) {
}
#[expect(clippy::too_many_arguments)]
fn complex<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(
_: T0,
_: T1,
_: T2,
_: T3,
_: T4,
_: T5,
_: T6,
_: T7,
_: T8,
_: T9,
) {
}

c.benchmark_group("with_overload")
.bench_function("01_simple_overload", |b| {
fn with_overload(c: &mut Criterion) {
c.benchmark_group(bench!("with_overload"))
.bench_function(BenchmarkId::new("simple_overload", 1), |b| {
b.iter_batched(
|| add::<i8>.into_function(),
|func| func.with_overload(add::<i16>),
|| simple::<i8>.into_function(),
|func| func.with_overload(simple::<i16>),
BatchSize::SmallInput,
);
})
.bench_function("01_complex_overload", |b| {
.bench_function(BenchmarkId::new("complex_overload", 1), |b| {
b.iter_batched(
|| complex::<i8, i16, i32, i64, i128, u8, u16, u32, u64, u128>.into_function(),
|func| {
Expand All @@ -115,18 +135,18 @@ fn overload(c: &mut Criterion) {
BatchSize::SmallInput,
);
})
.bench_function("03_simple_overload", |b| {
.bench_function(BenchmarkId::new("simple_overload", 3), |b| {
b.iter_batched(
|| add::<i8>.into_function(),
|| simple::<i8>.into_function(),
|func| {
func.with_overload(add::<i16>)
.with_overload(add::<i32>)
.with_overload(add::<i64>)
func.with_overload(simple::<i16>)
.with_overload(simple::<i32>)
.with_overload(simple::<i64>)
},
BatchSize::SmallInput,
);
})
.bench_function("03_complex_overload", |b| {
.bench_function(BenchmarkId::new("complex_overload", 3), |b| {
b.iter_batched(
|| complex::<i8, i16, i32, i64, i128, u8, u16, u32, u64, u128>.into_function(),
|func| {
Expand All @@ -137,24 +157,24 @@ fn overload(c: &mut Criterion) {
BatchSize::SmallInput,
);
})
.bench_function("10_simple_overload", |b| {
.bench_function(BenchmarkId::new("simple_overload", 10), |b| {
b.iter_batched(
|| add::<i8>.into_function(),
|| simple::<i8>.into_function(),
|func| {
func.with_overload(add::<i16>)
.with_overload(add::<i32>)
.with_overload(add::<i64>)
.with_overload(add::<i128>)
.with_overload(add::<u8>)
.with_overload(add::<u16>)
.with_overload(add::<u32>)
.with_overload(add::<u64>)
.with_overload(add::<u128>)
func.with_overload(simple::<i16>)
.with_overload(simple::<i32>)
.with_overload(simple::<i64>)
.with_overload(simple::<i128>)
.with_overload(simple::<u8>)
.with_overload(simple::<u16>)
.with_overload(simple::<u32>)
.with_overload(simple::<u64>)
.with_overload(simple::<u128>)
},
BatchSize::SmallInput,
);
})
.bench_function("10_complex_overload", |b| {
.bench_function(BenchmarkId::new("complex_overload", 10), |b| {
b.iter_batched(
|| complex::<i8, i16, i32, i64, i128, u8, u16, u32, u64, u128>.into_function(),
|func| {
Expand All @@ -171,41 +191,41 @@ fn overload(c: &mut Criterion) {
BatchSize::SmallInput,
);
})
.bench_function("01_nested_simple_overload", |b| {
.bench_function(BenchmarkId::new("nested_simple_overload", 1), |b| {
b.iter_batched(
|| add::<i8>.into_function(),
|func| func.with_overload(add::<i16>),
|| simple::<i8>.into_function(),
|func| func.with_overload(simple::<i16>),
BatchSize::SmallInput,
);
})
.bench_function("03_nested_simple_overload", |b| {
.bench_function(BenchmarkId::new("nested_simple_overload", 3), |b| {
b.iter_batched(
|| add::<i8>.into_function(),
|| simple::<i8>.into_function(),
|func| {
func.with_overload(
add::<i16>
.into_function()
.with_overload(add::<i32>.into_function().with_overload(add::<i64>)),
simple::<i16>.into_function().with_overload(
simple::<i32>.into_function().with_overload(simple::<i64>),
),
)
},
BatchSize::SmallInput,
);
})
.bench_function("10_nested_simple_overload", |b| {
.bench_function(BenchmarkId::new("nested_simple_overload", 10), |b| {
b.iter_batched(
|| add::<i8>.into_function(),
|| simple::<i8>.into_function(),
|func| {
func.with_overload(
add::<i16>.into_function().with_overload(
add::<i32>.into_function().with_overload(
add::<i64>.into_function().with_overload(
add::<i128>.into_function().with_overload(
add::<u8>.into_function().with_overload(
add::<u16>.into_function().with_overload(
add::<u32>.into_function().with_overload(
add::<u64>
simple::<i16>.into_function().with_overload(
simple::<i32>.into_function().with_overload(
simple::<i64>.into_function().with_overload(
simple::<i128>.into_function().with_overload(
simple::<u8>.into_function().with_overload(
simple::<u16>.into_function().with_overload(
simple::<u32>.into_function().with_overload(
simple::<u64>
.into_function()
.with_overload(add::<u128>),
.with_overload(simple::<u128>),
),
),
),
Expand All @@ -218,21 +238,23 @@ fn overload(c: &mut Criterion) {
BatchSize::SmallInput,
);
});
}

c.benchmark_group("call_overload")
.bench_function("01_simple_overload", |b| {
fn call_overload(c: &mut Criterion) {
c.benchmark_group(bench!("call_overload"))
.bench_function(BenchmarkId::new("simple_overload", 1), |b| {
b.iter_batched(
|| {
(
add::<i8>.into_function().with_overload(add::<i16>),
simple::<i8>.into_function().with_overload(simple::<i16>),
ArgList::new().push_owned(75_i8).push_owned(25_i8),
)
},
|(func, args)| func.call(args),
BatchSize::SmallInput,
);
})
.bench_function("01_complex_overload", |b| {
.bench_function(BenchmarkId::new("complex_overload", 1), |b| {
b.iter_batched(
|| {
(
Expand All @@ -258,23 +280,23 @@ fn overload(c: &mut Criterion) {
BatchSize::SmallInput,
);
})
.bench_function("03_simple_overload", |b| {
.bench_function(BenchmarkId::new("simple_overload", 3), |b| {
b.iter_batched(
|| {
(
add::<i8>
simple::<i8>
.into_function()
.with_overload(add::<i16>)
.with_overload(add::<i32>)
.with_overload(add::<i64>),
.with_overload(simple::<i16>)
.with_overload(simple::<i32>)
.with_overload(simple::<i64>),
ArgList::new().push_owned(75_i32).push_owned(25_i32),
)
},
|(func, args)| func.call(args),
BatchSize::SmallInput,
);
})
.bench_function("03_complex_overload", |b| {
.bench_function(BenchmarkId::new("complex_overload", 3), |b| {
b.iter_batched(
|| {
(
Expand Down Expand Up @@ -306,29 +328,29 @@ fn overload(c: &mut Criterion) {
BatchSize::SmallInput,
);
})
.bench_function("10_simple_overload", |b| {
.bench_function(BenchmarkId::new("simple_overload", 10), |b| {
b.iter_batched(
|| {
(
add::<i8>
simple::<i8>
.into_function()
.with_overload(add::<i16>)
.with_overload(add::<i32>)
.with_overload(add::<i64>)
.with_overload(add::<i128>)
.with_overload(add::<u8>)
.with_overload(add::<u16>)
.with_overload(add::<u32>)
.with_overload(add::<u64>)
.with_overload(add::<u128>),
.with_overload(simple::<i16>)
.with_overload(simple::<i32>)
.with_overload(simple::<i64>)
.with_overload(simple::<i128>)
.with_overload(simple::<u8>)
.with_overload(simple::<u16>)
.with_overload(simple::<u32>)
.with_overload(simple::<u64>)
.with_overload(simple::<u128>),
ArgList::new().push_owned(75_u8).push_owned(25_u8),
)
},
|(func, args)| func.call(args),
BatchSize::SmallInput,
);
})
.bench_function("10_complex_overload", |b| {
.bench_function(BenchmarkId::new("complex_overload", 10), |b| {
b.iter_batched(
|| {
(
Expand Down Expand Up @@ -379,10 +401,3 @@ fn overload(c: &mut Criterion) {
);
});
}

fn clone(c: &mut Criterion) {
c.benchmark_group("clone").bench_function("function", |b| {
let add = add.into_function();
b.iter(|| add.clone());
});
}
Loading
Loading