From 76358cb28cf64a16e85d1a62b4832ffceb192f5b Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Tue, 4 Jun 2019 17:25:42 +0300 Subject: [PATCH] Optimize buffer grow * Reuse capacity if possible * If not, use slice trick that is optimized by compiler --- message.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/message.go b/message.go index 96054b2..d9c1ef2 100644 --- a/message.go +++ b/message.go @@ -106,9 +106,11 @@ func (m *Message) Reset() { // grow ensures that internal buffer has n length. func (m *Message) grow(n int) { - for len(m.Raw) < n { - m.Raw = append(m.Raw, 0) + if cap(m.Raw) >= n { + m.Raw = m.Raw[:n] + return } + m.Raw = append(m.Raw, make([]byte, n-len(m.Raw))...) } // Add appends new attribute to message. Not goroutine-safe.