codec

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2026 License: MIT Imports: 3 Imported by: 0

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

View Source
const (
	IDRaw byte = 0x00
	IDLZ4 byte = 0x01
)

Protocol compression identifiers.

Variables

This section is empty.

Functions

func Register

func Register(c Codec)

Register adds a codec to the global registry. It is called during init() by each codec implementation. Panics if a codec with the same ID is already registered.

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

func Get(id byte) Codec

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).

func Raw

func Raw() Codec

Raw returns a codec that performs no compression (pass-through copy). Encode and Decode simply copy src into dst. This is the fastest codec and is used when compression overhead is not justified (small frames, already-compressed data, or LAN with abundant bandwidth).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL