7 use bytes
::{Bytes, BytesMut, BufMut}
;
10 fn alloc_small(b
: &mut Bencher
) {
13 test
::black_box(BytesMut
::with_capacity(12));
19 fn alloc_mid(b
: &mut Bencher
) {
21 test
::black_box(BytesMut
::with_capacity(128));
26 fn alloc_big(b
: &mut Bencher
) {
28 test
::black_box(BytesMut
::with_capacity(4096));
33 fn split_off_and_drop(b
: &mut Bencher
) {
36 let v
= vec
![10; 200];
37 let mut b
= Bytes
::from(v
);
38 test
::black_box(b
.split_off(100));
45 fn deref_unique(b
: &mut Bencher
) {
46 let mut buf
= BytesMut
::with_capacity(4096);
47 buf
.put(&[0u8; 1024][..]);
51 test
::black_box(&buf
[..]);
57 fn deref_unique_unroll(b
: &mut Bencher
) {
58 let mut buf
= BytesMut
::with_capacity(4096);
59 buf
.put(&[0u8; 1024][..]);
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
[..]);
76 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);
83 test
::black_box(&buf
[..]);
89 fn deref_inline(b
: &mut Bencher
) {
90 let mut buf
= BytesMut
::with_capacity(8);
91 buf
.put(&[0u8; 8][..]);
95 test
::black_box(&buf
[..]);
101 fn deref_two(b
: &mut Bencher
) {
102 let mut buf1
= BytesMut
::with_capacity(8);
103 buf1
.put(&[0u8; 8][..]);
105 let mut buf2
= BytesMut
::with_capacity(4096);
106 buf2
.put(&[0u8; 1024][..]);
110 test
::black_box(&buf1
[..]);
111 test
::black_box(&buf2
[..]);
117 fn clone_inline(b
: &mut Bencher
) {
118 let bytes
= Bytes
::from_static(b
"hello world");
122 test
::black_box(&bytes
.clone());
128 fn clone_static(b
: &mut Bencher
) {
129 let bytes
= Bytes
::from_static("hello world 1234567890 and have a good byte 0987654321".as_bytes());
133 test
::black_box(&bytes
.clone());
139 fn clone_arc(b
: &mut Bencher
) {
140 let bytes
= Bytes
::from("hello world 1234567890 and have a good byte 0987654321".as_bytes());
144 test
::black_box(&bytes
.clone());
150 fn alloc_write_split_to_mid(b
: &mut Bencher
) {
152 let mut buf
= BytesMut
::with_capacity(128);
153 buf
.put_slice(&[0u8; 64]);
154 test
::black_box(buf
.split_to(64));
159 fn drain_write_drain(b
: &mut Bencher
) {
160 let data
= [0u8; 128];
163 let mut buf
= BytesMut
::with_capacity(1024);
164 let mut parts
= Vec
::with_capacity(8);
168 parts
.push(buf
.split_to(128));
171 test
::black_box(parts
);
176 fn fmt_write(b
: &mut Bencher
) {
178 let mut buf
= BytesMut
::with_capacity(128);
179 let s
= "foo bar baz quux lorem ipsum dolor et";
181 b
.bytes
= s
.len() as u64;
183 let _
= write
!(buf
, "{}", s
);
184 test
::black_box(&buf
);
185 unsafe { buf.set_len(0); }
190 fn from_long_slice(b
: &mut Bencher
) {
191 let data
= [0u8; 128];
192 b
.bytes
= data
.len() as u64;
194 let buf
= BytesMut
::from(&data
[..]);
195 test
::black_box(buf
);
200 fn slice_empty(b
: &mut Bencher
) {
202 let b
= Bytes
::from(vec
![17; 1024]).clone();
204 test
::black_box(b
.slice(i
% 100, i
% 100));
210 fn slice_short_from_arc(b
: &mut Bencher
) {
212 // `clone` is to convert to ARC
213 let b
= Bytes
::from(vec
![17; 1024]).clone();
215 test
::black_box(b
.slice(1, 2 + i
% 10));
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;
227 fn slice_avg_le_inline_from_arc(b
: &mut Bencher
) {
229 // `clone` is to convert to ARC
230 let b
= Bytes
::from(vec
![17; 1024]).clone();
233 let len
= 1 + i
% (INLINE_CAP
- 1);
234 test
::black_box(b
.slice(i
% 10, i
% 10 + len
));
240 fn slice_large_le_inline_from_arc(b
: &mut Bencher
) {
242 // `clone` is to convert to ARC
243 let b
= Bytes
::from(vec
![17; 1024]).clone();
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
));