What if C# were like Go? Open source bflat has ‘Go-inspired tooling’, now targets bare metal

Bare metal
Bare metal

Updated Developer Michal Strehovský has released bflat 7.0.1, with support for C# applications that run on bare metal – taking native ahead of time (AOT) compilation further than is possible with Microsoft’s official Native AOT in .NET 7.

Strehovský is a software engineer at Microsoft working full-time on the .NET runtime, but open source bflat is a personal project.

The bflat site explains that this is a native compiler for C# based on the .NET 7 SDK but “reimagined as native-first and stripped to the bare minimum needed.” The compiler targets Linux, Windows and Android on x64 or arm64, or on x64 only, bare metal booted with UEFI (Unified Extensible Firmware). Output is either native code or .NET IL (Intermediate Language). It has two standard libraries, one that is compatible with .NET and another, called Zero, that “doesn’t even have a GC [Garbage Collector]”.  A default bflat executable is quite large (from 12 MB on Linux) but can be greatly reduced with arguments that remove stack trace data, reflection and other options.

“With all options turned on, one can comfortably fit useful programs under 1 MB,” the documentation states.

The project is now open source on GitHub under the MIT license.

Why might a C# developer have Go envy? Microsoft itself publicised the case for Go in 2018. “In the cloud, compiling down to a static binary is absolutely the number one advantage,” said developer advocate Brian Ketelsen (then at Microsoft, now at AWS). “With a static binary that you build in Go, you could have a Docker file that’s 10, 11, 12 megabytes instead of bringing in the entire Node.js ecosystem, or Python, or Java, where you’ve got these hundreds of megabyte-sized Docker files. So, shipping that tiny binary is amazing.”

Another scenario where small, dependency free executables are a big advantage are in serverless environments like AWS Lambda or Azure Functions, where fast start-up is critical.

The bflat compiler gives C# developers new choices, with the option to use a stripped-down but still highly compatible .NET library, or the “Zero” option that lacks GC or even exception support, but enables tiny executables.

As an unofficial project it is unlikely that developers will want to use bflat in production. There are also some limitations, one of which is lack of support for macOS. “Apple’s platforms are difficult to crosscompile for,” said Strehovský on Hacker News earlier today. “Then there’s a philosophical issue that I have where Apple essentially would like everyone to pay up if they want to distribute software that targets their platforms.”

That said, bflat shows off the capabilities and advantages of Native AOT to a greater extent than the official Microsoft version, and demonstrates how far C# has come since its early days as a Windows-only language for the .NET Framework.

Update on 4 January:

We got in touch with Strehovský for some background to the project.

How does it relate to his work at Microsoft?

“bflat is just a spare time project,” he told us. “My work at Microsoft is around the runtime – I’m not involved with the tooling (`dotnet` or the rest of the SDK). I made it to scratch my personal itch.”

Why did he create bflat?

“C# currently spans a broad spectrum – you can use it for scripting things like Jupyter notebooks, C# interactive, or JIT-things like hot reload, or AOT things like the newly added (in .NET 7) “PublishAot”. You can create self-contained applications that include the runtime. Or you can create an application that expects .NET runtime to be installed. You can use C# or F# or VB. The tooling around .NET is built to support all of these scenarios. But because of that, the tooling is also pretty complex. bflat is a response to “what if all I want is an ahead of time compiler for C# that works the same as any other ahead of time compiled language?” Don’t get me wrong, I love MSBuild and the ecosystem around it. But sometimes I just want to build something with a simple `Makefile`. bflat takes a more Unix-y approach of a single tool that does one thing and focuses on doing it well,” he told us.

How does he see the project evolving?

He has no big plans, he said. “it is just a remix of .NET 7 – the bflat specific part is pretty small. it is “cheap” (time-wise) enough that I could see myself supporting this longer term, upgrading the internals to .NET 8/9/whatever as they come. but it all depends on whether others will find it useful.”