1
+ use core:: hint:: black_box;
2
+
3
+ use benches:: bench;
1
4
use bevy_reflect:: func:: { ArgList , IntoFunction , IntoFunctionMut , TypedFunction } ;
2
- use criterion:: { criterion_group, BatchSize , Criterion } ;
5
+ use criterion:: { criterion_group, BatchSize , BenchmarkId , Criterion } ;
3
6
4
- criterion_group ! ( benches, typed, into, call, overload, clone) ;
7
+ criterion_group ! (
8
+ benches,
9
+ typed,
10
+ into,
11
+ call,
12
+ clone,
13
+ with_overload,
14
+ call_overload,
15
+ ) ;
5
16
6
17
fn add ( a : i32 , b : i32 ) -> i32 {
7
18
a + b
8
19
}
9
20
10
21
fn typed ( c : & mut Criterion ) {
11
- c. benchmark_group ( "typed" )
22
+ c. benchmark_group ( bench ! ( "typed" ) )
12
23
. bench_function ( "function" , |b| {
13
24
b. iter ( || add. get_function_info ( ) ) ;
14
25
} )
@@ -25,7 +36,7 @@ fn typed(c: &mut Criterion) {
25
36
}
26
37
27
38
fn into ( c : & mut Criterion ) {
28
- c. benchmark_group ( "into" )
39
+ c. benchmark_group ( bench ! ( "into" ) )
29
40
. bench_function ( "function" , |b| {
30
41
b. iter ( || add. into_function ( ) ) ;
31
42
} )
@@ -36,17 +47,18 @@ fn into(c: &mut Criterion) {
36
47
} )
37
48
. bench_function ( "closure_mut" , |b| {
38
49
let mut _capture = 25 ;
50
+ // `move` is required here because `into_function_mut()` takes ownership of `self`.
39
51
let closure = move |a : i32 | _capture += a;
40
52
b. iter ( || closure. into_function_mut ( ) ) ;
41
53
} ) ;
42
54
}
43
55
44
56
fn call ( c : & mut Criterion ) {
45
- c. benchmark_group ( "call" )
57
+ c. benchmark_group ( bench ! ( "call" ) )
46
58
. bench_function ( "trait_object" , |b| {
47
59
b. iter_batched (
48
60
|| Box :: new ( add) as Box < dyn Fn ( i32 , i32 ) -> i32 > ,
49
- |func| func ( 75 , 25 ) ,
61
+ |func| func ( black_box ( 75 ) , black_box ( 25 ) ) ,
50
62
BatchSize :: SmallInput ,
51
63
) ;
52
64
} )
@@ -78,35 +90,43 @@ fn call(c: &mut Criterion) {
78
90
} ) ;
79
91
}
80
92
81
- fn overload ( c : & mut Criterion ) {
82
- fn add < T : std:: ops:: Add < Output = T > > ( a : T , b : T ) -> T {
83
- a + b
84
- }
93
+ fn clone ( c : & mut Criterion ) {
94
+ c. benchmark_group ( bench ! ( "clone" ) )
95
+ . bench_function ( "function" , |b| {
96
+ let add = add. into_function ( ) ;
97
+ b. iter ( || add. clone ( ) ) ;
98
+ } ) ;
99
+ }
100
+
101
+ fn simple < T : std:: ops:: Add < Output = T > > ( a : T , b : T ) -> T {
102
+ a + b
103
+ }
85
104
86
- #[ expect( clippy:: too_many_arguments) ]
87
- fn complex < T0 , T1 , T2 , T3 , T4 , T5 , T6 , T7 , T8 , T9 > (
88
- _: T0 ,
89
- _: T1 ,
90
- _: T2 ,
91
- _: T3 ,
92
- _: T4 ,
93
- _: T5 ,
94
- _: T6 ,
95
- _: T7 ,
96
- _: T8 ,
97
- _: T9 ,
98
- ) {
99
- }
105
+ #[ expect( clippy:: too_many_arguments) ]
106
+ fn complex < T0 , T1 , T2 , T3 , T4 , T5 , T6 , T7 , T8 , T9 > (
107
+ _: T0 ,
108
+ _: T1 ,
109
+ _: T2 ,
110
+ _: T3 ,
111
+ _: T4 ,
112
+ _: T5 ,
113
+ _: T6 ,
114
+ _: T7 ,
115
+ _: T8 ,
116
+ _: T9 ,
117
+ ) {
118
+ }
100
119
101
- c. benchmark_group ( "with_overload" )
102
- . bench_function ( "01_simple_overload" , |b| {
120
+ fn with_overload ( c : & mut Criterion ) {
121
+ c. benchmark_group ( bench ! ( "with_overload" ) )
122
+ . bench_function ( BenchmarkId :: new ( "simple_overload" , 1 ) , |b| {
103
123
b. iter_batched (
104
- || add :: < i8 > . into_function ( ) ,
105
- |func| func. with_overload ( add :: < i16 > ) ,
124
+ || simple :: < i8 > . into_function ( ) ,
125
+ |func| func. with_overload ( simple :: < i16 > ) ,
106
126
BatchSize :: SmallInput ,
107
127
) ;
108
128
} )
109
- . bench_function ( "01_complex_overload" , |b| {
129
+ . bench_function ( BenchmarkId :: new ( "complex_overload" , 1 ) , |b| {
110
130
b. iter_batched (
111
131
|| complex :: < i8 , i16 , i32 , i64 , i128 , u8 , u16 , u32 , u64 , u128 > . into_function ( ) ,
112
132
|func| {
@@ -115,18 +135,18 @@ fn overload(c: &mut Criterion) {
115
135
BatchSize :: SmallInput ,
116
136
) ;
117
137
} )
118
- . bench_function ( "03_simple_overload" , |b| {
138
+ . bench_function ( BenchmarkId :: new ( "simple_overload" , 3 ) , |b| {
119
139
b. iter_batched (
120
- || add :: < i8 > . into_function ( ) ,
140
+ || simple :: < i8 > . into_function ( ) ,
121
141
|func| {
122
- func. with_overload ( add :: < i16 > )
123
- . with_overload ( add :: < i32 > )
124
- . with_overload ( add :: < i64 > )
142
+ func. with_overload ( simple :: < i16 > )
143
+ . with_overload ( simple :: < i32 > )
144
+ . with_overload ( simple :: < i64 > )
125
145
} ,
126
146
BatchSize :: SmallInput ,
127
147
) ;
128
148
} )
129
- . bench_function ( "03_complex_overload" , |b| {
149
+ . bench_function ( BenchmarkId :: new ( "complex_overload" , 3 ) , |b| {
130
150
b. iter_batched (
131
151
|| complex :: < i8 , i16 , i32 , i64 , i128 , u8 , u16 , u32 , u64 , u128 > . into_function ( ) ,
132
152
|func| {
@@ -137,24 +157,24 @@ fn overload(c: &mut Criterion) {
137
157
BatchSize :: SmallInput ,
138
158
) ;
139
159
} )
140
- . bench_function ( "10_simple_overload" , |b| {
160
+ . bench_function ( BenchmarkId :: new ( "simple_overload" , 10 ) , |b| {
141
161
b. iter_batched (
142
- || add :: < i8 > . into_function ( ) ,
162
+ || simple :: < i8 > . into_function ( ) ,
143
163
|func| {
144
- func. with_overload ( add :: < i16 > )
145
- . with_overload ( add :: < i32 > )
146
- . with_overload ( add :: < i64 > )
147
- . with_overload ( add :: < i128 > )
148
- . with_overload ( add :: < u8 > )
149
- . with_overload ( add :: < u16 > )
150
- . with_overload ( add :: < u32 > )
151
- . with_overload ( add :: < u64 > )
152
- . with_overload ( add :: < u128 > )
164
+ func. with_overload ( simple :: < i16 > )
165
+ . with_overload ( simple :: < i32 > )
166
+ . with_overload ( simple :: < i64 > )
167
+ . with_overload ( simple :: < i128 > )
168
+ . with_overload ( simple :: < u8 > )
169
+ . with_overload ( simple :: < u16 > )
170
+ . with_overload ( simple :: < u32 > )
171
+ . with_overload ( simple :: < u64 > )
172
+ . with_overload ( simple :: < u128 > )
153
173
} ,
154
174
BatchSize :: SmallInput ,
155
175
) ;
156
176
} )
157
- . bench_function ( "10_complex_overload" , |b| {
177
+ . bench_function ( BenchmarkId :: new ( "complex_overload" , 10 ) , |b| {
158
178
b. iter_batched (
159
179
|| complex :: < i8 , i16 , i32 , i64 , i128 , u8 , u16 , u32 , u64 , u128 > . into_function ( ) ,
160
180
|func| {
@@ -171,41 +191,41 @@ fn overload(c: &mut Criterion) {
171
191
BatchSize :: SmallInput ,
172
192
) ;
173
193
} )
174
- . bench_function ( "01_nested_simple_overload" , |b| {
194
+ . bench_function ( BenchmarkId :: new ( "nested_simple_overload" , 1 ) , |b| {
175
195
b. iter_batched (
176
- || add :: < i8 > . into_function ( ) ,
177
- |func| func. with_overload ( add :: < i16 > ) ,
196
+ || simple :: < i8 > . into_function ( ) ,
197
+ |func| func. with_overload ( simple :: < i16 > ) ,
178
198
BatchSize :: SmallInput ,
179
199
) ;
180
200
} )
181
- . bench_function ( "03_nested_simple_overload" , |b| {
201
+ . bench_function ( BenchmarkId :: new ( "nested_simple_overload" , 3 ) , |b| {
182
202
b. iter_batched (
183
- || add :: < i8 > . into_function ( ) ,
203
+ || simple :: < i8 > . into_function ( ) ,
184
204
|func| {
185
205
func. with_overload (
186
- add :: < i16 >
187
- . into_function ( )
188
- . with_overload ( add :: < i32 > . into_function ( ) . with_overload ( add :: < i64 > ) ) ,
206
+ simple :: < i16 > . into_function ( ) . with_overload (
207
+ simple :: < i32 > . into_function ( ) . with_overload ( simple :: < i64 > ) ,
208
+ ) ,
189
209
)
190
210
} ,
191
211
BatchSize :: SmallInput ,
192
212
) ;
193
213
} )
194
- . bench_function ( "10_nested_simple_overload" , |b| {
214
+ . bench_function ( BenchmarkId :: new ( "nested_simple_overload" , 10 ) , |b| {
195
215
b. iter_batched (
196
- || add :: < i8 > . into_function ( ) ,
216
+ || simple :: < i8 > . into_function ( ) ,
197
217
|func| {
198
218
func. with_overload (
199
- add :: < i16 > . into_function ( ) . with_overload (
200
- add :: < i32 > . into_function ( ) . with_overload (
201
- add :: < i64 > . into_function ( ) . with_overload (
202
- add :: < i128 > . into_function ( ) . with_overload (
203
- add :: < u8 > . into_function ( ) . with_overload (
204
- add :: < u16 > . into_function ( ) . with_overload (
205
- add :: < u32 > . into_function ( ) . with_overload (
206
- add :: < u64 >
219
+ simple :: < i16 > . into_function ( ) . with_overload (
220
+ simple :: < i32 > . into_function ( ) . with_overload (
221
+ simple :: < i64 > . into_function ( ) . with_overload (
222
+ simple :: < i128 > . into_function ( ) . with_overload (
223
+ simple :: < u8 > . into_function ( ) . with_overload (
224
+ simple :: < u16 > . into_function ( ) . with_overload (
225
+ simple :: < u32 > . into_function ( ) . with_overload (
226
+ simple :: < u64 >
207
227
. into_function ( )
208
- . with_overload ( add :: < u128 > ) ,
228
+ . with_overload ( simple :: < u128 > ) ,
209
229
) ,
210
230
) ,
211
231
) ,
@@ -218,21 +238,23 @@ fn overload(c: &mut Criterion) {
218
238
BatchSize :: SmallInput ,
219
239
) ;
220
240
} ) ;
241
+ }
221
242
222
- c. benchmark_group ( "call_overload" )
223
- . bench_function ( "01_simple_overload" , |b| {
243
+ fn call_overload ( c : & mut Criterion ) {
244
+ c. benchmark_group ( bench ! ( "call_overload" ) )
245
+ . bench_function ( BenchmarkId :: new ( "simple_overload" , 1 ) , |b| {
224
246
b. iter_batched (
225
247
|| {
226
248
(
227
- add :: < i8 > . into_function ( ) . with_overload ( add :: < i16 > ) ,
249
+ simple :: < i8 > . into_function ( ) . with_overload ( simple :: < i16 > ) ,
228
250
ArgList :: new ( ) . push_owned ( 75_i8 ) . push_owned ( 25_i8 ) ,
229
251
)
230
252
} ,
231
253
|( func, args) | func. call ( args) ,
232
254
BatchSize :: SmallInput ,
233
255
) ;
234
256
} )
235
- . bench_function ( "01_complex_overload" , |b| {
257
+ . bench_function ( BenchmarkId :: new ( "complex_overload" , 1 ) , |b| {
236
258
b. iter_batched (
237
259
|| {
238
260
(
@@ -258,23 +280,23 @@ fn overload(c: &mut Criterion) {
258
280
BatchSize :: SmallInput ,
259
281
) ;
260
282
} )
261
- . bench_function ( "03_simple_overload" , |b| {
283
+ . bench_function ( BenchmarkId :: new ( "simple_overload" , 3 ) , |b| {
262
284
b. iter_batched (
263
285
|| {
264
286
(
265
- add :: < i8 >
287
+ simple :: < i8 >
266
288
. into_function ( )
267
- . with_overload ( add :: < i16 > )
268
- . with_overload ( add :: < i32 > )
269
- . with_overload ( add :: < i64 > ) ,
289
+ . with_overload ( simple :: < i16 > )
290
+ . with_overload ( simple :: < i32 > )
291
+ . with_overload ( simple :: < i64 > ) ,
270
292
ArgList :: new ( ) . push_owned ( 75_i32 ) . push_owned ( 25_i32 ) ,
271
293
)
272
294
} ,
273
295
|( func, args) | func. call ( args) ,
274
296
BatchSize :: SmallInput ,
275
297
) ;
276
298
} )
277
- . bench_function ( "03_complex_overload" , |b| {
299
+ . bench_function ( BenchmarkId :: new ( "complex_overload" , 3 ) , |b| {
278
300
b. iter_batched (
279
301
|| {
280
302
(
@@ -306,29 +328,29 @@ fn overload(c: &mut Criterion) {
306
328
BatchSize :: SmallInput ,
307
329
) ;
308
330
} )
309
- . bench_function ( "10_simple_overload" , |b| {
331
+ . bench_function ( BenchmarkId :: new ( "simple_overload" , 10 ) , |b| {
310
332
b. iter_batched (
311
333
|| {
312
334
(
313
- add :: < i8 >
335
+ simple :: < i8 >
314
336
. into_function ( )
315
- . with_overload ( add :: < i16 > )
316
- . with_overload ( add :: < i32 > )
317
- . with_overload ( add :: < i64 > )
318
- . with_overload ( add :: < i128 > )
319
- . with_overload ( add :: < u8 > )
320
- . with_overload ( add :: < u16 > )
321
- . with_overload ( add :: < u32 > )
322
- . with_overload ( add :: < u64 > )
323
- . with_overload ( add :: < u128 > ) ,
337
+ . with_overload ( simple :: < i16 > )
338
+ . with_overload ( simple :: < i32 > )
339
+ . with_overload ( simple :: < i64 > )
340
+ . with_overload ( simple :: < i128 > )
341
+ . with_overload ( simple :: < u8 > )
342
+ . with_overload ( simple :: < u16 > )
343
+ . with_overload ( simple :: < u32 > )
344
+ . with_overload ( simple :: < u64 > )
345
+ . with_overload ( simple :: < u128 > ) ,
324
346
ArgList :: new ( ) . push_owned ( 75_u8 ) . push_owned ( 25_u8 ) ,
325
347
)
326
348
} ,
327
349
|( func, args) | func. call ( args) ,
328
350
BatchSize :: SmallInput ,
329
351
) ;
330
352
} )
331
- . bench_function ( "10_complex_overload" , |b| {
353
+ . bench_function ( BenchmarkId :: new ( "complex_overload" , 10 ) , |b| {
332
354
b. iter_batched (
333
355
|| {
334
356
(
@@ -379,10 +401,3 @@ fn overload(c: &mut Criterion) {
379
401
) ;
380
402
} ) ;
381
403
}
382
-
383
- fn clone ( c : & mut Criterion ) {
384
- c. benchmark_group ( "clone" ) . bench_function ( "function" , |b| {
385
- let add = add. into_function ( ) ;
386
- b. iter ( || add. clone ( ) ) ;
387
- } ) ;
388
- }
0 commit comments