or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

compiler-protogen.mdcore.mdencoding.mdindex.mdreflection.mdruntime.mdtesting.mdtypes-descriptorpb.mdtypes-dynamicpb.mdtypes-known.mdtypes-pluginpb.md
tile.json

runtime.mddocs/

Runtime Support

The runtime packages provide low-level interfaces and implementation details used by generated protocol buffer code. These packages define the contract between generated code and the runtime library, including fast-path optimizations, message implementation infrastructure, and lazy decoding control.

WARNING: These packages should only be imported by generated message implementations. The compatibility agreement covers only functionality needed to keep existing generated messages operational.

Package Imports

import (
    "google.golang.org/protobuf/runtime/protoiface"
    "google.golang.org/protobuf/runtime/protoimpl"
    "google.golang.org/protobuf/runtime/protolazy"
    "google.golang.org/protobuf/reflect/protoreflect"
)

Proto Interface Types

Legacy Message Interface

The v1 proto.Message interface for backward compatibility.

// MessageV1 is the v1 proto.Message interface
type MessageV1 interface {
    Reset()
    String() string
    ProtoMessage()
}

Fast-Path Methods

Optional fast-path implementations of various message operations.

// Methods is a set of optional fast-path implementations of various operations
type Methods struct {
    pragma.NoUnkeyedLiterals

    // Flags indicate support for optional features
    Flags SupportFlags

    // Size returns the size in bytes of the wire-format encoding of a message
    // Marshal must be provided if a custom Size is provided
    Size func(SizeInput) SizeOutput

    // Marshal formats a message in the wire-format encoding to the provided buffer
    // Size should be provided if a custom Marshal is provided
    // It must not return an error for a partial message
    Marshal func(MarshalInput) (MarshalOutput, error)

    // Unmarshal parses the wire-format encoding and merges the result into a message
    // It must not reset the target message or return an error for a partial message
    Unmarshal func(UnmarshalInput) (UnmarshalOutput, error)

    // Merge merges the contents of a source message into a destination message
    Merge func(MergeInput) MergeOutput

    // CheckInitialized returns an error if any required fields in the message are not set
    CheckInitialized func(CheckInitializedInput) (CheckInitializedOutput, error)

    // Equal compares two messages and returns EqualOutput.Equal == true if they are equal
    Equal func(EqualInput) EqualOutput
}

Support Flags

Indicate support for optional features in fast-path methods.

// SupportFlags indicate support for optional features
type SupportFlags uint64

const (
    // SupportMarshalDeterministic reports whether MarshalOptions.Deterministic is supported
    SupportMarshalDeterministic SupportFlags = 1 << iota

    // SupportUnmarshalDiscardUnknown reports whether UnmarshalOptions.DiscardUnknown is supported
    SupportUnmarshalDiscardUnknown
)

Marshal Operations

Marshal Input and Output

Types for the Marshal fast-path method.

// MarshalInput is input to the Marshal method
type MarshalInput struct {
    pragma.NoUnkeyedLiterals

    Message protoreflect.Message
    Buf     []byte // output is appended to this buffer
    Flags   MarshalInputFlags
}

// MarshalInputFlags configure the marshaler
// Most flags correspond to fields in proto.MarshalOptions
type MarshalInputFlags uint8

const (
    MarshalDeterministic MarshalInputFlags = 1 << iota
    MarshalUseCachedSize
)

// MarshalOutput is output from the Marshal method
type MarshalOutput struct {
    pragma.NoUnkeyedLiterals

    Buf []byte // contains marshaled message
}

Size Operations

Types for the Size fast-path method.

// SizeInput is input to the Size method
type SizeInput struct {
    pragma.NoUnkeyedLiterals

    Message protoreflect.Message
    Flags   MarshalInputFlags
}

// SizeOutput is output from the Size method
type SizeOutput struct {
    pragma.NoUnkeyedLiterals

    Size int
}

Unmarshal Operations

Unmarshal Input and Output

Types for the Unmarshal fast-path method.

// UnmarshalInput is input to the Unmarshal method
type UnmarshalInput struct {
    pragma.NoUnkeyedLiterals

    Message  protoreflect.Message
    Buf      []byte // input buffer
    Flags    UnmarshalInputFlags
    Resolver interface {
        FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error)
        FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error)
    }
    Depth int
}

// UnmarshalInputFlags configure the unmarshaler
// Most flags correspond to fields in proto.UnmarshalOptions
type UnmarshalInputFlags uint8

const (
    UnmarshalDiscardUnknown UnmarshalInputFlags = 1 << iota

    // UnmarshalAliasBuffer permits unmarshal operations to alias the input buffer
    // The unmarshaller must not modify the contents of the buffer
    UnmarshalAliasBuffer

    // UnmarshalValidated indicates that validation has already been
    // performed on the input buffer
    UnmarshalValidated

    // UnmarshalCheckRequired is set if this unmarshal operation ultimately will care if required fields are
    // initialized
    UnmarshalCheckRequired

    // UnmarshalNoLazyDecoding is set if this unmarshal operation should not use
    // lazy decoding, even when otherwise available
    UnmarshalNoLazyDecoding
)

// UnmarshalOutput is output from the Unmarshal method
type UnmarshalOutput struct {
    pragma.NoUnkeyedLiterals

    Flags UnmarshalOutputFlags
}

// UnmarshalOutputFlags are output from the Unmarshal method
type UnmarshalOutputFlags uint8

const (
    // UnmarshalInitialized may be set on return if all required fields are known to be set
    // If unset, then it does not necessarily indicate that the message is uninitialized,
    // only that its status could not be confirmed
    UnmarshalInitialized UnmarshalOutputFlags = 1 << iota
)

Merge Operations

Merge Input and Output

Types for the Merge fast-path method.

// MergeInput is input to the Merge method
type MergeInput struct {
    pragma.NoUnkeyedLiterals

    Source      protoreflect.Message
    Destination protoreflect.Message
}

// MergeOutput is output from the Merge method
type MergeOutput struct {
    pragma.NoUnkeyedLiterals

    Flags MergeOutputFlags
}

// MergeOutputFlags are output from the Merge method
type MergeOutputFlags uint8

const (
    // MergeComplete reports whether the merge was performed
    // If unset, the merger must have made no changes to the destination
    MergeComplete MergeOutputFlags = 1 << iota
)

CheckInitialized Operations

CheckInitialized Input and Output

Types for the CheckInitialized fast-path method.

// CheckInitializedInput is input to the CheckInitialized method
type CheckInitializedInput struct {
    pragma.NoUnkeyedLiterals

    Message protoreflect.Message
}

// CheckInitializedOutput is output from the CheckInitialized method
type CheckInitializedOutput struct {
    pragma.NoUnkeyedLiterals
}

Equal Operations

Equal Input and Output

Types for the Equal fast-path method.

// EqualInput is input to the Equal method
type EqualInput struct {
    pragma.NoUnkeyedLiterals

    MessageA protoreflect.Message
    MessageB protoreflect.Message
}

// EqualOutput is output from the Equal method
type EqualOutput struct {
    pragma.NoUnkeyedLiterals

    Equal bool
}

Legacy Extension Support

// ExtensionRangeV1 is the legacy extension range type
type ExtensionRangeV1 struct {
    Start, End int32 // both inclusive
}

Generated Code Support

Version Enforcement

Ensure compatibility between generated code and the runtime library.

// EnforceVersion is used by code generated by protoc-gen-go to statically
// enforce minimum and maximum versions of this package
// A compilation failure implies either that:
//   - the runtime package is too old and needs to be updated OR
//   - the generated code is too old and needs to be regenerated
type EnforceVersion uint

const (
    // MaxVersion is the maximum supported version for generated .pb.go files
    // It is always the current version of the module
    MaxVersion = version.Minor

    // GenVersion is the runtime version required by generated .pb.go files
    // This is incremented when generated code relies on new functionality
    // in the runtime
    GenVersion = 20

    // MinVersion is the minimum supported version for generated .pb.go files
    // This is incremented when the runtime drops support for old code
    MinVersion = 0
)

Usage in generated code:

const (
    // Verify that this generated code is sufficiently up-to-date
    _ = protoimpl.EnforceVersion(genVersion - protoimpl.MinVersion)
    // Verify that runtime/protoimpl is sufficiently up-to-date
    _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - genVersion)
)

Message Implementation Types

Types embedded in or used by generated messages.

// MessageInfo provides infrastructure for generated message types
type MessageInfo impl.MessageInfo

// MessageState is the state for message implementations
type MessageState impl.MessageState

// EnumInfo provides infrastructure for generated enum types
type EnumInfo impl.EnumInfo

// ExtensionInfo provides infrastructure for extension fields
type ExtensionInfo impl.ExtensionInfo

// ExtensionFields stores extension fields
type ExtensionFields impl.ExtensionFields

// ExtensionFieldV1 is the legacy extension field type
type ExtensionFieldV1 impl.ExtensionField

// UnknownFields stores unknown fields
type UnknownFields impl.UnknownFields

// WeakFields stores weak fields (deprecated)
type WeakFields impl.WeakFields

// SizeCache caches the encoded size of a message
type SizeCache impl.SizeCache

// Pointer is a pointer to a message
type Pointer impl.Pointer

// LazyUnmarshalInfo stores lazy unmarshal information
type LazyUnmarshalInfo *protolazy.XXX_lazyUnmarshalInfo

Descriptor and Type Builders

Types used by generated code in init functions.

// DescBuilder builds descriptors
type DescBuilder filedesc.Builder

// TypeBuilder builds types
type TypeBuilder filetype.Builder

Runtime Exports

Exported functionality for generated code.

// X contains exported functionality for generated code
var X impl.Export

Unsafe Support

// UnsafeEnabled specifies whether package unsafe can be used
const UnsafeEnabled = impl.UnsafeEnabled

Race Detection Support

// RaceDetectHookData provides hooks for race detection
type RaceDetectHookData impl.RaceDetectHookData

Lazy Decoding Control

Lazy Decoding Disable

Control lazy unmarshaling of opaque messages at runtime.

// Disable disables lazy unmarshaling of opaque messages
//
// Messages which are still on the OPEN or HYBRID API level are never lazily
// unmarshalled.
//
// Fields must be annotated with [lazy = true] in their .proto file to become
// eligible for lazy unmarshaling.
//
// Returns a function that can be called to re-enable lazy decoding.
func Disable() (reenable func())

The following logic determines whether lazy decoding is enabled:

  1. Lazy decoding is enabled by default, unless the environment variable GOPROTODEBUG=nolazy is set.
  2. If still on, calling protolazy.Disable() turns off lazy decoding.
  3. If still on, proto.UnmarshalOptions's NoLazyDecoding turns off lazy decoding for that Unmarshal operation only.

Usage example:

import "google.golang.org/protobuf/runtime/protolazy"

// Temporarily disable lazy decoding
reenable := protolazy.Disable()
defer reenable()

// Unmarshal operations here will not use lazy decoding
data, _ := proto.Marshal(msg)
msg2 := &pb.MyMessage{}
proto.Unmarshal(data, msg2)

Implementation Notes

The runtime packages are internal implementation details of the protobuf-go module. They provide:

  • Fast-path optimizations: Optional method implementations that bypass reflection for common operations
  • Version compatibility: Compile-time checks ensuring generated code and runtime are compatible
  • Message infrastructure: Types and interfaces embedded in generated messages
  • Extension support: Infrastructure for extension fields in proto2
  • Lazy decoding: Control over lazy unmarshaling for performance optimization

These packages should only be used by code generated by protoc-gen-go. Direct usage in user code is not supported and may break without notice.