From 1d2754feba6b49bfc813f41e8e8e42ffaf8ab0dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20H=C3=B6ner?= Date: Thu, 8 Apr 2021 01:34:12 +0200 Subject: [PATCH] fix(codec): Allocate inbound buffer once (#578) Gets rid of reallocs when receiving large messages, increasing throughput by about 30%-40%. --- tonic/src/codec/decode.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/tonic/src/codec/decode.rs b/tonic/src/codec/decode.rs index 157a270..dbebfe2 100644 --- a/tonic/src/codec/decode.rs +++ b/tonic/src/codec/decode.rs @@ -180,6 +180,7 @@ impl Streaming { } }; let len = self.buf.get_u32() as usize; + self.buf.reserve(len); self.state = State::ReadBody { compression: is_compressed, @@ -235,16 +236,6 @@ impl Stream for Streaming { }; 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.