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

Commit 930f124

Browse files
committed
Optimize internal buffer
Simplify Message.grow method and fix it's usage.
1 parent b0a423d commit 930f124

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

message.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,11 @@ func (m *Message) Reset() {
104104
m.Attributes = m.Attributes[:0]
105105
}
106106

107-
// grow ensures that internal buffer will fit v more bytes and
108-
// increases it capacity if necessary.
109-
func (m *Message) grow(v int) {
110-
// Not performing any optimizations here
111-
// (e.g. preallocate len(buf) * 2 to reduce allocations)
112-
// because they are already done by []byte implementation.
113-
n := len(m.Raw) + v
114-
for cap(m.Raw) < n {
107+
// grow ensures that internal buffer has n length.
108+
func (m *Message) grow(n int) {
109+
for len(m.Raw) < n {
115110
m.Raw = append(m.Raw, 0)
116111
}
117-
m.Raw = m.Raw[:n]
118112
}
119113

120114
// Add appends new attribute to message. Not goroutine-safe.

message_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,18 +472,18 @@ func TestMessage_Equal(t *testing.T) {
472472
func TestMessageGrow(t *testing.T) {
473473
m := New()
474474
m.grow(512)
475-
if len(m.Raw) < 532 {
475+
if len(m.Raw) < 512 {
476476
t.Error("Bad length", len(m.Raw))
477477
}
478478
}
479479

480480
func TestMessageGrowSmaller(t *testing.T) {
481481
m := New()
482482
m.grow(2)
483-
if cap(m.Raw) < 22 {
483+
if cap(m.Raw) < 20 {
484484
t.Error("Bad capacity", cap(m.Raw))
485485
}
486-
if len(m.Raw) < 22 {
486+
if len(m.Raw) < 20 {
487487
t.Error("Bad length", len(m.Raw))
488488
}
489489
}

0 commit comments

Comments
 (0)