fix(codec): Allocate inbound buffer once (#578)

Gets rid of reallocs when receiving large messages, increasing throughput by about 30%-40%.
This commit is contained in:
Joel Höner
2021-04-08 01:34:12 +02:00
committed by GitHub
parent 0d05aa0d02
commit 1d2754feba
+1 -10
View File
@@ -180,6 +180,7 @@ impl<T> Streaming<T> {
}
};
let len = self.buf.get_u32() as usize;
self.buf.reserve(len);
self.state = State::ReadBody {
compression: is_compressed,
@@ -235,16 +236,6 @@ impl<T> Stream for Streaming<T> {
};
if let Some(data) = chunk {
if data.remaining() > self.buf.remaining_mut() {
let amt = if data.remaining() > BUFFER_SIZE {
data.remaining()
} else {
BUFFER_SIZE
};
self.buf.reserve(amt);
}
self.buf.put(data);
} else {
// FIXME: improve buf usage.