Documentation
¶
Overview ¶
Package bytebuf provides pooled contiguous and segmented byte buffers with explicit ownership transfer.
Index ¶
- Constants
- Variables
- func ReleaseBuffer(buffer *Buffer)
- type Buffer
- func (b *Buffer) Available() int
- func (b *Buffer) AvailableBuffer() []byte
- func (b *Buffer) Bytes() []byte
- func (b *Buffer) Cap() int
- func (b *Buffer) CommitWrite(n int)
- func (b *Buffer) Discard(n int)
- func (b *Buffer) Grow(n int)
- func (b *Buffer) Len() int
- func (b *Buffer) Next(n int) []byte
- func (b *Buffer) Read(p []byte) (n int, err error)
- func (b *Buffer) ReadByte() (byte, error)
- func (b *Buffer) ReadBytes(delim byte) (line []byte, err error)
- func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)
- func (b *Buffer) ReadRune() (r rune, size int, err error)
- func (b *Buffer) ReadString(delim byte) (line string, err error)
- func (b *Buffer) Reset()
- func (b *Buffer) String() string
- func (b *Buffer) Truncate(n int)
- func (b *Buffer) UnreadByte() error
- func (b *Buffer) UnreadRune() error
- func (b *Buffer) Write(p []byte) (n int, err error)
- func (b *Buffer) WriteByte(c byte) error
- func (b *Buffer) WriteRune(r rune) (n int, err error)
- func (b *Buffer) WriteString(s string) (n int, err error)
- func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)
- type CompositeBuffer
- func (b *CompositeBuffer) AppendOwned(buffer *Buffer)
- func (b *CompositeBuffer) AppendOwnedCoalesced(buffer *Buffer, targetCapacity int)
- func (b *CompositeBuffer) Available() (bytes int)
- func (b *CompositeBuffer) Blocks() int
- func (b *CompositeBuffer) Cap() (capacity int)
- func (b *CompositeBuffer) Close() error
- func (b *CompositeBuffer) Discard(n int) int
- func (b *CompositeBuffer) Empty() bool
- func (b *CompositeBuffer) Gather(n int, tail []byte) (rest []byte)
- func (b *CompositeBuffer) Len() int
- func (b *CompositeBuffer) Peek(p []byte) []byte
- func (b *CompositeBuffer) PeekChunk() []byte
- func (b *CompositeBuffer) PeekVec(dst [][]byte) (vec [][]byte, length int)
- func (b *CompositeBuffer) PeekVecN(dst [][]byte, limit int) (vec [][]byte, length int)
- func (b *CompositeBuffer) PopBlock() *Buffer
- func (b *CompositeBuffer) PrependOwned(buffer *Buffer)
- func (b *CompositeBuffer) Read(p []byte) (n int, err error)
- func (b *CompositeBuffer) ReadFrom(r io.Reader) (n int64, err error)
- func (b *CompositeBuffer) Reserve(n, blockSize int) []byte
- func (b *CompositeBuffer) Reset()
- func (b *CompositeBuffer) Write(p []byte) (n int, err error)
- func (b *CompositeBuffer) WriteByte(c byte) error
- func (b *CompositeBuffer) WriteCoalesced(p []byte, blockSize int) int
- func (b *CompositeBuffer) WriteString(s string) (n int, err error)
- func (b *CompositeBuffer) WriteTo(w io.Writer) (n int64, err error)
- func (b *CompositeBuffer) Writev(vec [][]byte) (n int, err error)
- func (b *CompositeBuffer) WritevCoalesced(vec [][]byte, blockSize int) int
- type SharedBuffer
Constants ¶
const MinRead = 512
MinRead is the minimum slice size passed to a Read call by Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond what is required to hold the contents of r, ReadFrom will not grow the underlying buffer.
Variables ¶
var ErrTooLarge = errors.New("bytebuf.Buffer: too large")
ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
Functions ¶
func ReleaseBuffer ¶ added in v1.1.0
func ReleaseBuffer(buffer *Buffer)
ReleaseBuffer returns an owned buffer to the pool.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
A Buffer is a variable-sized buffer of bytes with Buffer.Read and Buffer.Write methods. The zero value for Buffer is an empty buffer ready to use.
func AcquireBuffer ¶ added in v1.1.0
AcquireBuffer returns an empty pooled buffer with at least capacity bytes of writable space. Release it with ReleaseBuffer when ownership is not transferred to a CompositeBuffer.
func CloneBuffer ¶ added in v1.1.0
CloneBuffer returns a pooled buffer containing one copy of p. The caller owns the returned buffer until it is appended to a CompositeBuffer or released with ReleaseBuffer.
func CloneBuffers ¶ added in v1.1.0
CloneBuffers returns one owned buffer containing one copy of every segment.
func CloneBuffersFrom ¶ added in v1.1.0
CloneBuffersFrom copies size bytes from vec after skipping the first skip bytes into one owned buffer.
func NewBuffer ¶
NewBuffer creates and initializes a new Buffer using buf as its initial contents. The new Buffer takes ownership of buf, and the caller should not use buf after this call. NewBuffer is intended to prepare a Buffer to read existing data. It can also be used to set the initial size of the internal buffer for writing. To do that, buf should have the desired capacity but a length of zero.
In most cases, new(Buffer) (or just declaring a Buffer variable) is sufficient to initialize a Buffer.
func NewBufferString ¶
NewBufferString creates and initializes a new Buffer using string s as its initial contents. It is intended to prepare a buffer to read an existing string.
In most cases, new(Buffer) (or just declaring a Buffer variable) is sufficient to initialize a Buffer.
func (*Buffer) AvailableBuffer ¶
AvailableBuffer returns an empty buffer with b.Available() capacity. This buffer is intended to be appended to and passed to an immediately succeeding Buffer.Write call. The buffer is only valid until the next write operation on b.
func (*Buffer) Bytes ¶
Bytes returns a slice of length b.Len() holding the unread portion of the buffer. The slice is valid for use only until the next buffer modification (that is, only until the next call to a method like Buffer.Read, Buffer.Write, Buffer.Reset, or Buffer.Truncate). The slice aliases the buffer content at least until the next buffer modification, so immediate changes to the slice will affect the result of future reads.
func (*Buffer) Cap ¶
Cap returns the capacity of the buffer's underlying byte slice, that is, the total space allocated for the buffer's data.
func (*Buffer) CommitWrite ¶
CommitWrite extends the readable length after a caller writes directly into AvailableBuffer. It panics when n exceeds the advertised capacity.
func (*Buffer) Discard ¶
Discard drops the next n unread bytes. n <= 0 drops all of them, Discard(0) included, so a caller that may have nothing to drop must check for it.
func (*Buffer) Grow ¶
Grow grows the buffer's capacity, if necessary, to guarantee space for another n bytes. After Grow(n), at least n bytes can be written to the buffer without another allocation. If n is negative, Grow will panic. If the buffer can't grow it will panic with ErrTooLarge.
func (*Buffer) Len ¶
Len returns the number of bytes of the unread portion of the buffer; b.Len() == len(b.Bytes()).
func (*Buffer) Next ¶
Next returns a slice containing the next n bytes from the buffer, advancing the buffer as if the bytes had been returned by Buffer.Read. If there are fewer than n bytes in the buffer, Next returns the entire buffer. The slice is only valid until the next call to a read or write method.
func (*Buffer) Read ¶
Read reads the next len(p) bytes from the buffer or until the buffer is drained. The return value n is the number of bytes read. If the buffer has no data to return, err is io.EOF (unless len(p) is zero); otherwise it is nil.
func (*Buffer) ReadByte ¶
ReadByte reads and returns the next byte from the buffer. If no byte is available, it returns error io.EOF.
func (*Buffer) ReadBytes ¶
ReadBytes reads until the first occurrence of delim in the input, returning a slice containing the data up to and including the delimiter. If ReadBytes encounters an error before finding a delimiter, it returns the data read before the error and the error itself (often io.EOF). ReadBytes returns err != nil if and only if the returned data does not end in delim.
func (*Buffer) 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. If the buffer becomes too large, ReadFrom will panic with ErrTooLarge.
func (*Buffer) ReadRune ¶
ReadRune reads and returns the next UTF-8-encoded Unicode code point from the buffer. If no bytes are available, the error returned is io.EOF. If the bytes are an erroneous UTF-8 encoding, it consumes one byte and returns U+FFFD, 1.
func (*Buffer) ReadString ¶
ReadString reads until the first occurrence of delim in the input, returning a string containing the data up to and including the delimiter. If ReadString encounters an error before finding a delimiter, it returns the data read before the error and the error itself (often io.EOF). ReadString returns err != nil if and only if the returned data does not end in delim.
func (*Buffer) Reset ¶
func (b *Buffer) Reset()
Reset resets the buffer to be empty, but it retains the underlying storage for use by future writes. Reset is the same as Buffer.Truncate(0).
func (*Buffer) String ¶
String returns the contents of the unread portion of the buffer as a string. If the Buffer is a nil pointer, it returns "<nil>".
To build strings more efficiently, see the strings.Builder type.
func (*Buffer) Truncate ¶
Truncate discards all but the first n unread bytes from the buffer but continues to use the same allocated storage. It panics if n is negative or greater than the length of the buffer.
func (*Buffer) UnreadByte ¶
UnreadByte unreads the last byte returned by the most recent successful read operation that read at least one byte. If a write has happened since the last read, if the last read returned an error, or if the read read zero bytes, UnreadByte returns an error.
func (*Buffer) UnreadRune ¶
UnreadRune unreads the last rune returned by Buffer.ReadRune. If the most recent read or write operation on the buffer was not a successful Buffer.ReadRune, UnreadRune returns an error. (In this regard it is stricter than Buffer.UnreadByte, which will unread the last byte from any read operation.)
func (*Buffer) Write ¶
Write appends the contents of p to the buffer, growing the buffer as needed. The return value n is the length of p; err is always nil. If the buffer becomes too large, Write will panic with ErrTooLarge.
func (*Buffer) WriteByte ¶
WriteByte appends the byte c to the buffer, growing the buffer as needed. The returned error is always nil, but is included to match bufio.Writer's WriteByte. If the buffer becomes too large, WriteByte will panic with ErrTooLarge.
func (*Buffer) WriteRune ¶
WriteRune appends the UTF-8 encoding of Unicode code point r to the buffer, returning its length and an error, which is always nil but is included to match bufio.Writer's WriteRune. The buffer is grown as needed; if it becomes too large, WriteRune will panic with ErrTooLarge.
func (*Buffer) WriteString ¶
WriteString appends the contents of s to the buffer, growing the buffer as needed. The return value n is the length of s; err is always nil. If the buffer becomes too large, WriteString will panic with ErrTooLarge.
type CompositeBuffer ¶
type CompositeBuffer struct {
// contains filtered or unexported fields
}
CompositeBuffer keeps payload in separately pooled blocks so writev can expose segments without joining or copying them.
func NewCompositeBuffer ¶
func NewCompositeBuffer() *CompositeBuffer
NewCompositeBuffer returns an empty segmented buffer.
func (*CompositeBuffer) AppendOwned ¶ added in v1.1.0
func (b *CompositeBuffer) AppendOwned(buffer *Buffer)
AppendOwned transfers ownership of buffer to the composite buffer without copying its payload. An empty buffer is released immediately.
func (*CompositeBuffer) AppendOwnedCoalesced ¶ added in v1.4.0
func (b *CompositeBuffer) AppendOwnedCoalesced(buffer *Buffer, targetCapacity int)
AppendOwnedCoalesced consumes buffer and packs small adjacent payloads into pooled blocks with at least targetCapacity. It is intended for a corked I/O round where reducing writev segments is worth one payload copy.
func (*CompositeBuffer) Available ¶
func (b *CompositeBuffer) Available() (bytes int)
Available returns how many bytes are unused in the buffer.
func (*CompositeBuffer) Blocks ¶ added in v1.4.1
func (b *CompositeBuffer) Blocks() int
Blocks returns how many blocks hold the unread bytes.
func (*CompositeBuffer) Cap ¶
func (b *CompositeBuffer) Cap() (capacity int)
Cap returns the capacity of the buffer's underlying byte slice, that is, the total space allocated for the buffer's data.
func (*CompositeBuffer) Close ¶
func (b *CompositeBuffer) Close() error
Close resets the buffer to be empty, Close is the same as CompositeBuffer.Reset
func (*CompositeBuffer) Discard ¶
func (b *CompositeBuffer) Discard(n int) int
Discard advances by at most n bytes and returns the number discarded. A non-positive n discards everything. Fully consumed blocks return to their originating pool.
func (*CompositeBuffer) Empty ¶
func (b *CompositeBuffer) Empty() bool
Empty reports whether the unread portion of the buffer is empty.
func (*CompositeBuffer) Gather ¶ added in v1.7.0
func (b *CompositeBuffer) Gather(n int, tail []byte) (rest []byte)
Gather makes the unread bytes start one block with room for n of them: it appends as much of tail as the first n bytes need and returns the rest, so the first min(n, Len) unread bytes are one chunk and later appends up to n join it. A first and only block with that room is kept; otherwise the unread bytes are copied into a new block of at least n bytes and the old blocks are released, which invalidates slices returned earlier by Peek, PeekChunk or PeekVec.
func (*CompositeBuffer) Len ¶
func (b *CompositeBuffer) Len() int
Len returns the number of bytes of the unread portion of the buffer;
func (*CompositeBuffer) Peek ¶
func (b *CompositeBuffer) Peek(p []byte) []byte
Peek returns the next len(b) bytes without advancing the buffer.
func (*CompositeBuffer) PeekChunk ¶ added in v1.1.0
func (b *CompositeBuffer) PeekChunk() []byte
PeekChunk returns the first contiguous unread block without advancing it.
func (*CompositeBuffer) PeekVec ¶
func (b *CompositeBuffer) PeekVec(dst [][]byte) (vec [][]byte, length int)
PeekVec appends all unread segments to dst without advancing the buffer. Returned slices remain valid until the corresponding blocks are discarded.
func (*CompositeBuffer) PeekVecN ¶
func (b *CompositeBuffer) PeekVecN(dst [][]byte, limit int) (vec [][]byte, length int)
PeekVecN returns at most limit unread segments without advancing the buffer. The limit prevents a flush from walking or allocating for the whole queue.
func (*CompositeBuffer) PopBlock ¶ added in v1.9.0
func (b *CompositeBuffer) PopBlock() *Buffer
PopBlock detaches and returns the front unread block, transferring ownership to the caller, or returns nil when empty.
func (*CompositeBuffer) PrependOwned ¶ added in v1.5.1
func (b *CompositeBuffer) PrependOwned(buffer *Buffer)
PrependOwned is AppendOwned at the head of the buffer: its bytes are read before everything already queued.
func (*CompositeBuffer) Read ¶
func (b *CompositeBuffer) Read(p []byte) (n int, err error)
Read reads the next len(p) bytes from the buffer or until the buffer is drained. The return value n is the number of bytes read. If the buffer has no data to return, err is io.EOF (unless len(p) is zero); otherwise it is nil.
func (*CompositeBuffer) ReadFrom ¶
func (b *CompositeBuffer) ReadFrom(r io.Reader) (n int64, err error)
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.
func (*CompositeBuffer) Reserve ¶ added in v1.4.1
func (b *CompositeBuffer) Reserve(n, blockSize int) []byte
Reserve appends n bytes and returns them for the caller to fill in place. They stay in the tail block when it has room; otherwise a new block is started, exactly sized when the buffer is empty and at least blockSize otherwise, so a burst of small reservations shares a few large blocks.
func (*CompositeBuffer) Reset ¶
func (b *CompositeBuffer) Reset()
Reset returns payload blocks to the shared pool and retains the pointer list.
func (*CompositeBuffer) Write ¶
func (b *CompositeBuffer) Write(p []byte) (n int, err error)
Write appends the contents of p to the buffer, growing the buffer as needed. The return value n is the length of p; err is always nil.
func (*CompositeBuffer) WriteByte ¶
func (b *CompositeBuffer) WriteByte(c byte) error
WriteByte appends the byte c to the buffer, growing the buffer as needed. The returned error is always nil, but is included to match bufio.Writer's WriteByte.
func (*CompositeBuffer) WriteCoalesced ¶ added in v1.8.0
func (b *CompositeBuffer) WriteCoalesced(p []byte, blockSize int) int
WriteCoalesced appends p the way WritevCoalesced appends its vector: into the tail block's room, or a new block of at least blockSize while output is already queued, so back-to-back writes a slow peer makes queue do not take one block each.
func (*CompositeBuffer) WriteString ¶
func (b *CompositeBuffer) WriteString(s string) (n int, err error)
WriteString appends the contents of s to the buffer, growing the buffer as needed. The return value n is the length of s; err is always nil.
func (*CompositeBuffer) WriteTo ¶
func (b *CompositeBuffer) WriteTo(w io.Writer) (n int64, err error)
WriteTo writes data to w until the buffer is drained or an error occurs. The return value n is the number of bytes written; it always fits into an int, but it is int64 to match the io.WriterTo interface. Any error encountered during the write is also returned.
func (*CompositeBuffer) Writev ¶
func (b *CompositeBuffer) Writev(vec [][]byte) (n int, err error)
Writev appends the contents of vec to the buffer, growing the buffer as needed. The return value n is the length of vec; err is always nil.
func (*CompositeBuffer) WritevCoalesced ¶ added in v1.4.1
func (b *CompositeBuffer) WritevCoalesced(vec [][]byte, blockSize int) int
WritevCoalesced appends the contents of vec. Once the tail block is full it continues in a new block sized like Reserve's, so small appends share blocks instead of taking one pooled block each.
type SharedBuffer ¶ added in v1.9.0
type SharedBuffer struct {
// contains filtered or unexported fields
}
SharedBuffer holds an immutable, read-only byte payload that can be queued across any number of connections without copying or per-connection heap allocations. Its backing storage returns to UIO's buffer pool once the broadcaster calls Release and all in-flight connection views finish sending.
SharedBuffer instances themselves are intentionally not pooled so a late buggy Release or AcquireView after Release always panics deterministically instead of racing a recycled handle (no ABA hazard).
func FreezeBuffer ¶ added in v1.9.0
func FreezeBuffer(buf *Buffer) *SharedBuffer
FreezeBuffer seals buf into an immutable SharedBuffer and clears *buf in place so subsequent mutations or ReleaseBuffer calls on buf cannot affect the frozen broadcast bytes.
func NewSharedBuffer ¶ added in v1.9.0
func NewSharedBuffer(p []byte) *SharedBuffer
NewSharedBuffer copies p once into a pooled Buffer and freezes it into an immutable SharedBuffer. The caller may freely mutate or reuse p afterward.
func (*SharedBuffer) AcquireView ¶ added in v1.9.0
func (sb *SharedBuffer) AcquireView() *Buffer
AcquireView borrows a pooled per-connection read-only *Buffer view over sb. It panics if sb is nil or has already been released.
func (*SharedBuffer) Bytes ¶ added in v1.9.0
func (sb *SharedBuffer) Bytes() []byte
Bytes returns the immutable payload slice with capacity capped to length.
func (*SharedBuffer) InitFreeze ¶ added in v1.9.0
func (sb *SharedBuffer) InitFreeze(buf *Buffer)
InitFreeze seals buf into sb in place and clears *buf so subsequent mutations or ReleaseBuffer calls on buf cannot affect the frozen broadcast bytes.
func (*SharedBuffer) InitOwned ¶ added in v1.9.0
func (sb *SharedBuffer) InitOwned(buf *Buffer)
InitOwned initializes sb directly from an internal freshly-acquired *Buffer that is never exposed to external callers, avoiding the extra *Buffer header clone allocation.
func (*SharedBuffer) Len ¶ added in v1.9.0
func (sb *SharedBuffer) Len() int
Len returns the byte length of the shared payload.
func (*SharedBuffer) Release ¶ added in v1.9.0
func (sb *SharedBuffer) Release()
Release ends the broadcaster's hold on sb. Once all in-flight connection views have also completed, the underlying pooled buffer is recycled. Calling Release more than once panics without decrementing in-flight references.