Experimental. The longer story, with everything that went wrong along the way, is in
notes/findings.md.
No runtime
A zero app is compiled by the official .NET Native AOT compiler, but against a tiny replacement for the base class library. That replacement defines just enough types for C# to compile and for the AOT compiler to produce an exe. With no runtime library to link, the exe contains your code and almost nothing else.
What you have:
- primitives, structs and
ref structs, and string literals, which are frozen into the image; - a real
Span<T>/ReadOnlySpan<T>, with slicing,foreach,"..."u8literals and collection expressions; - generics, including
static abstractinterface members andallows ref struct; - any OS function you declare with
[DllImport].
What you don’t have:
- allocation: there is no
newfor classes or arrays, and nothing to free memory with. The caller supplies buffers; - exceptions: every operation reports success with a
boolinstead. A bounds-check failure ends the process with a distinctive exit code; - run-time type information: no reflection, and no casts or
is/ason reference types; stackalloc: use an inline array struct instead (see Getting started).
Why no reflection is fine: generics resolved at compile time
protobuf-net builds serializers at run time, from attributes it reads by reflection. Here the
source generator does that work at compile time, and the library’s entry points are generic over
interfaces with static abstract members:
public static bool TrySerialize<T>(in T value, Span<byte> destination, out int bytesWritten)
where T : IProtoMessage<T>, allows ref struct
Every call binds to the contract’s own static methods for that T, so there is no virtual
dispatch, no casting and no lookup. allows ref struct lets a contract or view that holds spans
be the T.
One library binary, two worlds
The minimal base library is an assembly called System.Runtime, with the same identity as
the real one: name, public key token and a version no higher than any host’s. It exposes a strict
subset of the real System.Runtime, with the same signatures.
System.Runtime is what ordinary .NET libraries reference anyway, a contract that the real
runtime forwards to its core library. So ProtoBuf.Zero.dll, compiled against the subset:
- in a zero app, binds to the minimal
System.Runtimesupplied by the SDK; - in an ordinary .NET app, binds to the real
System.Runtime, and just works.
It is the same file in both, and the repository’s build checks that the two produce identical
bytes. It also means zero libraries need no shared “micro-BCL” of their own to agree on: the
shared surface is whichever part of System.Runtime they use. A library that wants to stay
usable from zero apps must use only that subset, and must allocate and throw nothing.
The cross-checks
Every build of the repository:
- writes a message from a zero exe, and checks protobuf-net produces identical bytes for the same values;
- reads protobuf-net’s output back in the zero exe, including fields it must skip;
- rejects truncated input and undersized buffers;
- runs the same library binary in an ordinary .NET app, and checks it agrees with both;
- packs the NuGet packages, and builds the getting-started sample from those packages alone.