Skip to content

Commit

Permalink
fix(codec): Allocate inbound buffer once (#578)
Browse files Browse the repository at this point in the history
Gets rid of reallocs when receiving large messages, increasing throughput by about 30%-40%.
  • Loading branch information
athre0z authored Apr 7, 2021
1 parent 0d05aa0 commit 1d2754f
Showing 1 changed file with 1 addition and 10 deletions.
11 changes: 1 addition & 10 deletions tonic/src/codec/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 1d2754f

Please sign in to comment.