413 points · by susam · 3y ago · 210 comments
Michal Strehovský used the framework currently known as .NET (as opposed to the Framework formerly known as .NET per OP) to create a snake game in under 8KB with no .NET [nor .NET Framework] runtime dependency. (Medium sucks and Twitter isn't useful right now, but I believe ~"hello world" supported Windows 3.11.)
https://medium.com/@MStrehovsky/building-a-self-contained-ga... [https://web.archive.org/web/20200103110836/https://medium.co... | https://archive.ph/b6qXE]
https://news.ycombinator.com/item?id=22010159 (2020)
https://news.ycombinator.com/item?id=22104734
You can see the latest work (and sponsorship!) at https://flattened.net: "bflat - C# as you know it but with Go-inspired tooling".
https://learn.microsoft.com/en-us/dotnet/core/extensions/cha...
I wonder if this could be used to make C# webassembly more viable. If I remember, a significant part of the download size is the .NET libraries (themselves written in C#?)
On that note, is there any 2D game framework/engine that targets the browser that uses C# and produces small binaries?
You might enjoy reading this GitHub thread [0] where the community contributed a WASM library wrapping the C# regex code so that regex101.com could have a "C# mode". Lots of nerd sniping about reducing the payload size.
(There's also another thread [1] discussing the minification of a rust version of that same regex101 wasm library to provide a "rust mode" using @burntsushi's regex crate.)
Until WASM ships GC, you're stuck with having to download an entire runtime regardless.
>On that note, is there any 2D game framework/engine that targets the browser that uses C# and produces small binaries?
Godot supports C# scripting and WASM compilation, probably your best bet.
Thanks for the Godot tip. I haven't checked it out in a while. I found it very counterintuitive (and a 3D engine is definitely overkill for small 2D games... I basically just want Flash... I guess Phaser or Haxe are my best bets).
A good source of truth for an actual executable would be Native AOT binary, which can run on Windows, Linux or macOS. Naturally, it will be much larger, having to incorporate GC, possibly ThreadPool, Console and other auxiliary code.
But anyway, this other article [0] (shared in another comment here) about creating the smallest version of Snake (the game) was done on top of .NET Core (the new version of .NET that isn't bundled with Windows) and effectively does the same thing, but actually produces a non-.NET binary, if that's more to your taste.
[0]: https://medium.com/@MStrehovsky/building-a-self-contained-ga...
except for runtime environment initialization, JIT compilation, and execution.
I think comparing container images would be a good start. Use the most minimal base image you can get away with, or even "FROM scratch" if possible, but compare only images with the same architecture. I'd prefer 32-bit, to take things like running 32-bit on 64-bit or 64-bit with pointer compression out of the picture.
Then compare the size of the uncompressed exported tar. Probably also not completely fair, depending on what question you want to answer but it takes the obvious variances out of the equation.
EDIT: Thinking about it some more, it might even be more fair to compare maximally compressed image size to account for compression within the container. Of course you'd have to compress with the same algorithm and parameters or just add the decompressors size to the final result like they do in compression benchmarks.
Specifically about how C leverages the “free” runtime of the OS while Lisp has to bundle is own.
This idea of container runtime sizes would be an amusing thread. A good test of something like Nix or Guix to a very fine grain. “We don’t need bash or ls so we removed it. “ “The VM only simulate a single network card, so we’ll scrap all the other driver. “
Windows has only recently been moving toward providing a public run-time for C; the "Universal C Run-Time" (UCRT) that has been introduced in Windows 10.
echo "hello world"I count 18 bytes in that program. Too long!
I left a definitive answer on Quora some years ago:
https://www.quora.com/What-programming-language-has-the-shor...
cd /tmp
echo 'echo $0' > hello\ world
chmod +x hello\ world
PATH=. hello\ worldHello World
It's an odd annoyance that Windows developers have to deal with.
As a user, think about how annoying it is to get a message saying you need to run Windows Update before you can start an application. Totally unnecessary own-goal for the team that decided to ship their independent component in Windows Update.
It's way easier to either 1.) go self-contained, or 2.) use the on-the-fly .NET download for a framework-dependent build. I absolutely think they made the right call removing current .NET as a Windows component. The annoyance was far greater when .NET Framework was part of Windows.
correction: without some specific version of .net.
You can do this for any format that stores executables, for any programming framework. .jar might be interesting, .py obviously isn't. You can do this for obscure old formats, or for something very common like the standard Linux .elf.
Although .zipapp is to Python what .jar is to .py. Probably even closer to .war.
But for purely stand alone stuff, "nuitka3 /tmp/hello.py --standalone" does output a executable that can be used without the user to manually bring in the Python run time. In that case, the hello world is about 16Mb on Ubuntu.
It would be interesting to do this with MicroPython though.
That's fair though. The post is titled "the smallest .NET hello world binary", not "the smallest C# hello world binary"
Display drivers and firmware of various mcus used in the computer monitor itself?
As a matter of OS design, this is no longer obvious:
https://lwn.net/Articles/806776/
> A new mechanism to help thwart return-oriented programming (ROP) and similar attacks has recently been added to the OpenBSD kernel. It will block system calls that are not made via the C library (libc) system-call wrappers.
MacOS doesn't implement that, sure, but it could.
Could be a fascinating story.
0x200 is 512, or 0.5K. It's been a long time since I've done PE size optimisation at this level but I remember 512 was the minimum accepted by Windows 9x but NT could accept alignments as small as 1.
Also, I didn't see any mention of the old trick of overlapping the DOS MZ and PE headers, which was state-of-the-art when I was still doing this stuff: http://www.phreedom.org/research/tinype/
...and then you realise that the demoscene has managed to do this in a 1k binary:
https://www.pouet.net/prodlist.php?type%5B%5D=1k&platform%5B...
> "A fully self-contained natively compiled C# Hello World, including GC and everything can be as small as ~440 kB."
.net classic framework is windows deprecated legacy, and really no new apps should be built on it.
https://learn.microsoft.com/en-us/dotnet/core/deploying/nati...
I agree though, the minimal .NET example is still not close to a full-featured .NET platform. I kind of like the idea of opting-out of features though. Bringing the whole runtime for the party each time is a bit overkill (unless its already on every target machine). Lots of code doesn't need reflection. Some special cases actively dislike GC, etc.
thats_just_c_with_extra_steps.jpg
module Hello
begin
println("hello world")
end Hello
It uses the https://github.com/rochus-keller/Pelib assembly generator.Recently though, I think maybe the way to get off the "cycle of reincarnation" for this type of thinking is to do WASM - but I cannot abandon hardware like that. I am mentally incapable of accepting a spiritual machine that I cannot break with a hammer upon my desk; [I am] too primitive; an indestructible global computer that runs on other people's hardware for thousands of dollars of compute time and is slower than an RPi (like Ethereum) is anathema to me in some fundamental way.
I still want to get that (Wirth was right) on a shirt with his face on it.
In what respect in this context? I should have explicitly stated that "Hello.dll" is a .NET assembly, i.e. Oberon+ uses the Mono CLR to run and debug the Oberon+ code (but not the .NET framework); it can also generate C99 (as a substitute for AOT compilation), but here it is about the minimal size of a Hello World .NET binary.
WASM and especially the WAMR runtime might be a good alternative to the Mono CLR in future, but today it's too slow and only a few architectures are supported (much less than Mono).
Shouldn't Oberon+ be self-hosting? There's so much C++ ecosystem crud. It reminds me of all those projects for open source software hosting forges promoted as alternatives to GitHub and Gitlab, and then when you go to clone their code or file a bug... it's all on GitHub...
As a former Smalltalk developer, what do you think about it would actually slow you down (despite popular claims re productivity in Smalltalk)? Is it tooling for C++ static types? If so, then what about Delphi?
And is it one goal of yours, then, to mold Oberon+ so that it would eventually be at parity, productivity-wise, with C++?
Just a fun anecdote. I don't know if this is the case anymore.
You can think of this like a source map, but only files and line numbers.
It has always been trivial to load an assembly in something like dnSpy or another IL Disassembler and generate C# code and patch .NET assemblies. At least in the versions < 5.
https://learn.microsoft.com/en-us/dotnet/standard/assembly/f...
But your original post especially doesn't make sense because the default Debug configuration has always put the debug symbols in a separate PDB file. So even if you were arguing that early C# developers didn't understand how to use their tools, they wouldn't have had the source embedded in the EXE.
I do recall good old Visual Basic did this though, as it ran interpreted. The executable it generated was just a small loader with the source code appended.
No. It taught me quite some.
https://www.gnostice.com/nl_article.asp?id=225&t=The_Smalles...
A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux: http://www.muppetlabs.com/~breadbox/software/tiny/teensy.htm...
Tiny ELF Files: Revisited in 2021: https://nathanotterness.com/2021/10/tiny_elf_modernized.html
https://nathanotterness.com/2021/10/tiny_elf_modernized.html
has a 120 byte hello world program for x86_64 ELF.
Sorry, I don't understand how the OS decides to load your "helloworld" runtime, instead of e.g. the .NET Framework 4 runtime, to go with your 0-length binary?
The program file created in the article has a valid PE file header, including a ".NET Directory" header section that tells the OS about the runtime to load. If you don't have an equivalent, how is your new runtime being loaded?
> See "Stück" for example.
I don't understand this reference?
[0]: https://medium.com/@MStrehovsky/building-a-self-contained-ga...
You know you don't need to use the new features, you can still build your apps the way you like and gradually adopt stuff you think might be better for you. Many of the new features aren't always aimed at line of business apps but have been added to improve "systems programming" capabilities.
And they aren't taking anything way from you (unless you're jumping from .NET Framework to .NET 5 and beyond, e.g. remoting).
Why not teach the right tool for the job and illustrate it being ported to C (or lower) if the aim is to make it smaller?
Can you imagine a mechanical engineer trying to turn a screw with a pair of pliers with the justification that it's just a simple screw and they want to see what the minimal number of turns is whilst shaving the screw to improve grip? You'd think they were nuts...
Not true. You can get all of that in C++ if you’re careful and enable compiler flags that no one uses. The entire reason for the IL platform that .NET uses is for cross-platform executables, just like Java.
.NET does give one safety in the form of bounds checking and what-not, but that’s the runtime, not the byte code. There’s no “bounds check” opcode; array dereferences are managed by the runtime and bounds checks are elided if safety can be proven (like Rust does).
(Granted it's apples-to-oranges, given that this blog post is for C# managed mode (as opposed to AoT).)