github.com/weaviate/weaviate@v1.24.6/entities/storobj/buffer_pool.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package storobj 13 14 import "sync" 15 16 func newBufferPool(initialSize int) *bufferPool { 17 return &bufferPool{ 18 pool: sync.Pool{ 19 New: func() any { 20 // initialize with len=0 to make sure we get a consistent result 21 // whether it's a new or used buffer. Every buffer will always have 22 // len=0 and cap>=initialSize 23 return make([]byte, 0, initialSize) 24 }, 25 }, 26 } 27 } 28 29 type bufferPool struct { 30 pool sync.Pool 31 } 32 33 func (b *bufferPool) Get() []byte { 34 buf := b.pool.Get().([]byte) 35 36 return buf 37 } 38 39 func (b *bufferPool) Put(buf []byte) { 40 // make sure the length is reset before putting it back into the pool. This 41 // way all buffers will always have len=0, either because they are brand-new 42 // or because they have been reset. 43 buf = buf[:0] 44 45 //nolint:staticcheck // I disagree with the linter, this doesn't need to be a 46 //pointer. Even if we copy the slicestruct header, the backing array is 47 //what's allocation-heavy and that will be reused. The profiles in the PR 48 //description prove this. 49 b.pool.Put(buf) 50 }