internal/proto: avoid moving data when buffer is big enough

This commit is contained in:
Vladimir Mihailenco 2018-08-07 14:49:35 +03:00
parent 316917d99f
commit e753829945
1 changed files with 7 additions and 1 deletions

View File

@ -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. // 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 { func (b *ElasticBufReader) Bytes() []byte {
return b.buf[b.r:b.w] 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) { func (b *ElasticBufReader) grow(n int) {
if b.w-b.r >= n {
return
}
// Slide existing data to beginning. // Slide existing data to beginning.
if b.r > 0 { if b.r > 0 {
copy(b.buf, b.buf[b.r:b.w]) copy(b.buf, b.buf[b.r:b.w])