1
- use std:: { cell:: RefCell , rc:: Rc } ;
2
-
3
1
use codspeed:: codspeed:: { black_box, CodSpeed } ;
4
2
use colored:: Colorize ;
5
3
use criterion:: BatchSize ;
@@ -10,36 +8,30 @@ use criterion::async_executor::AsyncExecutor;
10
8
use std:: future:: Future ;
11
9
12
10
pub struct Bencher < ' a > {
13
- codspeed : Rc < RefCell < CodSpeed > > ,
11
+ codspeed : & ' a mut CodSpeed ,
14
12
uri : String ,
15
- _marker : std:: marker:: PhantomData < & ' a ( ) > ,
16
13
}
17
14
18
15
#[ allow( clippy:: needless_lifetimes) ]
19
16
impl < ' a > Bencher < ' a > {
20
- pub fn new ( codspeed : Rc < RefCell < CodSpeed > > , uri : String ) -> Self {
21
- Bencher {
22
- codspeed,
23
- uri,
24
- _marker : std:: marker:: PhantomData ,
25
- }
17
+ pub ( crate ) fn new ( codspeed : & ' a mut CodSpeed , uri : String ) -> Self {
18
+ Bencher { codspeed, uri }
26
19
}
27
20
28
21
#[ inline( never) ]
29
22
pub fn iter < O , R > ( & mut self , mut routine : R )
30
23
where
31
24
R : FnMut ( ) -> O ,
32
25
{
33
- let mut codspeed = self . codspeed . borrow_mut ( ) ;
34
26
// NOTE: this structure hardens our benchmark against dead code elimination
35
27
// https://godbolt.org/z/KnYeKMd1o
36
28
for i in 0 ..codspeed:: codspeed:: WARMUP_RUNS + 1 {
37
29
if i < codspeed:: codspeed:: WARMUP_RUNS {
38
30
black_box ( routine ( ) ) ;
39
31
} else {
40
- codspeed. start_benchmark ( self . uri . as_str ( ) ) ;
32
+ self . codspeed . start_benchmark ( self . uri . as_str ( ) ) ;
41
33
black_box ( routine ( ) ) ;
42
- codspeed. end_benchmark ( ) ;
34
+ self . codspeed . end_benchmark ( ) ;
43
35
}
44
36
}
45
37
}
@@ -62,17 +54,15 @@ impl<'a> Bencher<'a> {
62
54
S : FnMut ( ) -> I ,
63
55
R : FnMut ( I ) -> O ,
64
56
{
65
- let mut codspeed = self . codspeed . borrow_mut ( ) ;
66
-
67
57
for i in 0 ..codspeed:: codspeed:: WARMUP_RUNS + 1 {
68
58
let input = black_box ( setup ( ) ) ;
69
59
let output = if i < codspeed:: codspeed:: WARMUP_RUNS {
70
60
routine ( input)
71
61
} else {
72
62
let input = black_box ( setup ( ) ) ;
73
- codspeed. start_benchmark ( self . uri . as_str ( ) ) ;
63
+ self . codspeed . start_benchmark ( self . uri . as_str ( ) ) ;
74
64
let output = routine ( input) ;
75
- codspeed. end_benchmark ( ) ;
65
+ self . codspeed . end_benchmark ( ) ;
76
66
output
77
67
} ;
78
68
drop ( black_box ( output) ) ;
@@ -108,16 +98,14 @@ impl<'a> Bencher<'a> {
108
98
S : FnMut ( ) -> I ,
109
99
R : FnMut ( & mut I ) -> O ,
110
100
{
111
- let mut codspeed = self . codspeed . borrow_mut ( ) ;
112
-
113
101
for i in 0 ..codspeed:: codspeed:: WARMUP_RUNS + 1 {
114
102
let mut input = black_box ( setup ( ) ) ;
115
103
let output = if i < codspeed:: codspeed:: WARMUP_RUNS {
116
104
black_box ( routine ( & mut input) )
117
105
} else {
118
- codspeed. start_benchmark ( self . uri . as_str ( ) ) ;
106
+ self . codspeed . start_benchmark ( self . uri . as_str ( ) ) ;
119
107
let output = black_box ( routine ( & mut input) ) ;
120
- codspeed. end_benchmark ( ) ;
108
+ self . codspeed . end_benchmark ( ) ;
121
109
output
122
110
} ;
123
111
drop ( black_box ( output) ) ;
@@ -149,14 +137,13 @@ impl<'a, 'b, A: AsyncExecutor> AsyncBencher<'a, 'b, A> {
149
137
{
150
138
let AsyncBencher { b, runner } = self ;
151
139
runner. block_on ( async {
152
- let mut codspeed = b. codspeed . borrow_mut ( ) ;
153
140
for i in 0 ..codspeed:: codspeed:: WARMUP_RUNS + 1 {
154
141
if i < codspeed:: codspeed:: WARMUP_RUNS {
155
142
black_box ( routine ( ) . await ) ;
156
143
} else {
157
- codspeed. start_benchmark ( b. uri . as_str ( ) ) ;
144
+ b . codspeed . start_benchmark ( b. uri . as_str ( ) ) ;
158
145
black_box ( routine ( ) . await ) ;
159
- codspeed. end_benchmark ( ) ;
146
+ b . codspeed . end_benchmark ( ) ;
160
147
}
161
148
}
162
149
} ) ;
@@ -214,16 +201,14 @@ impl<'a, 'b, A: AsyncExecutor> AsyncBencher<'a, 'b, A> {
214
201
{
215
202
let AsyncBencher { b, runner } = self ;
216
203
runner. block_on ( async {
217
- let mut codspeed = b. codspeed . borrow_mut ( ) ;
218
-
219
204
for i in 0 ..codspeed:: codspeed:: WARMUP_RUNS + 1 {
220
205
let input = black_box ( setup ( ) ) ;
221
206
let output = if i < codspeed:: codspeed:: WARMUP_RUNS {
222
207
routine ( input) . await
223
208
} else {
224
- codspeed. start_benchmark ( b. uri . as_str ( ) ) ;
209
+ b . codspeed . start_benchmark ( b. uri . as_str ( ) ) ;
225
210
let output = routine ( input) . await ;
226
- codspeed. end_benchmark ( ) ;
211
+ b . codspeed . end_benchmark ( ) ;
227
212
output
228
213
} ;
229
214
drop ( black_box ( output) ) ;
@@ -245,16 +230,14 @@ impl<'a, 'b, A: AsyncExecutor> AsyncBencher<'a, 'b, A> {
245
230
{
246
231
let AsyncBencher { b, runner } = self ;
247
232
runner. block_on ( async {
248
- let mut codspeed = b. codspeed . borrow_mut ( ) ;
249
-
250
233
for i in 0 ..codspeed:: codspeed:: WARMUP_RUNS + 1 {
251
234
let mut input = black_box ( setup ( ) ) ;
252
235
let output = if i < codspeed:: codspeed:: WARMUP_RUNS {
253
236
black_box ( routine ( & mut input) . await )
254
237
} else {
255
- codspeed. start_benchmark ( b. uri . as_str ( ) ) ;
238
+ b . codspeed . start_benchmark ( b. uri . as_str ( ) ) ;
256
239
let output = black_box ( routine ( & mut input) . await ) ;
257
- codspeed. end_benchmark ( ) ;
240
+ b . codspeed . end_benchmark ( ) ;
258
241
output
259
242
} ;
260
243
drop ( black_box ( output) ) ;
@@ -263,3 +246,17 @@ impl<'a, 'b, A: AsyncExecutor> AsyncBencher<'a, 'b, A> {
263
246
} ) ;
264
247
}
265
248
}
249
+
250
+ #[ cfg( test) ]
251
+ mod tests {
252
+ use super :: * ;
253
+
254
+ fn assert_send < T : Send > ( ) { }
255
+ fn assert_sync < T : Sync > ( ) { }
256
+
257
+ #[ test]
258
+ fn test_auto_traits ( ) {
259
+ assert_send :: < Bencher > ( ) ;
260
+ assert_sync :: < Bencher > ( ) ;
261
+ }
262
+ }
0 commit comments