Liking cljdoc? Tell your friends :D

CI License Language Dependencies

Release v0.1.20 — every binding ships this one version; VERSION is the source of truth, scripts/sync-versions.sh propagates it.

pbwire — a dynamic Protobuf runtime for C, no code generation

pbwire is a dependency-free Protobuf runtime written in C. It loads a schema at runtime — a hand-built descriptor tree or a standard FileDescriptorSet — then builds, reads, encodes and decodes messages dynamically through reflection. No protoc step, no generated message code, no libprotobuf in the process. It speaks proto2, proto3 and edition-2023 descriptor sets, the JSON mapping and the text format, and drives twenty-three host-language bindings from that one core.

The point is not to re-implement Protobuf encoding. It is to let every language with a C ABI share one dynamic Protobuf schema and one message-processing core instead of re-implementing Protobuf per host language.

Help wanted: Java 136 B encode (~0.26x vs protobuf-java). The pure-Java codec is tuned to what looks like a structural floor, and six measured attempts all failed. If you know HotSpot, see the call for help — a fix that keeps the binding dynamic, or a proof that the floor is real.

uint8_t *buf;
size_t len;
pbw_encode(msg, &buf, &len);   /* 0a 03 61 62 63 10 ff ff ff ff ff ff ff ff ff 01 */
pbw_free(buf);

Documentation

DocumentContents
C API guidethe tutorial, hand-built schema, FileDescriptorSet load, pbw_ctx loop, ownership rules, streaming, unknown fields, JSON/text, ABI
Design and architecturehow it works, dynamic message semantics, the two allocation strategies, scope, limits, use cases, repository layout
Performance benchmarksthe performance document: the four standard tiers, every binding against its official runtime, and the cross-binding comparison
Performance attempts ledgerevery optimization tried against a still-below-baseline cell, its A/B number, and why it was kept or rejected
Benchmark fairnesswhat makes a row comparable: full-flow native in/out, matched freshness and depth, same-run calibration
Help wanted: Java 136 B encodethe one open perf gap: what was tried, why it looks structural, and how to help
Examples indexone runnable round trip per binding, with the exact command
Testing and conformancethe self-check, pinned reference bytes, difftest, the official conformance suite
FAQsyntax support, thread safety, unknown fields, untrusted input, .proto at runtime
Protobuf pain pointswhich official-library complaints pbwire solves, eases or leaves alone — the searchable index
Bindingsevery host binding, where its schema comes from, and the per-language pain point each one removes
include/pbwire.hthe whole public API with per-function contracts
AGENTS.md · CONTRIBUTING.md · llms.txtworking on this repo, commit rules, a one-page factual summary
ROADMAP.mdwhat is done, what is never, what is next

Why use pbwire

  • Schema at runtime. Load a FileDescriptorSet produced by protoc, buf or pbwire-protoc, and handle message types you did not compile in.
  • No runtime dependencies. C99, one public header, ~13k lines of C (about 7.5k of it the binary codec, the rest the JSON mapping and text format). make and make test need nothing but a compiler — no libprotobuf, no protoc, no JSON/C++ runtime underneath.
  • Dynamic messages and reflection. Create messages from a descriptor, get/set fields by number or name, walk nested structures, oneofs and maps.
  • Thin models, not fat codecs. One C core owns every wire byte; codegen emits only field definitions (names, numbers, types, presence) for the host language. Extra annotations, attributes or comments on the model never touch the codec, so the output stays readable, editable and fast to compile.
  • Byte-identical to the reference implementation. Encodings match protoc 36.2 for every scalar type, packed/unpacked repetition and nested messages; pinned bytes in the test suite keep parity from regressing silently.
  • Built for embedding. Stable C ABI, no globals, caller-confined schema/message objects, arena-backed pbw_ctx for per-thread hot loops, wire input validated at the boundary. bindings/embedded packages that as one pbwe_box: one arena (on the heap, or in caller memory via pbwe_open_mem/pbw_ctx_new_mem, which never allocates), caller-owned encode buffers (bare or length-prefixed frames), and both inputs (frames and the startup descriptor set) capped by pbw_limits; make embedded-bench measures it against nanopb, byte for byte and ns for ns.
  • One core, twenty-three host languages. C, C++, Python, Ruby, Java, Kotlin, Node, Objective-C, PHP, Dart, C#, Rust, Go, Erlang, Lua, Swift, Scala, Elixir, Zig, F#, Clojure, Julia and R all speak the same wire format, each with a measured answer to its runtime's own pain point — see what each binding solves.

At a glance

LanguageC99 (C++ consumers include the same header)
Wire formatGoogle Protobuf binary, proto2 + proto3 (edition-2023 descriptor sets load too)
Schema inputhand-built descriptors, or a standard FileDescriptorSet
Message handlingdynamic value tree with reflection; no generated codec code (optional thin models carry field definitions only)
Runtime dependenciesnone (optional importer pbwire-protoc needs libprotobuf, C++17)
Not implemented.proto text parsing in the core (deliberately never: build the set once with protoc/buf/pbwire-protoc); RPC transport and stubs (services load, are queryable, and resolve to descriptors with pbw_service_resolve for routing — the transport itself stays official gRPC/Twirp/plain HTTP); no CLI (inspect with protoc --decode/--encode)
Memory modelone allocation per decoded tree, or one reusable arena pbw_ctx per thread
Limitsnesting depth 100, field numbers up to 2^29-1
PlatformsLinux, macOS, Windows (MSVC through CMake/ctest); any C99 compiler; 32-bit (ILP32) and big-endian are CI-verified
LicenseApache-2.0

Requirements and build

A C99 compiler for the core. CMake plus a C++17 compiler and libprotobuf only for pbwire-protoc.

make          # build build/libpbwire.a
make test     # self-checks: core, threads, descriptor set, C structs, examples round trip
make shared   # libpbwire.so.2 / libpbwire.2.dylib, for FFI hosts that dlopen it
make format   # clang-format (>= 19); make format-check
make clean

CMake is the packaging path and the only build that knows about pbwire-protoc:

cmake -B build-cmake                        # core only, no protobuf needed
cmake -B build-cmake -DPBW_BUILD_PROTOC=ON   # core + .proto importer (needs libprotobuf)
cmake --build build-cmake
(cd build-cmake && ctest)                    # same checks as `make test`, plus test_importer

# installed targets: pbwire::pbwire and pbwire::protoc
cmake --install build-cmake --prefix /usr/local

# then, in a consumer:
# find_package(pbwire REQUIRED)
# target_link_libraries(app PRIVATE pbwire::pbwire)

Everything else is optional and outside the commit gate: make difftest, make conformance and the per-binding *-test / *-bench targets. See docs/testing.md for what they verify and when they are worth running; binding targets are listed in each binding's README and in BENCHMARKS.md.

Quick start

#include "pbwire.h"

pbw_schema *schema = pbw_schema_new();
pbw_descriptor *person = pbw_schema_add_message(schema, "Person");
pbw_message_add_field(person, "name", 1, PBW_TYPE_STRING, PBW_LABEL_OPTIONAL);
pbw_message_add_field(person, "id", 2, PBW_TYPE_INT32, PBW_LABEL_OPTIONAL);
pbw_schema_link(schema);

pbw_message *msg = pbw_message_new(person);
pbw_message_set(msg, 1, pbw_value_new_string("abc"));
pbw_message_set(msg, 2, pbw_value_new_int(-1));

uint8_t *buf;
size_t len;
pbw_encode(msg, &buf, &len);   /* 0a 03 61 62 63 10 ff ff ff ff ff ff ff ff ff 01 */

pbw_message *decoded;
pbw_decode(person, buf, len, &decoded);
const char *name = pbw_value_get_string(pbw_message_get(decoded, 1));

pbw_free(buf);
pbw_message_free(decoded);
pbw_message_free(msg);
pbw_schema_free(schema);

Real programs rarely build a schema by hand — they load a FileDescriptorSet at runtime and process types they were never compiled against. The walk-through for that, the arena-backed hot loop, streaming framing and the JSON/text entry points is docs/guide.md; a runnable tutorial schema (proto2 address book, loaded from addressbook.desc at runtime) is in examples/ and runs with make examples.

Performance

make bench verifies byte-for-byte equality with libprotobuf before timing anything, so a speed number can never come from a wrong encoder. On an Apple M4 Pro with -O2 and protoc 36.2, the in-process core through its generated C struct and compiled struct codec encodes at 1.34-2.25x the throughput of the generated C++ codec on every tier (132 B to 86 KB), and 6.8-11.4x the speed of C++ DynamicMessage on encode (2.9-3.8x on decode). Decode wins through the arena at 1.45-1.98x on ctx and 1.20-1.89x plain — the same batched profile as the reference parse on both sides now. The old tree-API encode row (0.39x-0.58x) retired with the fairness contract (docs/perf-fairness.md): headlines go through the shipped thin model now. Generated code still wins a few published cells: the JVM core decode (Kotlin 0.77x, Scala 0.32x — all three ride the same pure-Java runtime, and Clojure's core trails on both ops at 0.25x/0.62x), Node's shim (0.19x/0.14x of protobufjs), the managed materialization on C# shim decode at 74 KB (0.94x — inside the band), and the two xlarge-encode band cells (Lua 1.03x, ObjC 1.00x — each inside its run-to-run band, parked as noise). Everything else in the table is at or above the official runtime. make bench-docker runs the same harnesses in a Linux container and make bench-compare pairs the two runs row by row, so the numbers above hold off Apple silicon as well (BENCHMARKS.md).

Every host binding passes its native object in one call — the same task as filling generated code's struct — and that struct-passing row is the published comparison in the table below. The core times the same task through its own generated C struct (bench/gen/msg_struct.h); the three single-arm bindings (Objective-C, Erlang, Elixir) quote their native row. Since the 2026-11-24 core re-run both sides fill a native struct fresh per iteration, even the core encode row is the same new + set + encode calling method as its baseline.

The per-language tables — the same four payload tiers for every binding against its official runtime (Python, Go, Java, Kotlin, Node, Ruby, Objective-C, PHP, Dart, Rust, C#, Erlang, Swift, Scala, Elixir, F#, Clojure, Julia, Lua, Zig, R), plus the fastest third-party runtime wherever one meaningfully exists (PHP, same package two builds), the C struct codec, 1-vs-8-thread scaling, and the reason behind each gap — live in BENCHMARKS.md. The one-row summary is here; the detail is there.

LanguageHow it talks to the coreBest encode / decode vs officialBaseline
Cstatic link, struct codec4.17x (xlarge) / 2.03x (xlarge)protoc C++
C++same C runtime, same header4.17x (xlarge) / 2.03x (xlarge)¹protoc C++
PythonCPython extension2.75x (xlarge, shim encode) / 3.58x (xlarge, shim to_dict)google.protobuf (upb)
RubyC extension, linked5.03x (xlarge) / 6.11x (xlarge) shimgoogle-protobuf gem
Javapure-Java codec, no native code1.05x (xlarge) / 1.26x (xlarge) core (generated model; encode 288.1 vs 275.6 MB/s, just above parity)protobuf-java
KotlinJava runtime + DSL27.05x (xlarge) / 0.77x (xlarge) coreprotobuf-kotlin
NodeN-API addon0.19x (xlarge) / 0.14x (xlarge) shim vs protobufjsprotobufjs
Objective-CARC wrapper1.00x (xlarge, parity) / 1.19x (xlarge) tree (fresh build per iteration)protobuf-objc
PHPC extension2.94x (xlarge) / 2.81x (xlarge) shim vs C extprotobuf-php
Dartdart:ffi1.82x (xlarge) / 4.84x (xlarge) shimprotobuf-dart
Ruststatic link2.10x (xlarge) / 1.85x (xlarge) shim vs prostprost
C#P/Invoke1.39x (xlarge) / 0.94x (xlarge) shimGoogle.Protobuf
Gothin generated models, pure-Go engine, no cgo1.55x (xlarge) / 1.28x (xlarge)google.golang.org/protobuf
ErlangNIF2.95x (xlarge) / 1.59x (xlarge)gpb
LuaC module, tables + builder1.03x (xlarge, band) / 1.41x (xlarge) shimlua-protobuf
SwiftSwiftPM, static link3.24x (xlarge) / 2.54x (xlarge) shimswift-protobuf
ScalaJava runtime + DSL181.79x (xlarge) / 0.32x (xlarge) core (case class)ScalaPB
ElixirNIF (shared core)2.98x (xlarge) / 1.91x (xlarge)elixir-protobuf
Zigstatic link1.84x (xlarge) / 1.47x (xlarge) shimzig-protobuf
F#C# runtime + facade1.85x (xlarge) / 1.21x (xlarge) shimGoogle.Protobuf
ClojureJava runtime + interop0.25x (xlarge) / 0.62x (xlarge) core (mutable-message fast path; 0.05x encode at 136 B)protobuf-java
Juliadlopen + ccall21.40x (xlarge) / 1.09x (xlarge) shimProtoBuf.jl
RC extension, named lists19.28x (xlarge) / 57.00x (xlarge) shimRProtoBuf

¹ C++ has no separate bench; same engine as the C struct codec. Ratios quote the struct-passing (one-call) row at the xlarge tier (Apple M4 Pro, protoc 36.2, 2026-11-24 full re-run with the twelve campaign bindings re-pasted from 2026-11-26 records); the core and the three single-arm bindings (Objective-C, Erlang, Elixir) quote their native row; per-tier tables and absolute MB/s are in BENCHMARKS.md.

Header ratios quote the struct-passing row (native object in, one codec call — the same task as filling generated code's struct): what a user of the shipped path pays. The core and the single-arm bindings quote their native row.

Comparison with generated code, DynamicMessage and upb

Schema at runtimeGenerated codeRuntime dependencyDynamic messages + reflection
pbwireyesnonenone (C99)yes
protoc generated C++noyes (per-message codec, slow to compile, hard to read/edit)libprotobufvia DynamicMessage, 11-15x slower encode than pbwire ctx
DynamicMessage (C++)yesnolibprotobufyes
upbyesoptionalbundled C runtimeyes
nanopbnoyesnone (embedded C)no

Pick pbwire when the schema is only known at runtime, when one C core serves several host languages, or when libprotobuf must not be in the process. Pick generated code only when the schema never changes and you accept a codegen step: even then the one-call path wins or ties most published encode cells — the exceptions are the managed per-call builds (Clojure 0.26x on the fresh-build task), Lua's run-to-run tie (0.98x) and Node's shim (0.19x) — while the in-process pbw_ctx wins every tier of the core benchmark and the C struct codec wins every tier of its own.

Contributing

See CONTRIBUTING.md. Enable hooks once per clone:

./scripts/setup-git-hooks.sh

License

Apache-2.0.

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close