Skip to content

Commit 4783f59

Browse files
committed
bytes: use padding as bootstarp
Signed-off-by: Maxim Eryomenko <[email protected]>
1 parent 955d89f commit 4783f59

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/bytes/buffer.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ type Buffer struct {
2323
off int // read at &buf[off], write at &buf[len(buf)]
2424
lastRead readOp // last read operation, so that Unread* can work correctly.
2525

26-
// This is padding for Buffer to cache lines to avoid false sharing.
27-
_ [cpu.BufferPaddingSize]byte
26+
// memory to hold first slice; helps small buffers (Printf) avoid allocation.
27+
// Also it's padding for Buffer to cache lines to avoid false sharing.
28+
bootstrap [cpu.BufferPaddingSize]byte
2829
}
2930

3031
// The readOp constants describe the last action performed on
@@ -128,9 +129,15 @@ func (b *Buffer) grow(n int) int {
128129
if i, ok := b.tryGrowByReslice(n); ok {
129130
return i
130131
}
131-
if b.buf == nil && n <= smallBufferSize {
132-
b.buf = make([]byte, n, smallBufferSize)
133-
return 0
132+
if b.buf == nil {
133+
switch {
134+
case n <= cpu.BufferPaddingSize:
135+
buf = b.bootstrap[0:]
136+
return 0
137+
case n <= smallBufferSize:
138+
b.buf = make([]byte, n, smallBufferSize)
139+
return 0
140+
}
134141
}
135142
c := cap(b.buf)
136143
if n <= c/2-m {

0 commit comments

Comments
 (0)