diff --git a/benches/benches/bevy_reflect/function.rs b/benches/benches/bevy_reflect/function.rs index 03673d3a9a..c46a43420d 100644 --- a/benches/benches/bevy_reflect/function.rs +++ b/benches/benches/bevy_reflect/function.rs @@ -1,7 +1,7 @@ use bevy_reflect::func::{ArgList, IntoFunction, IntoFunctionMut, TypedFunction}; use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; -criterion_group!(benches, typed, into, call, clone); +criterion_group!(benches, typed, into, call, overload, clone); criterion_main!(benches); fn add(a: i32, b: i32) -> i32 { @@ -79,6 +79,148 @@ fn call(c: &mut Criterion) { }); } +fn overload(c: &mut Criterion) { + fn add>(a: T, b: T) -> T { + a + b + } + + c.benchmark_group("with_overload") + .bench_function("01_overload", |b| { + b.iter_batched( + || add::.into_function(), + |func| func.with_overload(add::), + BatchSize::SmallInput, + ); + }) + .bench_function("03_overload", |b| { + b.iter_batched( + || add::.into_function(), + |func| { + func.with_overload(add::) + .with_overload(add::) + .with_overload(add::) + }, + BatchSize::SmallInput, + ); + }) + .bench_function("10_overload", |b| { + b.iter_batched( + || add::.into_function(), + |func| { + func.with_overload(add::) + .with_overload(add::) + .with_overload(add::) + .with_overload(add::) + .with_overload(add::) + .with_overload(add::) + .with_overload(add::) + .with_overload(add::) + .with_overload(add::) + }, + BatchSize::SmallInput, + ); + }) + .bench_function("01_nested_overload", |b| { + b.iter_batched( + || add::.into_function(), + |func| func.with_overload(add::), + BatchSize::SmallInput, + ); + }) + .bench_function("03_nested_overload", |b| { + b.iter_batched( + || add::.into_function(), + |func| { + func.with_overload( + add:: + .into_function() + .with_overload(add::.into_function().with_overload(add::)), + ) + }, + BatchSize::SmallInput, + ); + }) + .bench_function("10_nested_overload", |b| { + b.iter_batched( + || add::.into_function(), + |func| { + func.with_overload( + add::.into_function().with_overload( + add::.into_function().with_overload( + add::.into_function().with_overload( + add::.into_function().with_overload( + add::.into_function().with_overload( + add::.into_function().with_overload( + add::.into_function().with_overload( + add:: + .into_function() + .with_overload(add::), + ), + ), + ), + ), + ), + ), + ), + ) + }, + BatchSize::SmallInput, + ); + }); + + c.benchmark_group("call_overload") + .bench_function("01_overload", |b| { + b.iter_batched( + || { + ( + add::.into_function().with_overload(add::), + ArgList::new().push_owned(75_i8).push_owned(25_i8), + ) + }, + |(func, args)| func.call(args), + BatchSize::SmallInput, + ); + }) + .bench_function("03_overload", |b| { + b.iter_batched( + || { + ( + add:: + .into_function() + .with_overload(add::) + .with_overload(add::) + .with_overload(add::), + ArgList::new().push_owned(75_i32).push_owned(25_i32), + ) + }, + |(func, args)| func.call(args), + BatchSize::SmallInput, + ); + }) + .bench_function("10_overload", |b| { + b.iter_batched( + || { + ( + add:: + .into_function() + .with_overload(add::) + .with_overload(add::) + .with_overload(add::) + .with_overload(add::) + .with_overload(add::) + .with_overload(add::) + .with_overload(add::) + .with_overload(add::) + .with_overload(add::), + ArgList::new().push_owned(75_u8).push_owned(25_u8), + ) + }, + |(func, args)| func.call(args), + BatchSize::SmallInput, + ); + }); +} + fn clone(c: &mut Criterion) { c.benchmark_group("clone").bench_function("function", |b| { let add = add.into_function();