OmniBuf
for C# & .NET.
Managed .NET 8 library on NuGet paired with a Buf code generator. Serialize attributed records, classes, and structs, pass in Google Protobuf messages, or generate partial classes and lazy packet views for in-place field reads and updates.
$ dotnet add package OmniBufThree ways to use OmniBuf in C#.
All three produce identical wire bytes across .NET 8 and newer runtimes.
Attribute-annotated C# records and classes
[OmniBufMessage] and [OmniBufField] work on mutable classes, value structs, and immutable C# positional records. OmniBufSerializer compiles expression-tree encoders and decoders once per type.
using OmniBuf;
[OmniBufMessage(TypeName = "SensorSample")]
public sealed record SensorSample(
[property: OmniBufField(1, Name = "sensor_id")] string SensorId,
[property: OmniBufField(2, Name = "celsius")] double Celsius,
[property: OmniBufField(3, Name = "seq")] ulong Seq);
var sample = new SensorSample("rack-42-inlet", 21.75, 1042UL);
byte[] pkt = OmniBufSerializer.Marshal(sample);
SensorSample outSample = OmniBufSerializer.Unmarshal<SensorSample>(pkt);Standard Google.Protobuf.IMessage types
ASP.NET Core and gRPC projects already generating Google.Protobuf classes pass any IMessage directly to OmniBufProtobuf.Marshal and OmniBufProtobuf.Unmarshal<T>.
using Omnibuf.Bench;
using OmniBuf;
byte[] pkt = OmniBufProtobuf.Marshal(peer);
PeerUpdate copy = OmniBufProtobuf.Unmarshal<PeerUpdate>(pkt);Generated partial classes with protoc-gen-omnibuf-cs
Adding protoc-gen-omnibuf-cs to buf.gen.yaml attaches direct Span<byte> encoders and decoders onto generated C# partial classes and emits Opaque* views. Single-field sequence updates complete in ~134 ns.
version: v2
clean: true
plugins:
- protoc_builtin: csharp
out: src/Generated
- local: protoc-gen-omnibuf-cs
out: src/Generated$ buf lint
$ buf generateusing Omnibuf.Bench;
using OmniBuf;
byte[] pkt = peer.MarshalOmniBuf();
var view = new OpaquePeerUpdate(pkt);
string city = view.GetLocationCity();
view.Sequence = 9_876_543_211UL;
byte[] updated = view.MarshalOmniBuf();C# & .NET 8 benchmarks.
11-field PeerUpdate workload on .NET 8.0.29 (RyuJIT) across OmniBuf, Google.Protobuf, MessagePack-CSharp, and System.Text.Json. Shorter bars = Faster.
Serializing PeerUpdate into a fresh byte[] on .NET 8 RyuJIT.
Decoding ReadOnlySpan<byte> into a fresh PeerUpdate instance and reading all 11 fields.
Reading a nested city string via OpaquePeerUpdate vs full message decode.
Checking a nested city string and splicing an updated sequence counter via OpaquePeerUpdate vs full decode and re-encode.
Serializing a 39 KiB packet whose >16 KiB nested sub-messages trigger 2B-to-4B header expansion.
Growing a nested string field past 16 KiB ahead of 32 trailing Neighbor entries via Opaque splicing vs full decode and re-encode.
Benchmark Environment
Measured on .NET 8.0.29 (Release build with Dynamic PGO warmup) on an Apple M1 Pro Mac. Every benchmark allocates a fresh byte[] on encode and reads all 11 fields on decode, tracking per-operation heap bytes with GC.GetAllocatedBytesForCurrentThread.
Benchmark source lives in src/OmniBuf.Benchmarks/Program.cs, with recorded output in testdata/benchmarks.json.
C# & .NET questions.
Why does protoc-gen-omnibuf-cs pair with protoc_builtin: csharp?
Google.Protobuf generates sealed partial class message types in C#. protoc-gen-omnibuf-cs emits companion partial class files adding MarshalOmniBuf() and ParseOmniBuf(ReadOnlySpan<byte>) directly onto your existing message types alongside standalone Opaque* views.
Why is [OmniBufField] nearly as fast as generated code on .NET 8?
OmniBufSerializer compiles typed System.Linq.Expressions delegates once per type, avoiding per-field reflection and primitive boxing. Under RyuJIT Dynamic PGO, [OmniBufField] decodes within 20% of generated partial methods while still edging out Google.Protobuf.
How does OmniBufBlind inspect schema-less packets?
OmniBufBlind.DecodeBlind(pkt) reads 4-bit wire tags into a dictionary with field_1, field_2, ... keys, or maps Field #0 names when SelfDescribing = true was set on encode.
using OmniBuf;
byte[] pkt = OmniBufSerializer.Marshal(sample, new MarshalOptions { SelfDescribing = true });
string json = OmniBufBlind.DecodeBlindJson(pkt);