Friday, September 25, 2020

JsonSrcGen + CoreRT = Pure Magic

JsonSrcGen + CoreRT = Pure Magic

In my previous post I talked about how using a Source Generator allows us to make a reflection free Json generator. This has serious advantages when it comes deployment size and startup time.

These advantages become even bigger when paired with an AOT or Ahead Of Time compiler like CoreRT. CoreRT can produce impressively small binaries but cannot do Reflection.Emit and has only limited support for reflection. But seeing as JsonSrcGen doesn't do any reflection the two are a match made in heaven.

For me this produces a binary that is only 2.1 MB and will startup and serialise a simple Json class in only 5 ms.

In order to see just how good this is here a comparison to what you can do with the officially supported .NET Json and self contained publishing options. Which included the new Trimming and Ready To Run options.

How to use JsonSrcGen with CoreRT

1. Create a new .NET 5 console project

dotnet new console

2. Add the CoreRT Package Source

CoreRT requires a custom package source to be added to your nuget.config. I didn't have one so added one using: 

dotnet new nuget

Then add the dotnet-core Package Source to your nuget config so it looks as follows:

<?xml version="1.0" encoding="utf-8"?>
    <clear />
    <add key="dotnet-core" value="" />
    <add key="nuget" value="" />

3. Add the CoreRT and JsonSrcGen nuget packages

dotnet add package JsonSrcGen

dotnet add package Microsoft.DotNet.ILCompiler

4. Add the following lines to you .csproj file


The first line is required because Source Generators are currently in preview, this will not be required once they are released.
The Second line tells CoreRT that we do not need reflection, this is a significant saving in binary size.
The final line tells CoreRT that we do not need localisation support.

5. Install prerequisites
What you need here depends on your platform, but for me on linux I needed the following:

sudo apt-get install clang zlib1g-dev libkrb5-dev libtinfo5

6. Then build the project to get your output

dotnet publish -r linux-x64 -c Release

The -r linux-x64 tells CoreRT to build for linux 64 bit, if you are on a different platform you will need to change this.

7. If you are on linux use the strip command to get rid of debug information
This step is only required on linux, because the debug information ends up in the binary on linux.

strip bin/Release/net5.0/linux-x64/publish/CoreRTSample

Please checkout the JsonSrcGen project on github:

You can find a JsonSrcGen CoreRT sample project in our samples folder


Chris Sienkiewicz said...

This is great!

One of our foundational design goals for the feature was to improve 'AOTness' for projects that currently relied on reflection. Great to see it being used in conjuction with CoreRT for exactly that :)

- @chsienki

Nicolas Musset said...

I like the debig typo since for sure debug information are big. I'm adopting it now, too late.