Documentation
¶
Overview ¶
Package codec provides frame payload compression and decompression for the compose protocol. It defines a Codec interface with pluggable implementations and a global registry for protocol-level codec negotiation.
Implementations must be safe for concurrent use. The Encode and Decode methods accept caller-provided destination buffers to avoid allocations on the hot path. When the caller provides a sufficiently sized buffer, zero allocations occur.
Available codecs:
- Raw (ID 0x00): Pass-through copy, no compression.
- LZ4 (ID 0x01): LZ4 block compression via github.com/pierrec/lz4/v4.
Registration happens automatically via init() in each codec's source file. Use Get(id) to retrieve a codec by its protocol identifier.
Index ¶
Constants ¶
const ( IDRaw byte = 0x00 IDLZ4 byte = 0x01 )
Protocol compression identifiers.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Codec ¶
type Codec interface {
// Encode compresses src into dst. Returns the compressed slice (sub-slice of dst).
// dst must be large enough (use MaxEncodedSize to determine required capacity).
// If dst is nil or too small, a new buffer is allocated (slow path).
Encode(dst, src []byte) ([]byte, error)
// Decode decompresses src into dst. Returns the decompressed slice.
// dst must be large enough to hold the uncompressed data.
// If dst is nil or too small, a new buffer is allocated (slow path).
Decode(dst, src []byte) ([]byte, error)
// ID returns the protocol compression identifier.
ID() byte
// MaxEncodedSize returns the maximum possible compressed size for input of
// the given length. Use this to pre-allocate destination buffers.
MaxEncodedSize(srcLen int) int
}
Codec compresses and decompresses frame pixel data. Implementations must be safe for concurrent use. Encode/Decode must not allocate on the hot path when the caller provides a sufficiently sized destination buffer.
func Get ¶
Get returns the codec for the given protocol ID. Returns nil if no codec is registered for that ID.
func LZ4 ¶
func LZ4() Codec
LZ4 returns a codec using LZ4 block compression. It uses a pooled lz4.Compressor to avoid allocations on the hot path. The compressor hash table is reused across calls via sync.Pool.
LZ4 provides fast compression with moderate ratios, making it ideal for frame pixel data that contains large flat color regions (typical in GUIs).