Faster From<[u8]> for BytesMut, remove panic in fmt::Write (#133)

* use slice.to_vec instead of buf.put in From<[u8]>

* don't panic in fmt::Write for BytesMut
This commit is contained in:
Sean McArthur
2017-06-15 10:55:02 -07:00
committed by Carl Lerche
parent 3240fb9cd9
commit 7e8373da8d
3 changed files with 55 additions and 6 deletions
+24
View File
@@ -127,6 +127,30 @@ fn drain_write_drain(b: &mut Bencher) {
})
}
#[bench]
fn fmt_write(b: &mut Bencher) {
use std::fmt::Write;
let mut buf = BytesMut::with_capacity(128);
let s = "foo bar baz quux lorem ipsum dolor et";
b.bytes = s.len() as u64;
b.iter(|| {
let _ = write!(buf, "{}", s);
test::black_box(&buf);
unsafe { buf.set_len(0); }
})
}
#[bench]
fn from_long_slice(b: &mut Bencher) {
let data = [0u8; 128];
b.bytes = data.len() as u64;
b.iter(|| {
let buf = BytesMut::from(&data[..]);
test::black_box(buf);
})
}
#[bench]
fn slice_empty(b: &mut Bencher) {
b.iter(|| {