1 #![feature(test)] 2 #![warn(rust_2018_idioms)] 3 4 extern crate test; 5 6 use bytes::{BufMut, BytesMut}; 7 use test::Bencher; 8 9 #[bench] alloc_small(b: &mut Bencher)10fn 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] alloc_mid(b: &mut Bencher)19fn alloc_mid(b: &mut Bencher) { 20 b.iter(|| { 21 test::black_box(BytesMut::with_capacity(128)); 22 }) 23 } 24 25 #[bench] alloc_big(b: &mut Bencher)26fn alloc_big(b: &mut Bencher) { 27 b.iter(|| { 28 test::black_box(BytesMut::with_capacity(4096)); 29 }) 30 } 31 32 #[bench] deref_unique(b: &mut Bencher)33fn deref_unique(b: &mut Bencher) { 34 let mut buf = BytesMut::with_capacity(4096); 35 buf.put(&[0u8; 1024][..]); 36 37 b.iter(|| { 38 for _ in 0..1024 { 39 test::black_box(&buf[..]); 40 } 41 }) 42 } 43 44 #[bench] deref_unique_unroll(b: &mut Bencher)45fn deref_unique_unroll(b: &mut Bencher) { 46 let mut buf = BytesMut::with_capacity(4096); 47 buf.put(&[0u8; 1024][..]); 48 49 b.iter(|| { 50 for _ in 0..128 { 51 test::black_box(&buf[..]); 52 test::black_box(&buf[..]); 53 test::black_box(&buf[..]); 54 test::black_box(&buf[..]); 55 test::black_box(&buf[..]); 56 test::black_box(&buf[..]); 57 test::black_box(&buf[..]); 58 test::black_box(&buf[..]); 59 } 60 }) 61 } 62 63 #[bench] deref_shared(b: &mut Bencher)64fn deref_shared(b: &mut Bencher) { 65 let mut buf = BytesMut::with_capacity(4096); 66 buf.put(&[0u8; 1024][..]); 67 let _b2 = buf.split_off(1024); 68 69 b.iter(|| { 70 for _ in 0..1024 { 71 test::black_box(&buf[..]); 72 } 73 }) 74 } 75 76 #[bench] deref_two(b: &mut Bencher)77fn deref_two(b: &mut Bencher) { 78 let mut buf1 = BytesMut::with_capacity(8); 79 buf1.put(&[0u8; 8][..]); 80 81 let mut buf2 = BytesMut::with_capacity(4096); 82 buf2.put(&[0u8; 1024][..]); 83 84 b.iter(|| { 85 for _ in 0..512 { 86 test::black_box(&buf1[..]); 87 test::black_box(&buf2[..]); 88 } 89 }) 90 } 91 92 #[bench] clone_frozen(b: &mut Bencher)93fn clone_frozen(b: &mut Bencher) { 94 let bytes = BytesMut::from(&b"hello world 1234567890 and have a good byte 0987654321"[..]) 95 .split() 96 .freeze(); 97 98 b.iter(|| { 99 for _ in 0..1024 { 100 test::black_box(&bytes.clone()); 101 } 102 }) 103 } 104 105 #[bench] alloc_write_split_to_mid(b: &mut Bencher)106fn alloc_write_split_to_mid(b: &mut Bencher) { 107 b.iter(|| { 108 let mut buf = BytesMut::with_capacity(128); 109 buf.put_slice(&[0u8; 64]); 110 test::black_box(buf.split_to(64)); 111 }) 112 } 113 114 #[bench] drain_write_drain(b: &mut Bencher)115fn drain_write_drain(b: &mut Bencher) { 116 let data = [0u8; 128]; 117 118 b.iter(|| { 119 let mut buf = BytesMut::with_capacity(1024); 120 let mut parts = Vec::with_capacity(8); 121 122 for _ in 0..8 { 123 buf.put(&data[..]); 124 parts.push(buf.split_to(128)); 125 } 126 127 test::black_box(parts); 128 }) 129 } 130 131 #[bench] fmt_write(b: &mut Bencher)132fn fmt_write(b: &mut Bencher) { 133 use std::fmt::Write; 134 let mut buf = BytesMut::with_capacity(128); 135 let s = "foo bar baz quux lorem ipsum dolor et"; 136 137 b.bytes = s.len() as u64; 138 b.iter(|| { 139 let _ = write!(buf, "{}", s); 140 test::black_box(&buf); 141 unsafe { 142 buf.set_len(0); 143 } 144 }) 145 } 146 147 #[bench] bytes_mut_extend(b: &mut Bencher)148fn bytes_mut_extend(b: &mut Bencher) { 149 let mut buf = BytesMut::with_capacity(256); 150 let data = [33u8; 32]; 151 152 b.bytes = data.len() as u64 * 4; 153 b.iter(|| { 154 for _ in 0..4 { 155 buf.extend(&data); 156 } 157 test::black_box(&buf); 158 unsafe { 159 buf.set_len(0); 160 } 161 }); 162 } 163 164 // BufMut for BytesMut vs Vec<u8> 165 166 #[bench] put_slice_bytes_mut(b: &mut Bencher)167fn put_slice_bytes_mut(b: &mut Bencher) { 168 let mut buf = BytesMut::with_capacity(256); 169 let data = [33u8; 32]; 170 171 b.bytes = data.len() as u64 * 4; 172 b.iter(|| { 173 for _ in 0..4 { 174 buf.put_slice(&data); 175 } 176 test::black_box(&buf); 177 unsafe { 178 buf.set_len(0); 179 } 180 }); 181 } 182 183 #[bench] put_u8_bytes_mut(b: &mut Bencher)184fn put_u8_bytes_mut(b: &mut Bencher) { 185 let mut buf = BytesMut::with_capacity(256); 186 let cnt = 128; 187 188 b.bytes = cnt as u64; 189 b.iter(|| { 190 for _ in 0..cnt { 191 buf.put_u8(b'x'); 192 } 193 test::black_box(&buf); 194 unsafe { 195 buf.set_len(0); 196 } 197 }); 198 } 199 200 #[bench] put_slice_vec(b: &mut Bencher)201fn put_slice_vec(b: &mut Bencher) { 202 let mut buf = Vec::<u8>::with_capacity(256); 203 let data = [33u8; 32]; 204 205 b.bytes = data.len() as u64 * 4; 206 b.iter(|| { 207 for _ in 0..4 { 208 buf.put_slice(&data); 209 } 210 test::black_box(&buf); 211 unsafe { 212 buf.set_len(0); 213 } 214 }); 215 } 216 217 #[bench] put_u8_vec(b: &mut Bencher)218fn put_u8_vec(b: &mut Bencher) { 219 let mut buf = Vec::<u8>::with_capacity(256); 220 let cnt = 128; 221 222 b.bytes = cnt as u64; 223 b.iter(|| { 224 for _ in 0..cnt { 225 buf.put_u8(b'x'); 226 } 227 test::black_box(&buf); 228 unsafe { 229 buf.set_len(0); 230 } 231 }); 232 } 233 234 #[bench] put_slice_vec_extend(b: &mut Bencher)235fn put_slice_vec_extend(b: &mut Bencher) { 236 let mut buf = Vec::<u8>::with_capacity(256); 237 let data = [33u8; 32]; 238 239 b.bytes = data.len() as u64 * 4; 240 b.iter(|| { 241 for _ in 0..4 { 242 buf.extend_from_slice(&data); 243 } 244 test::black_box(&buf); 245 unsafe { 246 buf.set_len(0); 247 } 248 }); 249 } 250 251 #[bench] put_u8_vec_push(b: &mut Bencher)252fn put_u8_vec_push(b: &mut Bencher) { 253 let mut buf = Vec::<u8>::with_capacity(256); 254 let cnt = 128; 255 256 b.bytes = cnt as u64; 257 b.iter(|| { 258 for _ in 0..cnt { 259 buf.push(b'x'); 260 } 261 test::black_box(&buf); 262 unsafe { 263 buf.set_len(0); 264 } 265 }); 266 } 267