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,27 +8,22 @@ 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 ( ) ;
26
+ let codspeed = & mut * self . codspeed ;
34
27
// NOTE: this structure hardens our benchmark against dead code elimination
35
28
// https://godbolt.org/z/KnYeKMd1o
36
29
for i in 0 ..codspeed:: codspeed:: WARMUP_RUNS + 1 {
@@ -62,7 +55,7 @@ impl<'a> Bencher<'a> {
62
55
S : FnMut ( ) -> I ,
63
56
R : FnMut ( I ) -> O ,
64
57
{
65
- let mut codspeed = self . codspeed . borrow_mut ( ) ;
58
+ let codspeed = & mut * self . codspeed ;
66
59
67
60
for i in 0 ..codspeed:: codspeed:: WARMUP_RUNS + 1 {
68
61
let input = black_box ( setup ( ) ) ;
@@ -108,7 +101,7 @@ impl<'a> Bencher<'a> {
108
101
S : FnMut ( ) -> I ,
109
102
R : FnMut ( & mut I ) -> O ,
110
103
{
111
- let mut codspeed = self . codspeed . borrow_mut ( ) ;
104
+ let codspeed = & mut * self . codspeed ;
112
105
113
106
for i in 0 ..codspeed:: codspeed:: WARMUP_RUNS + 1 {
114
107
let mut input = black_box ( setup ( ) ) ;
@@ -149,7 +142,7 @@ impl<'a, 'b, A: AsyncExecutor> AsyncBencher<'a, 'b, A> {
149
142
{
150
143
let AsyncBencher { b, runner } = self ;
151
144
runner. block_on ( async {
152
- let mut codspeed = b. codspeed . borrow_mut ( ) ;
145
+ let codspeed = & mut * b. codspeed ;
153
146
for i in 0 ..codspeed:: codspeed:: WARMUP_RUNS + 1 {
154
147
if i < codspeed:: codspeed:: WARMUP_RUNS {
155
148
black_box ( routine ( ) . await ) ;
@@ -214,7 +207,7 @@ impl<'a, 'b, A: AsyncExecutor> AsyncBencher<'a, 'b, A> {
214
207
{
215
208
let AsyncBencher { b, runner } = self ;
216
209
runner. block_on ( async {
217
- let mut codspeed = b. codspeed . borrow_mut ( ) ;
210
+ let codspeed = & mut * b. codspeed ;
218
211
219
212
for i in 0 ..codspeed:: codspeed:: WARMUP_RUNS + 1 {
220
213
let input = black_box ( setup ( ) ) ;
@@ -245,7 +238,7 @@ impl<'a, 'b, A: AsyncExecutor> AsyncBencher<'a, 'b, A> {
245
238
{
246
239
let AsyncBencher { b, runner } = self ;
247
240
runner. block_on ( async {
248
- let mut codspeed = b. codspeed . borrow_mut ( ) ;
241
+ let codspeed = & mut * b. codspeed ;
249
242
250
243
for i in 0 ..codspeed:: codspeed:: WARMUP_RUNS + 1 {
251
244
let mut input = black_box ( setup ( ) ) ;
@@ -263,3 +256,17 @@ impl<'a, 'b, A: AsyncExecutor> AsyncBencher<'a, 'b, A> {
263
256
} ) ;
264
257
}
265
258
}
259
+
260
+ #[ cfg( test) ]
261
+ mod tests {
262
+ use super :: * ;
263
+
264
+ fn assert_send < T : Send > ( ) { }
265
+ fn assert_sync < T : Sync > ( ) { }
266
+
267
+ #[ test]
268
+ fn test_auto_traits ( ) {
269
+ assert_send :: < Bencher > ( ) ;
270
+ assert_sync :: < Bencher > ( ) ;
271
+ }
272
+ }
0 commit comments