/* * * 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.typeBufferPoolinterface {// Get returns a buffer with specified length from the pool.Get(length int) *[]byte// Put returns a buffer to the pool.Put(*[]byte)}var defaultBufferPoolSizes = []int{256,4 << 10, // 4KB (go page size)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.SetDefaultBufferPoolForTesting = 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 {returndefaultBufferPool}// 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 []*sizedBufferPool fallbackPool 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.Pool defaultSize int}func ( *sizedBufferPool) ( int) *[]byte { := .pool.Get().(*[]byte) := *clear([:cap()]) * = [:]return}func ( *sizedBufferPool) ( *[]byte) {ifcap(*) < .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{pool: sync.Pool{New: func() any { := make([]byte, )return & }, },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(*) >= { * = (*)[:]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() } := make([]byte, )return &}func ( *simpleBufferPool) ( *[]byte) { .pool.Put()}var _ BufferPool = NopBufferPool{}// NopBufferPool is a buffer pool that returns new buffers without pooling.typeNopBufferPoolstruct{}// 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 Goldsv0.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.