1
1
#![ feature( test) ]
2
+
2
3
extern crate test;
3
4
4
5
use speculate:: * ;
5
- use std:: sync:: mpsc;
6
- use std:: sync:: Arc ;
6
+ use std:: sync:: { mpsc, Arc } ;
7
7
use test:: { black_box, Bencher } ;
8
8
9
9
#[ bench]
@@ -17,26 +17,113 @@ fn bench_direct_2048(b: &mut Bencher) {
17
17
}
18
18
19
19
#[ bench]
20
- async fn test_spec_correct_2048 ( ) {
21
- let ( sender , receiver ) = mpsc :: channel :: < Option < ( usize , isize ) > > ( 10 ) ;
22
- let ( res_sender , res_receiver ) = mpsc :: channel :: < Vec < isize > > ( 1 ) ;
20
+ fn test_spec_correct_2048 ( b : & mut Bencher ) {
21
+ let v = ( 0 .. 2048 ) . collect :: < Vec < _ > > ( ) ;
22
+ let v_arc = Arc :: new ( v ) ;
23
23
24
- // Assuming the receiver is processed elsewhere and not directly within the closures
25
- let processed_data = process_receiver_data ( & receiver) ; // Pseudocode
24
+ b. iter ( || {
25
+ let ( tx, rx) = mpsc:: channel ( ) ;
26
+ let ( tx2, rx2) = mpsc:: channel ( ) ;
27
+ tx. send ( v_arc. clone ( ) ) . unwrap ( ) ;
28
+ tx2. send ( v_arc. clone ( ) ) . unwrap ( ) ;
26
29
27
- let loop_body = Arc :: new ( move |idx : usize , val : isize | -> isize {
28
- // Use processed data instead of directly accessing the receiver
29
- val + 1 // Example operation
30
+ spec (
31
+ move || {
32
+ let local_arc = rx. recv ( ) . unwrap ( ) ;
33
+ local_arc. iter ( ) . fold ( 0 , |old, & new| old + new)
34
+ } ,
35
+ || 2096128 ,
36
+ move |x| {
37
+ let local_arc = rx2. recv ( ) . unwrap ( ) ;
38
+ local_arc. iter ( ) . fold ( x, |old, & new| old + new)
39
+ } ,
40
+ ) ;
30
41
} ) ;
42
+ }
43
+
44
+ #[ bench]
45
+ fn bench_spec_wrong_2048 ( b : & mut Bencher ) {
46
+ let v = ( 0 ..2048 ) . collect :: < Vec < _ > > ( ) ;
47
+ let v_arc = Arc :: new ( v) ;
48
+
49
+ b. iter ( || {
50
+ let ( tx, rx) = mpsc:: channel ( ) ;
51
+ let ( tx2, rx2) = mpsc:: channel ( ) ;
52
+ tx. send ( v_arc. clone ( ) ) . unwrap ( ) ;
53
+ tx2. send ( v_arc. clone ( ) ) . unwrap ( ) ;
54
+ tx2. send ( v_arc. clone ( ) ) . unwrap ( ) ;
31
55
32
- let predictor = Arc :: new ( move |idx : usize | -> isize {
33
- idx as isize * 2 // Directly return a prediction
56
+ spec (
57
+ move || {
58
+ let local_arc = rx. recv ( ) . unwrap ( ) ;
59
+ local_arc. iter ( ) . fold ( 0 , |old, & new| old + new)
60
+ } ,
61
+ || 0 , // Incorrect result of `fold`
62
+ move |x| {
63
+ let local_arc = rx2. recv ( ) . unwrap ( ) ;
64
+ local_arc. iter ( ) . fold ( x, |old, & new| old + new)
65
+ } ,
66
+ ) ;
34
67
} ) ;
68
+ }
35
69
36
- // Assuming spec is properly defined to accept the closures
37
- spec ( 5 , loop_body, predictor) . await ;
70
+ #[ bench]
71
+ fn bench_direct_65536 ( b : & mut Bencher ) {
72
+ let v: Vec < usize > = ( 0 ..65536 ) . collect ( ) ;
73
+ b. iter ( || {
74
+ // Use black_box to prevent compiler optimizations regarding these computations
75
+ let val = v. iter ( ) . fold ( 0 , |old, new| old + * new) ;
76
+ black_box ( v. iter ( ) . fold ( val, |old, new| old + * new) ) ;
77
+ } ) ;
78
+ }
79
+
80
+ #[ bench]
81
+ fn bench_spec_correct_65536 ( b : & mut Bencher ) {
82
+ let v = ( 0 ..65536 ) . collect :: < Vec < _ > > ( ) ;
83
+ let v_arc = Arc :: new ( v) ;
38
84
39
- // Example operations
40
- sender. send ( None ) . await . unwrap ( ) ;
41
- let results = res_receiver. recv ( ) . await . unwrap ( ) ;
85
+ b. iter ( || {
86
+ let ( tx, rx) = mpsc:: channel ( ) ;
87
+ let ( tx2, rx2) = mpsc:: channel ( ) ;
88
+ tx. send ( v_arc. clone ( ) ) . unwrap ( ) ;
89
+ tx2. send ( v_arc. clone ( ) ) . unwrap ( ) ;
90
+
91
+ spec (
92
+ move || {
93
+ let local_arc = rx. recv ( ) . unwrap ( ) ;
94
+ local_arc. iter ( ) . fold ( 0 , |old, & new| old + new)
95
+ } ,
96
+ || 2147450880 ,
97
+ move |x| {
98
+ let local_arc = rx2. recv ( ) . unwrap ( ) ;
99
+ local_arc. iter ( ) . fold ( x, |old, & new| old + new)
100
+ } ,
101
+ ) ;
102
+ } ) ;
103
+ }
104
+
105
+ #[ bench]
106
+ fn bench_spec_wrong_65536 ( b : & mut Bencher ) {
107
+ let v = ( 0 ..65536 ) . collect :: < Vec < _ > > ( ) ;
108
+ let v_arc = Arc :: new ( v) ;
109
+
110
+ b. iter ( || {
111
+ let ( tx, rx) = mpsc:: channel ( ) ;
112
+ let ( tx2, rx2) = mpsc:: channel ( ) ;
113
+ tx. send ( v_arc. clone ( ) ) . unwrap ( ) ;
114
+ tx2. send ( v_arc. clone ( ) ) . unwrap ( ) ;
115
+ tx2. send ( v_arc. clone ( ) ) . unwrap ( ) ;
116
+
117
+ spec (
118
+ move || {
119
+ let local_arc = rx. recv ( ) . unwrap ( ) ;
120
+ local_arc. iter ( ) . fold ( 0 , |old, & new| old + new)
121
+ } ,
122
+ || 0 , // Incorrect result of `fold`
123
+ move |x| {
124
+ let local_arc = rx2. recv ( ) . unwrap ( ) ;
125
+ local_arc. iter ( ) . fold ( x, |old, & new| old + new)
126
+ } ,
127
+ ) ;
128
+ } ) ;
42
129
}
0 commit comments