Always return slice with cap (#19395)

Documentation promised this - so we should do it as well. Try to get a buffer and stash if it isn't big enough.
This commit is contained in:
Klaus Post 2024-04-02 08:56:18 -07:00 committed by GitHub
parent 4f660a8eb7
commit 912bbb2f1d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -87,12 +87,19 @@ var GetByteBuffer = func() []byte {
// GetByteBufferCap returns a length 0 byte buffer with at least the given capacity.
func GetByteBufferCap(wantSz int) []byte {
switch {
case wantSz <= defaultBufferSize:
return GetByteBuffer()[:0]
case wantSz <= maxBufferSize:
if wantSz < defaultBufferSize {
b := GetByteBuffer()[:0]
if cap(b) >= wantSz {
return b
}
PutByteBuffer(b)
}
if wantSz <= maxBufferSize {
b := *internal32KByteBuffer.Get().(*[]byte)
return b[:0]
if cap(b) >= wantSz {
return b[:0]
}
internal32KByteBuffer.Put(&b)
}
return make([]byte, 0, wantSz)
}