daemon/logger: Increase initial buffers size

Make the allocated buffers bigger to allow better reusability and avoid
frequent reallocations.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski 2022-05-30 10:46:33 +02:00
parent 98810847c4
commit d8a731c3aa
2 changed files with 7 additions and 2 deletions

View File

@ -23,7 +23,7 @@ const Name = "json-file"
// Every buffer will have to store the same constant json structure with the message
// len(`{"log":"","stream:"stdout","time":"2000-01-01T00:00:00.000000000Z"}\n`) = 68.
// So let's start with a buffer bigger than this.
const initialBufSize = 128
const initialBufSize = 256
var buffersPool = sync.Pool{New: func() interface{} { return bytes.NewBuffer(make([]byte, 0, initialBufSize)) }}

View File

@ -3,6 +3,7 @@ package local // import "github.com/docker/docker/daemon/logger/local"
import (
"encoding/binary"
"io"
"math/bits"
"strconv"
"sync"
"time"
@ -110,7 +111,11 @@ func marshal(m *logger.Message, buffer *[]byte) error {
buf := *buffer
if writeLen > cap(buf) {
buf = make([]byte, writeLen)
// If we already need to reallocate the buffer, make it larger to be more reusable.
// Round to the next power of two.
capacity := 1 << (bits.Len(uint(writeLen)) + 1)
buf = make([]byte, writeLen, capacity)
} else {
buf = buf[:writeLen]
}