Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of BytesMut::reserve #313

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions benches/bytes_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,25 @@ fn fmt_write(b: &mut Bencher) {
})
}

#[bench]
fn bytes_mut_extend(b: &mut Bencher) {
let mut buf = BytesMut::with_capacity(256);
let data = [33u8; 32];

b.bytes = data.len() as u64 * 4;
b.iter(|| {
for _ in 0..4 {
buf.extend(&data);
}
test::black_box(&buf);
unsafe { buf.set_len(0); }
});
}

// BufMut for BytesMut vs Vec<u8>

#[bench]
fn put_bytes_mut(b: &mut Bencher) {
fn put_slice_bytes_mut(b: &mut Bencher) {
let mut buf = BytesMut::with_capacity(256);
let data = [33u8; 32];

Expand Down Expand Up @@ -174,7 +189,7 @@ fn put_u8_bytes_mut(b: &mut Bencher) {
}

#[bench]
fn put_vec(b: &mut Bencher) {
fn put_slice_vec(b: &mut Bencher) {
let mut buf = Vec::<u8>::with_capacity(256);
let data = [33u8; 32];

Expand Down Expand Up @@ -204,7 +219,7 @@ fn put_u8_vec(b: &mut Bencher) {
}

#[bench]
fn put_vec_extend(b: &mut Bencher) {
fn put_slice_vec_extend(b: &mut Bencher) {
let mut buf = Vec::<u8>::with_capacity(256);
let data = [33u8; 32];

Expand Down
9 changes: 9 additions & 0 deletions src/bytes_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ impl BytesMut {
/// # Panics
///
/// Panics if the new capacity overflows `usize`.
#[inline]
pub fn reserve(&mut self, additional: usize) {
let len = self.len();
let rem = self.capacity() - len;
Expand All @@ -523,6 +524,13 @@ impl BytesMut {
return;
}

self.reserve_inner(additional);
}

// In separate function to allow the short-circuits in `reserve` to
// be inline-able. Significant helps performance.
fn reserve_inner(&mut self, additional: usize) {
let len = self.len();
let kind = self.kind();

if kind == KIND_VEC {
Expand Down Expand Up @@ -637,6 +645,7 @@ impl BytesMut {

// Forget the vector handle
mem::forget(v);

}
/// Appends given bytes to this object.
///
Expand Down