mirror of
https://github.com/kopia/kopia.git
synced 2026-03-12 11:16:25 -04:00
, 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
64 lines
1.3 KiB
Go
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{}
|
|
}
|