Files
kopia/internal/gather/gather_write_buffer.go
Jarek Kowalski 60977812f0 Support for gather writes (#373)
, where blob.Storage.PutBlob gets a list of slices and writes them sequentially 
* performance: added gather.Bytes and gather.WriteBuffer

They are similar to bytes.Buffer but instead of managing a single
byte slice, they maintain a list of slices that and when they run out of
space they allocate new fixed-size slice from a free list.

This helps keep memory allocations completely under control regardless
of the size of data written.

* switch from byte slices and bytes.Buffer to gather.Bytes.

This is mostly mechanical, the only cases where it's not involve blob
storage providers, where we leverage the fact that we don't need to
ever concatenate the slices into one and instead we can do gather
writes.

* PR feedback
2020-03-24 15:05:52 -07:00

64 lines
1.3 KiB
Go

package gather
// WriteBuffer is a write buffer for content of unknown size that manages
// data in a series of byte slices of uniform size.
type WriteBuffer struct {
Bytes
}
// Close releases all memory allocated by this buffer.
func (b *WriteBuffer) Close() {
for _, s := range b.Slices {
releaseChunk(s)
}
b.Slices = nil
}
// Reset resets buffer back to empty.
func (b *WriteBuffer) Reset() {
for _, s := range b.Slices {
releaseChunk(s)
}
b.Slices = nil
}
// Write implements io.Writer for appending to the buffer.
func (b *WriteBuffer) Write(data []byte) (n int, err error) {
b.Append(data)
return len(data), nil
}
// Append appends the specified slice of bytes to the buffer.
func (b *WriteBuffer) Append(data []byte) {
if len(b.Slices) == 0 {
b.sliceBuf[0] = allocChunk()
b.Slices = b.sliceBuf[0:1]
}
for len(data) > 0 {
ndx := len(b.Slices) - 1
remaining := cap(b.Slices[ndx]) - len(b.Slices[ndx])
if remaining == 0 {
b.Slices = append(b.Slices, allocChunk())
ndx = len(b.Slices) - 1
remaining = cap(b.Slices[ndx]) - len(b.Slices[ndx])
}
chunkSize := remaining
if chunkSize > len(data) {
chunkSize = len(data)
}
b.Slices[ndx] = append(b.Slices[ndx], data[0:chunkSize]...)
data = data[chunkSize:]
}
}
// NewWriteBuffer creates new write buffer.
func NewWriteBuffer() *WriteBuffer {
return &WriteBuffer{}
}