Optimize Bytes::slice(n, n) (#123)

Return empty `Bytes` object

Bench for `slice_empty` difference is

```
55 ns/iter (+/- 1) # before this patch
17 ns/iter (+/- 5) # with this patch
```

Bench for `slice_not_empty` is

```
25,058 ns/iter (+/- 1,099) # before this patch
25,072 ns/iter (+/- 1,593) # with this patch
```
This commit is contained in:
Stepan Koltsov
2017-05-22 13:15:08 -07:00
committed by Carl Lerche
parent 70ee87ea29
commit 3f5890be70
3 changed files with 35 additions and 1 deletions
+21 -1
View File
@@ -4,7 +4,7 @@ extern crate bytes;
extern crate test;
use test::Bencher;
use bytes::{BytesMut, BufMut};
use bytes::{Bytes, BytesMut, BufMut};
#[bench]
fn alloc_small(b: &mut Bencher) {
@@ -126,3 +126,23 @@ fn drain_write_drain(b: &mut Bencher) {
test::black_box(parts);
})
}
#[bench]
fn slice_empty(b: &mut Bencher) {
b.iter(|| {
// Use empty vec to avoid measure of allocation/deallocation
let bytes = Bytes::from(Vec::new());
(bytes.slice(0, 0), bytes)
})
}
#[bench]
fn slice_not_empty(b: &mut Bencher) {
b.iter(|| {
let b = Bytes::from(b"aabbccddeeffgghh".to_vec());
for _ in 0..1024 {
test::black_box(b.slice(3, 5));
test::black_box(&b);
}
})
}