Source File
buffer_pool.go
Belonging Package
google.golang.org/grpc/mem
/*** Copyright 2024 gRPC authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.**/package memimport ()// BufferPool is a pool of buffers that can be shared and reused, resulting in// decreased memory allocation.type BufferPool interface {// Get returns a buffer with specified length from the pool.Get(length int) *[]byte// Put returns a buffer to the pool.//// The provided pointer must hold a prefix of the buffer obtained via// BufferPool.Get to ensure the buffer's entire capacity can be re-used.Put(*[]byte)}const goPageSize = 4 << 10 // 4KiB. N.B. this must be a power of 2.var defaultBufferPoolSizes = []int{256,goPageSize,16 << 10, // 16KB (max HTTP/2 frame size used by gRPC)32 << 10, // 32KB (default buffer size for io.Copy)1 << 20, // 1MB}var defaultBufferPool BufferPoolfunc init() {defaultBufferPool = NewTieredBufferPool(defaultBufferPoolSizes...)internal.SetDefaultBufferPool = func( BufferPool) {defaultBufferPool =}internal.SetBufferPoolingThresholdForTesting = func( int) {bufferPoolingThreshold =}}// DefaultBufferPool returns the current default buffer pool. It is a BufferPool// created with NewBufferPool that uses a set of default sizes optimized for// expected workflows.func () BufferPool {return defaultBufferPool}// NewTieredBufferPool returns a BufferPool implementation that uses multiple// underlying pools of the given pool sizes.func ( ...int) BufferPool {sort.Ints():= make([]*sizedBufferPool, len())for , := range {[] = newSizedBufferPool()}return &tieredBufferPool{sizedPools: ,}}// tieredBufferPool implements the BufferPool interface with multiple tiers of// buffer pools for different sizes of buffers.type tieredBufferPool struct {sizedPools []*sizedBufferPoolfallbackPool simpleBufferPool}func ( *tieredBufferPool) ( int) *[]byte {return .getPool().Get()}func ( *tieredBufferPool) ( *[]byte) {.getPool(cap(*)).Put()}func ( *tieredBufferPool) ( int) BufferPool {:= sort.Search(len(.sizedPools), func( int) bool {return .sizedPools[].defaultSize >=})if == len(.sizedPools) {return &.fallbackPool}return .sizedPools[]}// sizedBufferPool is a BufferPool implementation that is optimized for specific// buffer sizes. For example, HTTP/2 frames within gRPC have a default max size// of 16kb and a sizedBufferPool can be configured to only return buffers with a// capacity of 16kb. Note that however it does not support returning larger// buffers and in fact panics if such a buffer is requested. Because of this,// this BufferPool implementation is not meant to be used on its own and rather// is intended to be embedded in a tieredBufferPool such that Get is only// invoked when the required size is smaller than or equal to defaultSize.type sizedBufferPool struct {pool sync.PooldefaultSize int}func ( *sizedBufferPool) ( int) *[]byte {, := .pool.Get().(*[]byte)if ! {:= make([]byte, , .defaultSize)return &}:= *clear([:cap()])* = [:]return}func ( *sizedBufferPool) ( *[]byte) {if cap(*) < .defaultSize {// Ignore buffers that are too small to fit in the pool. Otherwise, when// Get is called it will panic as it tries to index outside the bounds// of the buffer.return}.pool.Put()}func newSizedBufferPool( int) *sizedBufferPool {return &sizedBufferPool{defaultSize: ,}}var _ BufferPool = (*simpleBufferPool)(nil)// simpleBufferPool is an implementation of the BufferPool interface that// attempts to pool buffers with a sync.Pool. When Get is invoked, it tries to// acquire a buffer from the pool but if that buffer is too small, it returns it// to the pool and creates a new one.type simpleBufferPool struct {pool sync.Pool}func ( *simpleBufferPool) ( int) *[]byte {, := .pool.Get().(*[]byte)if && cap(*) >= {clear((*)[:cap(*)])* = (*)[:]return}// A buffer was pulled from the pool, but it is too small. Put it back in// the pool and create one large enough.if {.pool.Put()}// 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.:= ( + goPageSize - 1) & ^(goPageSize - 1):= make([]byte, , )return &}func ( *simpleBufferPool) ( *[]byte) {.pool.Put()}var _ BufferPool = NopBufferPool{}// NopBufferPool is a buffer pool that returns new buffers without pooling.type NopBufferPool struct{}// Get returns a buffer with specified length from the pool.func (NopBufferPool) ( int) *[]byte {:= make([]byte, )return &}// Put returns a buffer to the pool.func (NopBufferPool) (*[]byte) {}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |