4
4
extern crate test;
5
5
6
6
use test:: Bencher ;
7
- use bytes:: { Bytes , BytesMut , BufMut } ;
8
-
9
- #[ bench]
10
- fn alloc_small ( b : & mut Bencher ) {
11
- b. iter ( || {
12
- for _ in 0 ..1024 {
13
- test:: black_box ( BytesMut :: with_capacity ( 12 ) ) ;
14
- }
15
- } )
16
- }
17
-
18
- #[ bench]
19
- fn alloc_mid ( b : & mut Bencher ) {
20
- b. iter ( || {
21
- test:: black_box ( BytesMut :: with_capacity ( 128 ) ) ;
22
- } )
23
- }
24
-
25
- #[ bench]
26
- fn alloc_big ( b : & mut Bencher ) {
27
- b. iter ( || {
28
- test:: black_box ( BytesMut :: with_capacity ( 4096 ) ) ;
29
- } )
30
- }
31
-
32
- #[ bench]
33
- fn split_off_and_drop ( b : & mut Bencher ) {
34
- b. iter ( || {
35
- for _ in 0 ..1024 {
36
- let v = vec ! [ 10 ; 200 ] ;
37
- let mut b = Bytes :: from ( v) ;
38
- test:: black_box ( b. split_off ( 100 ) ) ;
39
- test:: black_box ( b) ;
40
- }
41
- } )
42
- }
7
+ use bytes:: Bytes ;
43
8
44
9
#[ bench]
45
10
fn deref_unique ( b : & mut Bencher ) {
46
- let mut buf = BytesMut :: with_capacity ( 4096 ) ;
47
- buf. put ( & [ 0u8 ; 1024 ] [ ..] ) ;
11
+ let buf = Bytes :: from ( vec ! [ 0 ; 1024 ] ) ;
48
12
49
13
b. iter ( || {
50
14
for _ in 0 ..1024 {
@@ -53,30 +17,10 @@ fn deref_unique(b: &mut Bencher) {
53
17
} )
54
18
}
55
19
56
- #[ bench]
57
- fn deref_unique_unroll ( b : & mut Bencher ) {
58
- let mut buf = BytesMut :: with_capacity ( 4096 ) ;
59
- buf. put ( & [ 0u8 ; 1024 ] [ ..] ) ;
60
-
61
- b. iter ( || {
62
- for _ in 0 ..128 {
63
- test:: black_box ( & buf[ ..] ) ;
64
- test:: black_box ( & buf[ ..] ) ;
65
- test:: black_box ( & buf[ ..] ) ;
66
- test:: black_box ( & buf[ ..] ) ;
67
- test:: black_box ( & buf[ ..] ) ;
68
- test:: black_box ( & buf[ ..] ) ;
69
- test:: black_box ( & buf[ ..] ) ;
70
- test:: black_box ( & buf[ ..] ) ;
71
- }
72
- } )
73
- }
74
-
75
20
#[ bench]
76
21
fn deref_shared ( b : & mut Bencher ) {
77
- let mut buf = BytesMut :: with_capacity ( 4096 ) ;
78
- buf. put ( & [ 0u8 ; 1024 ] [ ..] ) ;
79
- let _b2 = buf. split_off ( 1024 ) ;
22
+ let buf = Bytes :: from ( vec ! [ 0 ; 1024 ] ) ;
23
+ let _b2 = buf. clone ( ) ;
80
24
81
25
b. iter ( || {
82
26
for _ in 0 ..1024 {
@@ -86,9 +30,8 @@ fn deref_shared(b: &mut Bencher) {
86
30
}
87
31
88
32
#[ bench]
89
- fn deref_inline ( b : & mut Bencher ) {
90
- let mut buf = BytesMut :: with_capacity ( 8 ) ;
91
- buf. put ( & [ 0u8 ; 8 ] [ ..] ) ;
33
+ fn deref_static ( b : & mut Bencher ) {
34
+ let buf = Bytes :: from_static ( b"hello world" ) ;
92
35
93
36
b. iter ( || {
94
37
for _ in 0 ..1024 {
@@ -97,33 +40,6 @@ fn deref_inline(b: &mut Bencher) {
97
40
} )
98
41
}
99
42
100
- #[ bench]
101
- fn deref_two ( b : & mut Bencher ) {
102
- let mut buf1 = BytesMut :: with_capacity ( 8 ) ;
103
- buf1. put ( & [ 0u8 ; 8 ] [ ..] ) ;
104
-
105
- let mut buf2 = BytesMut :: with_capacity ( 4096 ) ;
106
- buf2. put ( & [ 0u8 ; 1024 ] [ ..] ) ;
107
-
108
- b. iter ( || {
109
- for _ in 0 ..512 {
110
- test:: black_box ( & buf1[ ..] ) ;
111
- test:: black_box ( & buf2[ ..] ) ;
112
- }
113
- } )
114
- }
115
-
116
- #[ bench]
117
- fn clone_inline ( b : & mut Bencher ) {
118
- let bytes = Bytes :: from_static ( b"hello world" ) ;
119
-
120
- b. iter ( || {
121
- for _ in 0 ..1024 {
122
- test:: black_box ( & bytes. clone ( ) ) ;
123
- }
124
- } )
125
- }
126
-
127
43
#[ bench]
128
44
fn clone_static ( b : & mut Bencher ) {
129
45
let bytes = Bytes :: from_static ( "hello world 1234567890 and have a good byte 0987654321" . as_bytes ( ) ) ;
@@ -136,8 +52,8 @@ fn clone_static(b: &mut Bencher) {
136
52
}
137
53
138
54
#[ bench]
139
- fn clone_arc ( b : & mut Bencher ) {
140
- let bytes = Bytes :: from ( "hello world 1234567890 and have a good byte 0987654321" . as_bytes ( ) ) ;
55
+ fn clone_shared ( b : & mut Bencher ) {
56
+ let bytes = Bytes :: from ( b "hello world 1234567890 and have a good byte 0987654321". to_vec ( ) ) ;
141
57
142
58
b. iter ( || {
143
59
for _ in 0 ..1024 {
@@ -147,42 +63,14 @@ fn clone_arc(b: &mut Bencher) {
147
63
}
148
64
149
65
#[ bench]
150
- fn alloc_write_split_to_mid ( b : & mut Bencher ) {
151
- b. iter ( || {
152
- let mut buf = BytesMut :: with_capacity ( 128 ) ;
153
- buf. put_slice ( & [ 0u8 ; 64 ] ) ;
154
- test:: black_box ( buf. split_to ( 64 ) ) ;
155
- } )
156
- }
157
-
158
- #[ bench]
159
- fn drain_write_drain ( b : & mut Bencher ) {
160
- let data = [ 0u8 ; 128 ] ;
66
+ fn clone_arc_vec ( b : & mut Bencher ) {
67
+ use std:: sync:: Arc ;
68
+ let bytes = Arc :: new ( b"hello world 1234567890 and have a good byte 0987654321" . to_vec ( ) ) ;
161
69
162
70
b. iter ( || {
163
- let mut buf = BytesMut :: with_capacity ( 1024 ) ;
164
- let mut parts = Vec :: with_capacity ( 8 ) ;
165
-
166
- for _ in 0 ..8 {
167
- buf. put ( & data[ ..] ) ;
168
- parts. push ( buf. split_to ( 128 ) ) ;
71
+ for _ in 0 ..1024 {
72
+ test:: black_box ( & bytes. clone ( ) ) ;
169
73
}
170
-
171
- test:: black_box ( parts) ;
172
- } )
173
- }
174
-
175
- #[ bench]
176
- fn fmt_write ( b : & mut Bencher ) {
177
- use std:: fmt:: Write ;
178
- let mut buf = BytesMut :: with_capacity ( 128 ) ;
179
- let s = "foo bar baz quux lorem ipsum dolor et" ;
180
-
181
- b. bytes = s. len ( ) as u64 ;
182
- b. iter ( || {
183
- let _ = write ! ( buf, "{}" , s) ;
184
- test:: black_box ( & buf) ;
185
- unsafe { buf. set_len ( 0 ) ; }
186
74
} )
187
75
}
188
76
@@ -191,7 +79,7 @@ fn from_long_slice(b: &mut Bencher) {
191
79
let data = [ 0u8 ; 128 ] ;
192
80
b. bytes = data. len ( ) as u64 ;
193
81
b. iter ( || {
194
- let buf = BytesMut :: from ( & data[ ..] ) ;
82
+ let buf = Bytes :: copy_from_slice ( & data[ ..] ) ;
195
83
test:: black_box ( buf) ;
196
84
} )
197
85
}
@@ -217,34 +105,14 @@ fn slice_short_from_arc(b: &mut Bencher) {
217
105
} )
218
106
}
219
107
220
- // Keep in sync with bytes.rs
221
- #[ cfg( target_pointer_width = "64" ) ]
222
- const INLINE_CAP : usize = 4 * 8 - 1 ;
223
- #[ cfg( target_pointer_width = "32" ) ]
224
- const INLINE_CAP : usize = 4 * 4 - 1 ;
225
-
226
- #[ bench]
227
- fn slice_avg_le_inline_from_arc ( b : & mut Bencher ) {
228
- b. iter ( || {
229
- // `clone` is to convert to ARC
230
- let b = Bytes :: from ( vec ! [ 17 ; 1024 ] ) . clone ( ) ;
231
- for i in 0 ..1000 {
232
- // [1, INLINE_CAP]
233
- let len = 1 + i % ( INLINE_CAP - 1 ) ;
234
- test:: black_box ( b. slice ( i % 10 ..i % 10 + len) ) ;
235
- }
236
- } )
237
- }
238
-
239
108
#[ bench]
240
- fn slice_large_le_inline_from_arc ( b : & mut Bencher ) {
109
+ fn split_off_and_drop ( b : & mut Bencher ) {
241
110
b. iter ( || {
242
- // `clone` is to convert to ARC
243
- let b = Bytes :: from ( vec ! [ 17 ; 1024 ] ) . clone ( ) ;
244
- for i in 0 ..1000 {
245
- // [INLINE_CAP - 10, INLINE_CAP]
246
- let len = INLINE_CAP - 9 + i % 10 ;
247
- test:: black_box ( b. slice ( i % 10 ..i % 10 + len) ) ;
111
+ for _ in 0 ..1024 {
112
+ let v = vec ! [ 10 ; 200 ] ;
113
+ let mut b = Bytes :: from ( v) ;
114
+ test:: black_box ( b. split_off ( 100 ) ) ;
115
+ test:: black_box ( b) ;
248
116
}
249
117
} )
250
118
}
0 commit comments