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
+5
View File
@@ -464,6 +464,11 @@ impl Bytes {
/// Requires that `begin <= end` and `end <= self.len()`, otherwise slicing
/// will panic.
pub fn slice(&self, begin: usize, end: usize) -> Bytes {
if begin == end {
assert!(begin <= self.len());
return Bytes::new();
}
let mut ret = self.clone();
unsafe {