@@ -26,7 +26,7 @@ pub fn overhead(c: &mut Criterion) {
26
26
register ( ) ;
27
27
28
28
let mut group = c. benchmark_group ( "task_overhead" ) ;
29
- group. sample_size ( 50 ) ;
29
+ group. sample_size ( 100 ) ;
30
30
// Test durations between 10us and 1ms
31
31
for micros in [ 1 , 10 , 50 , 100 , 500 , 1000 ] {
32
32
let duration = Duration :: from_micros ( micros) ;
@@ -35,28 +35,76 @@ pub fn overhead(c: &mut Criterion) {
35
35
b. iter ( || busy_task ( black_box ( d) ) )
36
36
} ) ;
37
37
38
- group. bench_with_input ( BenchmarkId :: new ( "turbo" , micros) , & duration, |b, & d| {
39
- run_turbo :: < false > ( b, d) ;
40
- } ) ;
38
+ group. bench_with_input (
39
+ BenchmarkId :: new ( "turbo-uncached" , micros) ,
40
+ & duration,
41
+ |b, & d| {
42
+ run_turbo :: < Uncached > ( b, d) ;
43
+ } ,
44
+ ) ;
41
45
42
46
group. bench_with_input (
43
- BenchmarkId :: new ( "turbo-cached" , micros) ,
47
+ BenchmarkId :: new ( "turbo-cached-same " , micros) ,
44
48
& duration,
45
49
|b, & d| {
46
- run_turbo :: < true > ( b, d) ;
50
+ run_turbo :: < CachedSame > ( b, d) ;
51
+ } ,
52
+ ) ;
53
+
54
+ group. bench_with_input (
55
+ BenchmarkId :: new ( "turbo-cached-different" , micros) ,
56
+ & duration,
57
+ |b, & d| {
58
+ run_turbo :: < CachedDifferent > ( b, d) ;
47
59
} ,
48
60
) ;
49
61
}
50
62
group. finish ( ) ;
51
63
}
52
64
53
- fn run_turbo < const CACHED : bool > ( b : & mut criterion:: Bencher < ' _ > , d : Duration ) {
65
+ trait TurboMode {
66
+ fn key ( index : u64 ) -> u64 ;
67
+ fn is_cached ( ) -> bool ;
68
+ }
69
+ struct Uncached ;
70
+ impl TurboMode for Uncached {
71
+ fn key ( index : u64 ) -> u64 {
72
+ index
73
+ }
74
+
75
+ fn is_cached ( ) -> bool {
76
+ false
77
+ }
78
+ }
79
+ struct CachedSame ;
80
+ impl TurboMode for CachedSame {
81
+ fn key ( _index : u64 ) -> u64 {
82
+ 0
83
+ }
84
+
85
+ fn is_cached ( ) -> bool {
86
+ true
87
+ }
88
+ }
89
+ struct CachedDifferent ;
90
+ impl TurboMode for CachedDifferent {
91
+ fn key ( index : u64 ) -> u64 {
92
+ index
93
+ }
94
+
95
+ fn is_cached ( ) -> bool {
96
+ true
97
+ }
98
+ }
99
+ fn run_turbo < Mode : TurboMode > ( b : & mut criterion:: Bencher < ' _ > , d : Duration ) {
54
100
let rt = tokio:: runtime:: Builder :: new_multi_thread ( )
55
101
. enable_all ( )
56
102
. build ( )
57
103
. unwrap ( ) ;
58
104
59
105
b. to_async ( rt) . iter_custom ( |iters| {
106
+ // It is important to create the tt instance here to ensure the cache is not shared across
107
+ // iterations.
60
108
let tt = TurboTasks :: new ( TurboTasksBackend :: new (
61
109
BackendOptions {
62
110
storage_mode : None ,
@@ -67,13 +115,15 @@ fn run_turbo<const CACHED: bool>(b: &mut criterion::Bencher<'_>, d: Duration) {
67
115
68
116
async move {
69
117
tt. run_once ( async move {
70
- // If cached run once outside the loop to ensure the task is cached.
71
- if CACHED {
72
- busy_turbo ( 0 , black_box ( d) ) . await ?;
118
+ // If cached run once outside the loop to ensure the tasks are cached.
119
+ if Mode :: is_cached ( ) {
120
+ for i in 0 ..iters {
121
+ black_box ( busy_turbo ( i, black_box ( d) ) . await ?) ;
122
+ }
73
123
}
74
124
let start = Instant :: now ( ) ;
75
125
for i in 0 ..iters {
76
- black_box ( busy_turbo ( if CACHED { 0 } else { i } , black_box ( d) ) . await ?) ;
126
+ black_box ( busy_turbo ( Mode :: key ( i ) , black_box ( d) ) . await ?) ;
77
127
}
78
128
Ok ( start. elapsed ( ) )
79
129
} )
0 commit comments