@@ -12,106 +12,184 @@ fn main() {
12
12
}
13
13
14
14
#[ cfg( feature = "bench" ) ]
15
- fn main ( ) {
16
- println ! (
17
- "Contended Intern {}" ,
18
- bench( || {
19
- let mut handles: Vec <std:: thread:: JoinHandle <( ) >> = Vec :: new( ) ;
20
- for i in 0 ..50 {
21
- handles. push( std:: thread:: spawn( move || {
22
- let mut interned_strings = Vec :: new( ) ;
23
- let mut interned_ints = Vec :: new( ) ;
24
- let mut interned_pairs = Vec :: new( ) ;
25
- let mut interned_triples = Vec :: new( ) ;
26
- for n in 0 ..100000 {
27
- match ( i + n) % 4 {
28
- 0 => {
29
- interned_strings. push( Intern :: new( format!( "i {} and n {}" , i, n) ) ) ;
30
- }
31
- 1 => {
32
- interned_ints. push( Intern :: new( i * n * 137 ) ) ;
33
- }
34
- 2 => {
35
- interned_pairs. push( Intern :: new( ( i, n) ) ) ;
36
- }
37
- _ => {
38
- interned_triples. push( Intern :: new( ( i, n, i + n) ) ) ;
39
- }
40
- }
41
- }
42
- } ) ) ;
15
+ macro_rules! four_types {
16
+ ( $intern: ident, $i: expr) => {
17
+ $intern:: <String >:: benchmarking_only_clear_interns( ) ;
18
+ $intern:: <usize >:: benchmarking_only_clear_interns( ) ;
19
+ $intern:: <( usize , usize ) >:: benchmarking_only_clear_interns( ) ;
20
+ $intern:: <( usize , usize , usize ) >:: benchmarking_only_clear_interns( ) ;
21
+
22
+ let mut interned_strings = Vec :: new( ) ;
23
+ let mut interned_ints = Vec :: new( ) ;
24
+ let mut interned_pairs = Vec :: new( ) ;
25
+ let mut interned_triples = Vec :: new( ) ;
26
+ for n in 0 ..100000 {
27
+ match ( $i + n) % 4 {
28
+ 0 => {
29
+ interned_strings. push( $intern:: new( format!( "i {} and n {}" , $i, n) ) ) ;
30
+ }
31
+ 1 => {
32
+ interned_ints. push( $intern:: new( $i * n * 137 ) ) ;
33
+ }
34
+ 2 => {
35
+ interned_pairs. push( $intern:: new( ( $i, n) ) ) ;
36
+ }
37
+ _ => {
38
+ interned_triples. push( $intern:: new( ( $i, n, $i + n) ) ) ;
39
+ }
43
40
}
44
- for h in handles {
45
- h. join( ) . unwrap( ) ;
41
+ }
42
+ } ;
43
+ }
44
+
45
+ #[ cfg( feature = "bench" ) ]
46
+ macro_rules! same_arrays {
47
+ ( $intern: ident, $i: expr) => {
48
+ $intern:: <[ usize ; 1000 ] >:: benchmarking_only_clear_interns( ) ;
49
+ $intern:: <[ usize ; 20 ] >:: benchmarking_only_clear_interns( ) ;
50
+ $intern:: <[ usize ; 30 ] >:: benchmarking_only_clear_interns( ) ;
51
+ $intern:: <[ usize ; 40 ] >:: benchmarking_only_clear_interns( ) ;
52
+ $intern:: <[ usize ; 50 ] >:: benchmarking_only_clear_interns( ) ;
53
+ $intern:: <[ usize ; 60 ] >:: benchmarking_only_clear_interns( ) ;
54
+ $intern:: <[ usize ; 70 ] >:: benchmarking_only_clear_interns( ) ;
55
+ $intern:: <[ usize ; 80 ] >:: benchmarking_only_clear_interns( ) ;
56
+ $intern:: <[ usize ; 90 ] >:: benchmarking_only_clear_interns( ) ;
57
+ $intern:: <[ usize ; 100 ] >:: benchmarking_only_clear_interns( ) ;
58
+
59
+ let mut size = $i;
60
+ for _ in 0 ..100000 {
61
+ size = ( size * 75 + 74 ) % ( ( 1 << 16 ) + 1 ) ;
62
+ match size % 10 {
63
+ 0 => {
64
+ $intern:: new( [ 0 ; 1000 ] ) ;
65
+ }
66
+ 1 => {
67
+ $intern:: new( [ 0 ; 20 ] ) ;
68
+ }
69
+ 2 => {
70
+ $intern:: new( [ 0 ; 30 ] ) ;
71
+ }
72
+ 3 => {
73
+ $intern:: new( [ 0 ; 40 ] ) ;
74
+ }
75
+ 4 => {
76
+ $intern:: new( [ 0 ; 50 ] ) ;
77
+ }
78
+ 5 => {
79
+ $intern:: new( [ 0 ; 60 ] ) ;
80
+ }
81
+ 6 => {
82
+ $intern:: new( [ 0 ; 70 ] ) ;
83
+ }
84
+ 7 => {
85
+ $intern:: new( [ 0 ; 80 ] ) ;
86
+ }
87
+ 8 => {
88
+ $intern:: new( [ 0 ; 90 ] ) ;
89
+ }
90
+ _ => {
91
+ $intern:: new( [ 0 ; 100 ] ) ;
92
+ }
46
93
}
47
- } )
48
- ) ;
49
- println ! (
50
- "Contended ArcIntern {}" ,
94
+ }
95
+ } ;
96
+ }
97
+
98
+ #[ cfg( feature = "bench" ) ]
99
+ macro_rules! different_arrays {
100
+ ( $intern: ident, $i: expr) => {
101
+ $intern:: <[ usize ; 1000 ] >:: benchmarking_only_clear_interns( ) ;
102
+ $intern:: <[ usize ; 20 ] >:: benchmarking_only_clear_interns( ) ;
103
+ $intern:: <[ usize ; 30 ] >:: benchmarking_only_clear_interns( ) ;
104
+ $intern:: <[ usize ; 40 ] >:: benchmarking_only_clear_interns( ) ;
105
+ $intern:: <[ usize ; 50 ] >:: benchmarking_only_clear_interns( ) ;
106
+ $intern:: <[ usize ; 60 ] >:: benchmarking_only_clear_interns( ) ;
107
+ $intern:: <[ usize ; 70 ] >:: benchmarking_only_clear_interns( ) ;
108
+ $intern:: <[ usize ; 80 ] >:: benchmarking_only_clear_interns( ) ;
109
+ $intern:: <[ usize ; 90 ] >:: benchmarking_only_clear_interns( ) ;
110
+ $intern:: <[ usize ; 100 ] >:: benchmarking_only_clear_interns( ) ;
111
+
112
+ let mut size = $i;
113
+ for _ in 0 ..100000 {
114
+ size = ( size * 75 + 74 ) % ( ( 1 << 16 ) + 1 ) ;
115
+ match size % 10 {
116
+ 0 => {
117
+ $intern:: new( [ size+$i; 1000 ] ) ;
118
+ }
119
+ 1 => {
120
+ $intern:: new( [ size+$i; 20 ] ) ;
121
+ }
122
+ 2 => {
123
+ $intern:: new( [ size+$i; 30 ] ) ;
124
+ }
125
+ 3 => {
126
+ $intern:: new( [ size+$i; 40 ] ) ;
127
+ }
128
+ 4 => {
129
+ $intern:: new( [ size+$i; 50 ] ) ;
130
+ }
131
+ 5 => {
132
+ $intern:: new( [ size+$i; 60 ] ) ;
133
+ }
134
+ 6 => {
135
+ $intern:: new( [ size+$i; 70 ] ) ;
136
+ }
137
+ 7 => {
138
+ $intern:: new( [ size+$i; 80 ] ) ;
139
+ }
140
+ 8 => {
141
+ $intern:: new( [ size+$i; 90 ] ) ;
142
+ }
143
+ _ => {
144
+ $intern:: new( [ size+$i; 100 ] ) ;
145
+ }
146
+ }
147
+ }
148
+ } ;
149
+ }
150
+
151
+ #[ cfg( feature = "bench" ) ]
152
+ macro_rules! contended {
153
+ ( $intern: ident, $mac: ident) => {
51
154
bench( || {
52
155
let mut handles: Vec <std:: thread:: JoinHandle <( ) >> = Vec :: new( ) ;
53
156
for i in 0 ..50 {
54
- handles. push( std:: thread:: spawn( move || {
55
- let mut interned_strings = Vec :: new( ) ;
56
- let mut interned_ints = Vec :: new( ) ;
57
- let mut interned_pairs = Vec :: new( ) ;
58
- let mut interned_triples = Vec :: new( ) ;
59
- for n in 0 ..100000 {
60
- match ( i + n) % 4 {
61
- 0 => {
62
- interned_strings. push( ArcIntern :: new( format!( "i {} and n {}" , i, n) ) ) ;
63
- }
64
- 1 => {
65
- interned_ints. push( ArcIntern :: new( i * n * 137 ) ) ;
66
- }
67
- 2 => {
68
- interned_pairs. push( ArcIntern :: new( ( i, n) ) ) ;
69
- }
70
- _ => {
71
- interned_triples. push( ArcIntern :: new( ( i, n, i + n) ) ) ;
72
- }
73
- }
74
- }
75
- } ) ) ;
157
+ handles. push( std:: thread:: spawn( move || { $mac!( $intern, i) ; } ) ) ;
76
158
}
77
159
for h in handles {
78
160
h. join( ) . unwrap( ) ;
79
161
}
80
162
} )
81
- ) ;
82
- println ! (
83
- "Contended LocalIntern {} (not really contended)" ,
163
+ } ;
164
+ }
165
+
166
+ #[ cfg( feature = "bench" ) ]
167
+ macro_rules! uncontended {
168
+ ( $intern: ident, $mac: ident) => {
84
169
bench( || {
85
- let mut handles: Vec <std:: thread:: JoinHandle <( ) >> = Vec :: new( ) ;
86
170
for i in 0 ..50 {
87
- handles. push( std:: thread:: spawn( move || {
88
- let mut interned_strings = Vec :: new( ) ;
89
- let mut interned_ints = Vec :: new( ) ;
90
- let mut interned_pairs = Vec :: new( ) ;
91
- let mut interned_triples = Vec :: new( ) ;
92
- for n in 0 ..100000 {
93
- match ( i + n) % 4 {
94
- 0 => {
95
- interned_strings. push( LocalIntern :: new( format!( "i {} and n {}" , i, n) ) ) ;
96
- }
97
- 1 => {
98
- interned_ints. push( LocalIntern :: new( i * n * 137 ) ) ;
99
- }
100
- 2 => {
101
- interned_pairs. push( LocalIntern :: new( ( i, n) ) ) ;
102
- }
103
- _ => {
104
- interned_triples. push( LocalIntern :: new( ( i, n, i + n) ) ) ;
105
- }
106
- }
107
- }
108
- } ) ) ;
109
- }
110
- for h in handles {
111
- h. join( ) . unwrap( ) ;
171
+ $mac!( $intern, i) ;
112
172
}
113
173
} )
114
- ) ;
174
+ } ;
175
+ }
176
+
177
+
178
+ #[ cfg( feature = "bench" ) ]
179
+ fn main ( ) {
180
+ println ! ( "Contended Intern {} / {}" , contended!( Intern , four_types) , uncontended!( Intern , four_types) ) ;
181
+ println ! ( "Contended ArcIntern {} / {}" , contended!( ArcIntern , four_types) , uncontended!( ArcIntern , four_types) ) ;
182
+ println ! ( "Contended LocalIntern {} / {}" , contended!( LocalIntern , four_types) , uncontended!( LocalIntern , four_types) ) ;
183
+ println ! ( "\n " ) ;
184
+
185
+ println ! ( "Contended arrays Intern {} / {}" , contended!( Intern , same_arrays) , uncontended!( Intern , same_arrays) ) ;
186
+ println ! ( "Contended arrays ArcIntern {} / {}" , contended!( ArcIntern , same_arrays) , uncontended!( ArcIntern , same_arrays) ) ;
187
+ println ! ( "Contended arrays LocalIntern {} / {}" , contended!( LocalIntern , same_arrays) , uncontended!( LocalIntern , same_arrays) ) ;
188
+ println ! ( "\n " ) ;
189
+
190
+ println ! ( "Contended different arrays Intern {} / {}" , contended!( Intern , different_arrays) , uncontended!( Intern , different_arrays) ) ;
191
+ println ! ( "Contended different arrays ArcIntern {} / {}" , contended!( ArcIntern , different_arrays) , uncontended!( ArcIntern , different_arrays) ) ;
192
+ println ! ( "Contended different arrays LocalIntern {} / {}" , contended!( LocalIntern , different_arrays) , uncontended!( LocalIntern , different_arrays) ) ;
115
193
println ! ( "\n " ) ;
116
194
117
195
Intern :: new ( 0i64 ) ;
0 commit comments