Skip to content

parser pulls in protodesc/descriptorpb, costing ~3 MB of binary size for a feature most callers don't use #195

Closed

Description

@mvdan

Importing github.com/protocolbuffers/txtpbfmt/parser adds about 3.9 MB to a Go binary, of which roughly 3 MB is google.golang.org/protobuf/reflect/protodesc and types/descriptorpb.

The chain is parser → impl → txtpbfmt/descriptor → protodesc + descriptorpb + protoregistry + proto. The only use is in impl.ParseWithMetaCommentConfig:

if c.SortFieldsByFieldNumber {
    if c.ProtoDescriptor == "" { ... }
    loader, err := descriptor.NewLoader(c.ProtoDescriptor)
    ...
}

The guard is a runtime condition, so the linker can't prove it's unreachable and keeps the entire descriptor runtime. Callers who never set SortFieldsByFieldNumber - which I'd guess is most of them, since the flag needs a .desc file on disk - pay the full cost.

Measured with go1.26.0 on linux/amd64, each binary built from a main importing only the package named:

binary size
hello world 2,405,246
+ txtpbfmt/parser 6,307,225
+ txtpbfmt/parser, with the descriptor import removed from impl 3,237,979
+ protobuf/reflect/protoreflect only 2,563,379

So severing impl → descriptor saves ~3.07 MB. protoreflect itself is only ~158 KB and stays; the expense is entirely protodesc/descriptorpb, which are confined to the 83 lines of descriptor/descriptor.go.

Concretely: CUE uses parser.ParseWithConfig and ast for its textproto support (cuelang.org/go/encoding/protobuf/textproto). Stubbing that out drops the cue binary from 38,747,949 to 35,702,773 bytes — 3.05 MB, 7.9% of the whole tool — and it's all descriptor machinery we never reach.

One shape that would fix it, keeping the CLI feature intact: have config.Config carry an already-resolved protoreflect.MessageDescriptor instead of a ProtoDescriptor file path, and move the file loading into cmd/txtpbfmt. impl already threads protoreflect.MessageDescriptor through parse/parseMessage/parseFieldValue, so it keeps its cheap protoreflect import and loses the expensive ones, and the library stops doing file I/O on the caller's behalf. A registration hook in the descriptor package would also work if you'd rather not change Config.

This is the same shape as #70 — the heavy dependency belongs in the binary that needs it, not in the library. Happy to send a PR if you'd like, once you've picked an approach.

Activity

  1. mvdan commented on Sep 9, 2026

    @mvdan
    ContributorAuthor

    cc @stapelberg as you kindly assisted last time on #70 :) I'm not sure who is the best person to route this to.

  2. kssilveira commented on Sep 10, 2026

    @kssilveira
    Member
  3. mvdan commented on Sep 11, 2026

    @mvdan
    ContributorAuthor

    @kssilveira thanks very much for the quick response! Here's a small PR: #196

  4. added a commit that references this issue on Sep 16, 2026
    de69cb9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions