Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions mem/buffer_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ type BufferPool interface {
Put(*[]byte)
}

const goPageSize = 4 << 10 // 4KiB. N.B. this must be a power of 2.

var defaultBufferPoolSizes = []int{
256,
4 << 10, // 4KB (go page size)
goPageSize,
16 << 10, // 16KB (max HTTP/2 frame size used by gRPC)
32 << 10, // 32KB (default buffer size for io.Copy)
1 << 20, // 1MB
Expand Down Expand Up @@ -172,7 +174,14 @@ func (p *simpleBufferPool) Get(size int) *[]byte {
p.pool.Put(bs)
}

b := make([]byte, size)
// If we're going to allocate, round up to the nearest page. This way if
// requests frequently arrive with small variation we don't allocate
// repeatedly if we get unlucky and they increase over time. By default we
// only allocate here if size > 1MiB. Because goPageSize is a power of 2, we
// can round up efficiently.
allocSize := (size + goPageSize - 1) & ^(goPageSize - 1)

b := make([]byte, size, allocSize)
return &b
}

Expand Down