From 043f723907845d3dfe06f647db2cd2d2f37bb88a Mon Sep 17 00:00:00 2001 From: Michal Nazarewicz Date: Fri, 20 Jan 2023 22:17:47 +0100 Subject: [PATCH] Use [T]::copy_within in EncoderWriter i/o [T]::rotate_left MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For now not doing the same in DecoderReader because benchmarks indicate a performance regression and I’d like to take a closer look at it. With EncoderWriter benchmark results are as follows: encode_reuse_buf_stream: 3 21.01 ns +3.3135% Regression p = 0.00 50 58.20 ns +1.8117% Regression p = 0.00 100 71.93 ns -7.4010% Improvement p = 0.00 500 237.47 ns -0.3117% Within noise p = 0.02 3072 1.283 µs -2.5103% Improvement p = 0.00 3145728 1.335 ms +0.3749% Within noise p = 0.03 10485760 4.442 ms +0.8301% Within noise p = 0.00 31457280 14.135 ms -0.0776% No change p = 0.50 --- src/write/encoder.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/write/encoder.rs b/src/write/encoder.rs index 1c19bb4..c1a0f67 100644 --- a/src/write/encoder.rs +++ b/src/write/encoder.rs @@ -185,16 +185,12 @@ impl<'e, E: Engine, W: io::Write> EncoderWriter<'e, E, W> { self.panicked = false; res.map(|consumed| { - debug_assert!(consumed <= current_output_len); - - if consumed < current_output_len { - self.output_occupied_len = current_output_len.checked_sub(consumed).unwrap(); + self.output_occupied_len = current_output_len.checked_sub(consumed).unwrap(); + if self.output_occupied_len > 0 { // If we're blocking on I/O, the minor inefficiency of copying bytes to the // start of the buffer is the least of our concerns... - // TODO Rotate moves more than we need to; copy_within now stable. - self.output.rotate_left(consumed); - } else { - self.output_occupied_len = 0; + self.output + .copy_within(consumed..consumed + self.output_occupied_len, 0) } }) }