Documentation ¶
Overview ¶
Package bufferv2 provides the implementation of a non-contiguous buffer that is reference counted, pooled, and copy-on-write. It allows O(1) append, and prepend operations.
Index ¶
- Constants
- type Buffer
- func (b *Buffer) Append(src *View) error
- func (b *Buffer) Apply(fn func(*View))
- func (b *Buffer) AsBufferReader() BufferReader
- func (b *Buffer) Checksum(offset int) uint16
- func (b *Buffer) Clone() Buffer
- func (b *Buffer) Flatten() []byte
- func (b *Buffer) GrowTo(length int64, zero bool)
- func (b *Buffer) Merge(other *Buffer)
- func (b *Buffer) Prepend(src *View) error
- func (b *Buffer) PullUp(offset, length int) (View, bool)
- func (b *Buffer) ReadAt(p []byte, offset int64) (int, error)
- func (b *Buffer) ReadToWriter(w io.Writer, count int64) (int64, error)
- func (b *Buffer) Release()
- func (b *Buffer) Size() int64
- func (b *Buffer) SubApply(offset, length int, fn func(*View))
- func (b *Buffer) TrimFront(count int64)
- func (b *Buffer) Truncate(length int64)
- func (b *Buffer) WriteFromReader(r io.Reader, count int64) (int64, error)
- type BufferReader
- type Range
- type View
- func (v *View) AsSlice() []byte
- func (v *View) AvailableSize() int
- func (v *View) BasePtr() *byte
- func (v *View) CapLength(n int)
- func (v *View) Capacity() int
- func (v *View) Clone() *View
- func (v *View) Full() bool
- func (v *View) Grow(n int)
- func (v *View) Read(p []byte) (int, error)
- func (v *View) ReadAt(p []byte, off int) (int, error)
- func (v *View) ReadByte() (byte, error)
- func (v *View) ReadFrom(r io.Reader) (n int64, err error)
- func (v *View) Release()
- func (v *View) Reset()
- func (v *View) Size() int
- func (v *View) ToSlice() []byte
- func (v *View) TrimFront(n int)
- func (v *View) Write(p []byte) (int, error)
- func (v *View) WriteAt(p []byte, off int) (int, error)
- func (v *View) WriteTo(w io.Writer) (n int64, err error)
Constants ¶
const ( // MaxChunkSize is largest payload size that we pool. Payloads larger than // this will be allocated from the heap and garbage collected as normal. MaxChunkSize = baseChunkSize << (numPools - 1) // 64k )
const ReadSize = 512
ReadSize is the default amount that a View's size is increased by when an io.Reader has more data than a View can hold during calls to ReadFrom.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is a non-linear buffer.
+stateify savable
func MakeWithData ¶
MakeWithData creates a new Buffer initialized with given data. This function should be used with caution to avoid unnecessary []byte allocations. When in doubt use NewWithView to maximize chunk reuse.
func MakeWithView ¶
MakeWithView creates a new Buffer initialized with given view. This function takes ownership of v.
func (*Buffer) AsBufferReader ¶
func (b *Buffer) AsBufferReader() BufferReader
AsBufferReader returns the Buffer as a BufferReader capabable of io methods. The new BufferReader takes ownership of b.
func (*Buffer) Checksum ¶
Checksum calculates a checksum over the buffer's payload starting at offset.
func (*Buffer) Clone ¶
Clone creates a copy-on-write clone of b. The underlying chunks are shared until they are written to.
func (*Buffer) Flatten ¶
Flatten returns a flattened copy of this data.
This method should not be used in any performance-sensitive paths. It may allocate a fresh byte slice sufficiently large to contain all the data in the buffer. This is principally for debugging.
N.B. Tee data still belongs to this Buffer, as if there is a single buffer present, then it will be returned directly. This should be used for temporary use only, and a reference to the given slice should not be held.
func (*Buffer) GrowTo ¶
GrowTo grows the given Buffer to the number of bytes, which will be appended. If zero is true, all these bytes will be zero. If zero is false, then this is the caller's responsibility.
Precondition: length must be >= 0.
func (*Buffer) Merge ¶
Merge merges the provided Buffer with this one.
The other Buffer will be appended to v, and other will be empty after this operation completes.
func (*Buffer) ReadToWriter ¶
ReadToWriter reads from the buffer into an io.Writer.
N.B. This does not consume the bytes read. TrimFront should be called appropriately after this call in order to do so.
func (*Buffer) SubApply ¶
SubApply applies fn to a given range of data in b. Any part of the range outside of b is ignored.
type BufferReader ¶
type BufferReader struct {
// contains filtered or unexported fields
}
BufferReader implements io methods on Buffer. Users must call Close() when finished with the buffer to free the underlying memory.
func (*BufferReader) Close ¶
func (br *BufferReader) Close()
Close implements the io.Closer interface.
func (*BufferReader) Len ¶
func (br *BufferReader) Len() int
Len returns the number of bytes in the unread portion of the buffer.
func (*BufferReader) Read ¶
func (br *BufferReader) Read(p []byte) (int, error)
Read implements the io.Reader interface.
func (*BufferReader) ReadByte ¶
func (br *BufferReader) ReadByte() (byte, error)
ReadByte implements the io.ByteReader interface.
type Range ¶
type Range struct {
// contains filtered or unexported fields
}
Range specifies a range of buffer.
type View ¶
type View struct {
// contains filtered or unexported fields
}
View is a window into a shared chunk. Views are held by Buffers in viewLists to represent contiguous memory.
A View must be created with NewView, NewViewWithData, or Clone. Owners are responsible for maintaining ownership over their views. When Views need to be shared or copied, the owner should create a new View with Clone. Clone must only ever be called on a owned View, not a borrowed one.
Users are responsible for calling Release when finished with their View so that its resources can be returned to the pool.
Users must not write directly to slices returned by AsSlice. Instead, they must use Write/WriteAt/CopyIn to modify the underlying View. This preserves the safety guarantees of copy-on-write.
+stateify savable
func NewView ¶
NewView creates a new view with capacity at least as big as cap. It is analogous to make([]byte, 0, cap).
func NewViewSize ¶
NewViewSize creates a new view with capacity at least as big as size and length that is exactly size. It is analogous to make([]byte, size).
func NewViewWithData ¶
NewViewWithData creates a new view and initializes it with data. This function should be used with caution to avoid unnecessary []byte allocations. When in doubt use NewWithView to maximize chunk reuse in production environments.
func (*View) AvailableSize ¶
AvailableSize returns the number of bytes available for writing.
func (*View) CapLength ¶
CapLength caps the length of the view's read slice to n. If n > v.Size(), the function is a no-op.
func (*View) Clone ¶
Clone creates a shallow clone of v where the underlying chunk is shared.
The caller must own the View to call Clone. It is not safe to call Clone on a borrowed or shared View because it can race with other View methods.
func (*View) Full ¶
Full indicates the chunk is full.
This indicates there is no capacity left to write.
func (*View) Grow ¶
Grow increases the size of the view. If the new size is greater than the view's current capacity, Grow will reallocate the view with an increased capacity.
func (*View) ReadAt ¶
ReadAt reads data to the p starting at offset.
Implements the io.ReaderAt interface.
func (*View) ReadFrom ¶
ReadFrom reads data from r until EOF and appends it to the buffer, growing the buffer as needed. The return value n is the number of bytes read. Any error except io.EOF encountered during the read is also returned.
ReadFrom implements the io.ReaderFrom interface.
func (*View) Release ¶
func (v *View) Release()
Release releases the chunk held by v and returns v to the pool.
func (*View) Reset ¶
func (v *View) Reset()
Reset sets the view's read and write indices back to zero.
func (*View) Write ¶
Write writes data to the view's chunk starting at the v.write index. If the view's chunk has a reference count greater than 1, the chunk is copied first and then written to.
Implements the io.Writer interface.