Skip to content
This repository has been archived by the owner on Jun 5, 2021. It is now read-only.

Commit

Permalink
Optimize buffer grow
Browse files Browse the repository at this point in the history
* Reuse capacity if possible
* If not, use slice trick that is optimized by compiler
  • Loading branch information
ernado committed Jul 16, 2019
1 parent 68274f5 commit 76358cb
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 76358cb

Please sign in to comment.