Scott Hanselman

C sharp or B flat? Experiments in self-contained native executables in .NET

June 29, 2021 Comment on this post [4] Posted in Open Source
Sponsored By

One of the best parts of the .NET ecosystem is the excitement around experimentation. Someone is always taking .NET to the next level, trying new things, pushing the envelope.

Michal Strehovsky has an interesting experiment on his GitHub called "bflat." This is not a product, it's a playground.

bflat is a concoction of Roslyn - the "official" C# compiler that produces .NET executables - and NativeAOT (née CoreRT) - the experimental ahead of time compiler for .NET based on CoreCLR's crossgen2. Thanks to this, you get access to the latest C# features using the high performance CoreCLR GC and native code generator (RyuJIT).

bflat merges the two components together into a single ahead of time crosscompiler and runtime for C#.

I find this characterization funny:

bflat is to dotnet as VS Code is to VS.

Michal is basically stripping .NET down to the bare minimum and combining the official compiler and and the experimental AOT (Ahead of Time) compiler to make single small EXEs that are totally self-contained.

Michal says you can get involved if you like!

If you think bflat is useful, you can leave me a tip in my tip jar and include your GitHub user name in a note so that I can give you access to a private repo when I'm ready.

Hello World today is about 2 megs. He says it's because:

By default, bflat produces executables that are between 2 MB and 3 MB in size, even for the simplest apps. There are multiple reasons for this:

  • bflat includes stack trace data about all compiled methods so that it can print pretty exception stack traces
  • even the simplest apps might end up calling into reflection (to e.g. get the name of the OutOfMemoryException class), globalization, etc.
  • method bodies are aligned at 16-byte boundaries to optimize CPU cache line utilization
  • (Doesn't apply to Windows) DWARF debug information is included in the executable

So when I ran bflat build, here was my output.

2.8 meg hello world

But when I run

bflat.exe build --no-reflection --no-stacktrace-data --no-globalization --no-exception-messages .\hello.cs

I end up with a 750kb file!

750kb Hello World

Sure, it's not C code because it'll never be C code. You get access to a LOT MORE with C#.

This could be a useful system for creating tiny apps in C# for Linux or Windows command line administration. It also showcases how the open pieces of .NET can be plugged together differently to achieve interesting results.

I'm sure there's lot of AOT limitations around Reflection, Attributes, and more, but this is still a very cool experiment, go check it out at https://github.com/MichalStrehovsky/bflat!


Sponsor: Pluralsight helps teams build better tech skills through expert-led, hands-on practice and clear development paths. For a limited time, get 50% off your first month and start building stronger skills.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
July 03, 2021 4:58
I already generate single file .NET Core executables.

Can someone please explain what this is offering that I can't already do?

Thanks!

July 03, 2021 20:41
@rich File size is extremely small compared to single file executables in .NET
July 04, 2021 19:15
Nice piece of information dear.
July 07, 2021 13:15
Get the cure to low sperm count, penis enlargement, weak erection and premature ejaculation problem from Dr Okoye, my wife left me because i was not able to perform on bed, and because of that we were childless, he sent his product (herbal medicine) to me which i used and it is a permanent cure to weak erection and premature ejaculation my new wife is two month pregnant now, so i give all thanks to Dr Okoye for making me a man again, i was cured with his herbal product. Dr Okoye contact drokoyesnaturals@gmail.com or whatsapp him +2348109476757

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.