Skip to content

Commit 63a83ad

Browse files
authored
Optimize condition for removing written data in BufWriter (#415)
* Optimize condition for removing written data in BufWriter Do it strategically, when: - it can use a memcpy instead of memmove to remove it - when no space left for new data Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix typo Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Replace multiplication with division To prevent overflo Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Update crates/async-compression/src/generic/write/buf_writer.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Do copyback at 512B flushed data So that memmove AVX256 can be used to do the copyback, which should be reasonably fast Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix commit issues Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix fmt Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> --------- Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>
1 parent e894423 commit 63a83ad

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

crates/async-compression/src/generic/write/buf_writer.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,13 @@ impl BufWriter {
136136
) -> Poll<io::Result<&mut [u8]>> {
137137
ready!(self.partial_flush_buf(poll_write))?;
138138

139-
if self.written > 0 {
139+
// when the flushed data is larger than or equal to half of yet-to-be-flushed data,
140+
// the copyback could use version of memcpy that do copies from the head of the buffer.
141+
// Anything smaller than that, an overlap would happen that forces use of memmove.
142+
if self.written >= (self.buffered / 3)
143+
|| self.written >= 512
144+
|| self.buffered == self.buf.len()
145+
{
140146
self.remove_written();
141147
}
142148

0 commit comments

Comments
 (0)