Show HN: Zero C#, Power of C# without any GC

Hacker News by 4 min read 43x views
Show HN: Zero C#, Power of C# without any GC

Share Post

A fork of bflat compiles C# direct to a small native program: no .NET runtime, no refuse collector, nothing to instal anywhere it runs.

You keep C#'s syntax, category checking and generics. Memory comes from arenas you liberated all at once, and a small archive gives you strings, collections, input and output on those terms. The identical origin runs on Linux, macOS and Windows, on WebAssembly, and on DOS on a 386.

// Read into a stack buffer: nothing allocated per answer. Span<char> buffer = stackalloc char[32]; Out.Write($"Q{i}: {a} + {b} = "); int dimension = In.ReadLine(buffer); // Kept until the end, so it goes into an arena. mistakes.Add(Str.Format(keep, $"{a} + {b} = {a + b}, not {value}"));

From the quiz the tutorial builds, stage by step.

The tongue stays, the runtime goes

The compiler motionless checks everything it checks in .NET. What's missing is what a runtime would do during your program runs.

You keep

  • C# 15Unions, expansion members and the new recollection safety rules.
  • Real genericsA specialized copy for all type, and fixed theoretical members.
  • Interfaces and virtual methodsDefault interface methods, casts, is and as.
  • Records, tuples, anonymous typesValue equality, with, and the generated ToString.
  • Lambdas and queriesWhere, Select and GroupBy, from a small archive alternatively of LINQ.
  • ThreadsParallel.For runs a iteration on all core.

You do without

  • Garbage collectorArenas liberated their recollection all at once, whenever you say.
  • .NET runtimeHello earth is 11 KB on Windows and 40 KB on Linux, alongside nothing to install.
  • Catching exceptionsthrow ends the program. Return errors as unions.
  • Reflection and asyncA category can be compared, but it has no name.

One program, five machines

The hello-world sample, unchanged, built for all target. Each build is checked by the identical test suite, which requires the identical output everywhere.

$ ./build.sh Minimal $ ./run.sh Minimal Hello from zero-mode! $ wc -c bin/Minimal 39848 bin/Minimal

Linux, x64 or arm64

A native program that needs lone the C library. On macOS it builds and runs in a container.

$ ./build.sh --mac Minimal $ bin/mac/Minimal Hello from zero-mode! $ wc -c bin/mac/Minimal 64728 bin/mac/Minimal

macOS on Apple Silicon

A native Mac program: bflat compiles it, the Mac's own collision links it. No receptacle to run it.

$ ./build.sh --win Minimal $ ./run.sh --win Minimal Hello from zero-mode! $ wc -c bin/win/Minimal.exe 10752 bin/win/Minimal.exe

Windows x64

A console program on Windows' own C library, built from Linux or macOS. Tested under Wine.

$ ./build.sh --wasm Minimal $ ./run.sh --wasm Minimal Hello from zero-mode! $ wc -c bin/wasm/Minimal.wasm 15833 bin/wasm/Minimal.wasm

WebAssembly

A WASI module, through NativeAOT-LLVM. Runs under wasmtime.

C:\>D: D:\>PROGRAM Hello from zero-mode! D:\>DIR PROGRAM.EXE PROGRAM EXE 16,384 D:\>

DOS on a 386

The identical bitcode compiled for a 386, run by the HX DOS extender. Tested on Bochs's i386 model.

What's different, and what you get

Without a runtime, a few things activity differently. Each has a leaf that explains why and what to do instead.

  • Memory is yours to free

    There is no refuse collector, so new is never freed. Arenas hand out recollection and liberated it all at once, one per lifetime.

    Memory without a refuse collector
  • Text without throwaway strings

    Out.Line($"...") formats direct into the output buffer, and In.ReadLine says into a span. Strings that must final go into an arena.

    Strings without a refuse collector
  • Collections that live in arenas

    List, Dict, and their versions for references and cord keys, all handles into an arena.

    Choose a collection
  • C# 15, alongside the safety rules on

    Every program compiles under the updated recollection safety rules: creating a pointer is safe, study through one is marked and explained.

    The recollection safety rules

Where to start

If you're new to it, these four pages obtain you from nothing to an interactive program.

  1. Install

    The bflat fork and the prerequisites.

  2. Your archetypal program

    Build it, run it, see how small it is.

  3. Run it on DOS

    The identical program under FreeDOS, in an emulator.

  4. The tutorial

    A quiz: output, input, bad input, arenas.

Looking for item specific? The task guides answer one inquiry each, and the reference lists all category in the library.

Other Article Hacker News
↑
Close Right Ads
Close Left Ads