gather: MakeContiguous support for arbitrary chunk sizes (#1247)

This commit is contained in:
Jarek Kowalski
2021-08-22 12:33:36 -07:00
committed by GitHub
parent 9e182f131a
commit a203fa414d
2 changed files with 21 additions and 2 deletions

View File

@@ -41,8 +41,14 @@ func (b *WriteBuffer) MakeContiguous(length int) []byte {
b.mu.Lock()
defer b.mu.Unlock()
b.alloc = contiguousAllocator
v := b.allocChunk()[0:length]
var v []byte
if length > contiguousAllocator.chunkSize {
v = make([]byte, length)
} else {
b.alloc = contiguousAllocator
v = b.allocChunk()[0:length]
}
b.inner.Slices = [][]byte{v}

View File

@@ -4,6 +4,8 @@
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestGatherWriteBuffer(t *testing.T) {
@@ -68,3 +70,14 @@ func TestGatherDefaultWriteBuffer(t *testing.T) {
t.Errorf("invalid number of slices %v, want %v", got, want)
}
}
func TestGatherWriteBufferContig(t *testing.T) {
var w WriteBuffer
defer w.Close()
// allocate more than contig allocator can provide
theCap := contiguousAllocator.chunkSize + 10
b := w.MakeContiguous(theCap)
require.Equal(t, theCap, len(b))
require.Equal(t, theCap, cap(b))
}