From e753829945660842b1ae53519a5749d8146e7eed Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Tue, 7 Aug 2018 14:49:35 +0300 Subject: [PATCH] internal/proto: avoid moving data when buffer is big enough --- internal/proto/elastic_reader.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/proto/elastic_reader.go b/internal/proto/elastic_reader.go index 9e4eea2..20eca64 100644 --- a/internal/proto/elastic_reader.go +++ b/internal/proto/elastic_reader.go @@ -48,7 +48,9 @@ func (b *ElasticBufReader) reset(buf []byte, rd io.Reader) { } // Buffered returns the number of bytes that can be read from the current buffer. -func (b *ElasticBufReader) Buffered() int { return b.w - b.r } +func (b *ElasticBufReader) Buffered() int { + return b.w - b.r +} func (b *ElasticBufReader) Bytes() []byte { return b.buf[b.r:b.w] @@ -211,6 +213,10 @@ func (b *ElasticBufReader) ReadN(n int) ([]byte, error) { } func (b *ElasticBufReader) grow(n int) { + if b.w-b.r >= n { + return + } + // Slide existing data to beginning. if b.r > 0 { copy(b.buf, b.buf[b.r:b.w])