Source File
iobufpool.go
Belonging Package
github.com/jackc/pgx/v5/internal/iobufpool
// Package iobufpool implements a global segregated-fit pool of buffers for IO.
//
// It uses *[]byte instead of []byte to avoid the sync.Pool allocation with Put. Unfortunately, using a pointer to avoid
// an allocation is purposely not documented. https://github.com/golang/go/issues/16323
package iobufpool
import
const minPoolExpOf2 = 8
var pools [18]*sync.Pool
func init() {
for := range pools {
:= 1 << (minPoolExpOf2 + )
pools[] = &sync.Pool{
New: func() any {
:= make([]byte, )
return &
},
}
}
}
// Get gets a []byte of len size with cap <= size*2.
func ( int) *[]byte {
:= getPoolIdx()
if >= len(pools) {
:= make([]byte, )
return &
}
:= (pools[].Get().(*[]byte))
* = (*)[:]
return
}
func getPoolIdx( int) int {
--
>>= minPoolExpOf2
:= 0
for > 0 {
>>= 1
++
}
return
}
// Put returns buf to the pool.
func ( *[]byte) {
:= putPoolIdx(cap(*))
if < 0 {
return
}
pools[].Put()
}
func putPoolIdx( int) int {
:= 1 << minPoolExpOf2
for := range pools {
if == << {
return
}
}
return -1
}
![]() |
The pages are generated with Golds v0.6.7. (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 @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |