Show HN: Zero C#, Power of C# without any GC
A unique project has emerged in the C# programming community, offering a way to compile C# code into standalone native programs without the need for a .NET runtime or garbage collector. This innovative approach, known as bflat, maintains C#'s familiar syntax, type checking, and generics, while offering a more efficient memory management system.
The bflat compiler produces memory from arenas, which are freed all at once at the end of a program's lifetime. This eliminates the need for traditional garbage collection and new keyword handling. Instead, memory is managed using arenas, which are allocated once and released together when no longer needed.
The library provided by bflat includes strings, collections, input, and output functionality. These components are designed to work seamlessly with the arena-based memory management system. Each program compiles under updated memory safety rules, ensuring safe pointer creation and explicit marking of reading through pointers.
bflat supports various platforms, including Linux, macOS, Windows, WebAssembly, and DOS on a 386. Each target receives its own built and tested hello-world sample, ensuring consistent behavior across different environments. The project's test suite maintains the same output requirements, regardless of the target platform.
One key aspect of bflat is its ability to run native programs without the need for a container. For example, on macOS, the compiler builds and runs the program directly in the system's C library. Similarly, on Windows, the program is built using the system's C library, and tested under Wine for compatibility.
The bflat project has even expanded to support WebAssembly (WASM) and the WebAssembly System Interface (WASI). By using NativeAOT-LLVM, bflat can generate WASI modules that run under wasmtime, a popular WASM runtime.
To accommodate older systems, bflat has implemented a solution using the HX DOS extender, allowing the program to run on a 386 architecture without a runtime. The program's bitcode can be compiled for the 386 and executed using Bochs's i386 model.
Although the absence of a runtime introduces some differences in behavior, bflat provides comprehensive documentation for each case. The project offers guidance on how to handle memory management, input/output operations, and other aspects that differ from traditional C# programming.
For those interested in diving into bflat, the project offers a set of resources to help them get started. The four-page introduction takes newcomers from zero to an interactive program, covering all necessary concepts. Additionally, the reference section lists every type in the bflat library, making it easy for developers to explore the available functionality.
Rewritten by urgent.news; may contain errors — check the original.