From 8e49bea00fea274cd48a17cb848000186e5ee7e9 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Mon, 27 Mar 2017 20:33:09 +0300 Subject: [PATCH] Fix InputBuffer::reserve() to not be a no-op MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current implementation uses the `remaining_mut()` function from the bytes::BufMut implementation for Vec. In terms of the BufMut trait, a Vec buffer has infinite capacity - you can always write more to the buffer, since the Vec grows as needed. Hence, the `remaining_mut` here actually returns +∞ (actually, `usize::MAX - len`). So the first `if` is always true, and the calls to `reserve` never actually allocate the appropriate space. What happens instead is that the `bytes_mut()` call in `read_from` picks up the slack, but it merely grows the buffer a 64 bytes at a time, which slows things down. This changes the check to use the Vec capacity instead of `remaining_mut`. In my profile (sending 100,000 10KiB messages back and forth), this reduces `__memmove_avx_unaligned_erms`'s share of the total runtime from ~77% to ~53%. --- src/input_buffer.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/input_buffer.rs b/src/input_buffer.rs index e08e704..c2a87c0 100644 --- a/src/input_buffer.rs +++ b/src/input_buffer.rs @@ -23,7 +23,8 @@ impl InputBuffer { /// Reserve the given amount of space. pub fn reserve(&mut self, space: usize, limit: usize) -> Result<(), SizeLimit>{ - if self.inp_mut().remaining_mut() >= space { + let remaining = self.inp_mut().capacity() - self.inp_mut().len(); + if remaining >= space { // We have enough space right now. Ok(()) } else {