# .NET 8, Native AOT, and what a container actually costs

> The AOT numbers in the announcement are large enough to be suspicious, so I went back to the experiment I set up two years ago and ran it on a release that can finally produce a native executable.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2023-12-16
- Tags: C#, .NET, Docker, Containers, Performance
- URL: https://deyanpeev.com/posts/dotnet-eight-native-aot-and-the-container

.NET 8 arrived on the fourteenth of November as a long-term support release, and
the announcement leads with a number. Publishing an ASP.NET Core application as
Native AOT is said to cut startup time by around 80 per cent and application size
by around 87 per cent.

Numbers like that are the kind I've argued shouldn't go into a capacity plan
without your own measurement. So this is a note about what the feature is, what it
costs, and how I would check it, with the same sort of experiment as in the
[.NET 5 container memory post](https://deyanpeev.com/posts/dotnet-five-linux-containers-memory)
from two years ago.

# No JIT, no IL

Normally a .NET application ships as IL and the runtime JIT-compiles it as it
runs. That's why a process is slow for its first few seconds and fast afterwards,
and why the container needs a runtime in it.

Native AOT compiles ahead of time to a native executable. There is no JIT and no
IL, and a much smaller runtime gets linked in. What you deploy is a binary.

.NET 7 had this for console applications. .NET 8 adds ASP.NET Core support,
focused on minimal APIs, so this is the first time it's relevant to the kind of
service I write.

```xml
<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <PublishAot>true</PublishAot>
    <InvariantGlobalization>true</InvariantGlobalization>
  </PropertyGroup>
</Project>
```

The container that results doesn't need a .NET base image at all:

```dockerfile
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
RUN apt-get update && apt-get install -y clang zlib1g-dev
WORKDIR /src
COPY . .
RUN dotnet publish -c Release -r linux-x64 -o /out

FROM mcr.microsoft.com/dotnet/runtime-deps:8.0 AS final
WORKDIR /app
COPY --from=build /out/Probe ./
USER 1000:1000
ENTRYPOINT ["./Probe"]
```

`runtime-deps` carries native dependencies and no runtime. The AOT toolchain needs
a native linker at build time, hence the `clang`. That is a real change to the
build image, and I'd want to know about it before a CI pipeline finds it for me.

# What it costs

It isn't the default because a great deal of ordinary .NET code can't survive it.

Runtime code generation is gone, so anything that emits IL at run time has nothing
to emit into. Unbounded reflection is gone too, becuase the trimmer has to decide
what to keep by static analysis, and a type only reached through a string name
looks unreachable. Most of the serialisation, dependency injection and ORM
machinery people rely on was built on those two capabilities.

So the supported scenario is minimal APIs with source-generated JSON. Controllers
with reflective model binding are not it. Source generators are the compensating
mechanism everywhere you look: work that used to happen by reflection at run time
moves to code generated at compile time.

The failure mode to know about is that trimming warnings are only warnings. An
application can publish, start, serve most requests, and then throw on the one
path that reflects over a type the trimmer removed. I think trimming and AOT
analysis warnings should be errors in any build that's meant to ship this way.

# How I would check the numbers

I'd use the same discipline as before, since the quantities being quoted are
different quantities.

Startup time is the claim most likely to hold, because its mechanism is the
clearest one. There is no JIT, so there's no warm-up. Measure it as time from
container start to first successful response, not from process start, and
measure it cold, before the image is in page cache.

Size needs care about which size. Published output, uncompressed image, compressed
transfer size and the incremental layer size are four different numbers, and an
87 per cent reduction in one of them isn't a reduction in the others. The one
that matters for deployment is usually the compressed layers you didn't already
have on the node.

Memory is the one I would be most curious about and least willing to predict. No
JIT means no JIT data structures, but the garbage collector is still there and the
application's own allocation behaviour is unchanged. Peak under load, sampled the
same way on both sides, is the comparison I'd want.

And it has to be the same application on both sides. A minimal API rewritten to
be AOT-compatible is not the controller-based application it replaced, so some of
any difference comes from the rewrite and not from the compilation model.

# Services that start often

Startup time is the property with a business consequence, and it matters most
where processes start often. A service that scales out under bursty traffic, a
container you scale to zero, a short-lived job.

For a long-running service that starts twice a week, an 80 per cent improvement in
startup barely registers. There you pay for the trade (constrained reflection, a
changed build image, a rewrite towards minimal APIs) and the benefit doesn't
arrive.

So what I would ask of this release is whether the thing it makes faster is
something I do often. For services that do start often, .NET 8 is the first
release in this line where ASP.NET Core is supported at all.
