codec

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package codec provides high-performance encoding/decoding using hyperpb.

Index

Constants

This section is empty.

Variables

View Source
var DefaultDecoderOptions = DecoderOptions{
	EnablePooling:      true,
	InitialPoolSize:    10,
	AllowUnknownFields: false,
	EnablePGO:          false,
}

DefaultDecoderOptions are the default options for decoders.

Functions

func ClearPools

func ClearPools()

ClearPools clears all global pools.

Types

type Codec

type Codec struct {
	// contains filtered or unexported fields
}

Codec provides high-level encoding/decoding operations.

func New

New creates a new codec for the given message descriptor.

func (*Codec) Descriptor

func (c *Codec) Descriptor() protoreflect.MessageDescriptor

Descriptor returns the message descriptor.

func (*Codec) Marshal

func (c *Codec) Marshal(msg protobuf.Message) ([]byte, error)

Marshal encodes a message to bytes.

func (*Codec) MarshalToJSON

func (c *Codec) MarshalToJSON(msg protobuf.Message) ([]byte, error)

MarshalToJSON encodes a message to JSON.

func (*Codec) NewMessage

func (c *Codec) NewMessage() protobuf.Message

NewMessage creates a new message instance.

func (*Codec) ReleaseMessage

func (c *Codec) ReleaseMessage(msg protobuf.Message)

ReleaseMessage returns a message to the pool.

func (*Codec) Unmarshal

func (c *Codec) Unmarshal(data []byte) (protobuf.Message, error)

Unmarshal decodes bytes to a message.

func (*Codec) UnmarshalFromJSON

func (c *Codec) UnmarshalFromJSON(data []byte) (protobuf.Message, error)

UnmarshalFromJSON decodes JSON to a message.

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder handles decoding protobuf format to Go values using hyperpb.

func NewDecoder

NewDecoder creates a new decoder for the given message descriptor.

func (*Decoder) Decode

func (d *Decoder) Decode(data []byte) (*hyperpb.Message, error)

Decode unmarshals bytes to a protobuf message.

func (*Decoder) DecodeInto

func (d *Decoder) DecodeInto(data []byte, msg protobuf.Message) error

DecodeInto unmarshals bytes into an existing message.

func (*Decoder) DecodeJSON

func (d *Decoder) DecodeJSON(data []byte) (*hyperpb.Message, error)

DecodeJSON unmarshals JSON to a protobuf message.

func (*Decoder) Descriptor

func (d *Decoder) Descriptor() protoreflect.MessageDescriptor

Descriptor returns the message descriptor.

func (*Decoder) GetMessage

func (d *Decoder) GetMessage() *hyperpb.Message

GetMessage returns a message from the pool or creates a new one.

func (*Decoder) PutMessage

func (d *Decoder) PutMessage(msg *hyperpb.Message)

PutMessage returns a message to the pool.

type DecoderOptions

type DecoderOptions struct {
	// EnablePooling enables message pooling for better performance
	EnablePooling bool
	// InitialPoolSize sets the initial pool size
	InitialPoolSize int
	// AllowUnknownFields allows unknown fields in the input
	AllowUnknownFields bool
	// EnablePGO enables profile-guided optimization
	EnablePGO bool
}

DecoderOptions configures the decoder.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder handles encoding Go values to protobuf format using hyperpb.

func NewEncoder

NewEncoder creates a new encoder for the given message descriptor.

func (*Encoder) Descriptor

func (e *Encoder) Descriptor() protoreflect.MessageDescriptor

Descriptor returns the message descriptor.

func (*Encoder) Encode

func (e *Encoder) Encode(msg protobuf.Message) ([]byte, error)

Encode marshals a protobuf message to bytes.

func (*Encoder) EncodeJSON

func (e *Encoder) EncodeJSON(msg protobuf.Message) ([]byte, error)

EncodeJSON marshals a protobuf message to JSON.

func (*Encoder) GetMessage

func (e *Encoder) GetMessage() *hyperpb.Message

GetMessage returns a message from the pool or creates a new one.

func (*Encoder) PutMessage

func (e *Encoder) PutMessage(msg *hyperpb.Message)

PutMessage returns a message to the pool.

type EncoderOptions

type EncoderOptions struct {
	// EnablePooling enables message pooling for better performance
	EnablePooling bool
	// InitialPoolSize sets the initial pool size
	InitialPoolSize int
}

EncoderOptions configures the encoder.

type GlobalPools

type GlobalPools struct {
	// contains filtered or unexported fields
}

GlobalPools manages pools for all message types.

type MessagePool

type MessagePool struct {
	// contains filtered or unexported fields
}

MessagePool manages a pool of reusable messages.

func GetPool

GetPool returns a pool for the given message descriptor.

func NewMessagePool

func NewMessagePool(md protoreflect.MessageDescriptor) (*MessagePool, error)

NewMessagePool creates a new message pool.

func (*MessagePool) Get

func (p *MessagePool) Get() *hyperpb.Message

Get retrieves a message from the pool.

func (*MessagePool) Put

func (p *MessagePool) Put(msg *hyperpb.Message)

Put returns a message to the pool.

func (*MessagePool) Stats

func (p *MessagePool) Stats() PoolStats

Stats returns the current pool statistics.

type Options

type Options struct {
	// EnablePooling enables message pooling
	EnablePooling bool
	// PoolSize sets the initial pool size
	PoolSize int
	// AllowUnknownFields allows unknown fields when decoding
	AllowUnknownFields bool
}

Options configures codec behavior.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns default codec options.

type PoolStats

type PoolStats struct {
	Gets    atomic.Int64
	Puts    atomic.Int64
	News    atomic.Int64
	InUse   atomic.Int64
	MaxSize atomic.Int64
}

PoolStats tracks pool usage statistics.

Jump to

Keyboard shortcuts

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