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.