I notice you moved from asm! to llvm_asm! [0]. Since the commit message doesn't explain why, would you be able to do so here? (Purely for my own curiosity) [0] https://github.com/mcountryman/min-sized-rust-windows/commit...
Show HN: Tiny 1k Rust binary on Windows
21–30 of 66 posts
Re: Show HN: Tiny 1k Rust binary on Windows
#22Zig actually does a good job of cutting final output bytes automatically , even with standard library use. I think (?) its compiler sort-of implements a more advanced version of Unix strip. Andrew Kelley could probably chime in more on it.
const std = @import("std");
pub fn main() void {
std.debug.print("Hello, world!\n", .{});
}
----------zig build-exe hello.zig -O ReleaseSmall --strip --single-threaded -target x86_64-windows
Resulting hello.exe is 3072 bytes. From these 2560 are zero bytes. The only system calls from the resulting binary are to ExitProcess, GetLastError and WriteFile from KERNEL32.dll Additionally, changing the release optimizations:
zig build-exe hello.zig -O ReleaseFast --single-threaded --strip -target x86_64-windows
produces hello.exe of 2560 bytes, 1849 from which are zeroes.
Note all: winter_blue has right to mention Zig. It's about what's provided out of the box in that new language and the provided optimizations are really effective.
Edit: asking mcountryman to explain: "the compiled rust exe has 480 non-zero bytes" -- I think you mean not just "compiled" but "hand-optimized writing hundreds of lines of the rust source code and not using standard library"? https://github.com/mcountryman/min-sized-rust-windows/blob/m... https://github.com/mcountryman/min-sized-rust-windows/blob/m...
Edit2: Please note that zig doesn't eventually link to printf and libc there, but links and resolves the std calls from the source to the calls to KERNEL32.dll.
Re: Show HN: Tiny 1k Rust binary on Windows
#23I notice you moved from asm! to llvm_asm! [0]. Since the commit message doesn't explain why, would you be able to do so here? (Purely for my own curiosity) [0] https://github.com/mcountryman/min-sized-rust-windows/commit...
They are the same: asm! was replaced by llvm_asm! because it currently only supports llvm asm and the name might suggest otherwise [1]. A backend independant syntax for asm! was recently specified [2], but that will take some time to implement. [1] https://github.com/rust-lang/rfcs/pull/2843 [2] https://github.com/rust-lang/rfcs/pull/2873
That first PR moved the old asm to llvm_asm, and then the new asm was built after a period of time so folks could migrate their code to llvm_asm, as a temporary measure. This commit happened after that transition so this has to be from the new asm back to the transition version.
Re: Show HN: Tiny 1k Rust binary on Windows
#24Earlier quoted context omitted.
They are the same: asm! was replaced by llvm_asm! because it currently only supports llvm asm and the name might suggest otherwise [1]. A backend independant syntax for asm! was recently specified [2], but that will take some time to implement. [1] https://github.com/rust-lang/rfcs/pull/2843 [2] https://github.com/rust-lang/rfcs/pull/2873
They are not the same, and the asm! version is already implemented; the RFC came with an implementation. That first PR moved the old asm to llvm_asm, and then the new asm was built after a period of time so folks could migrate their code to llvm_asm, as a temporary measure. This commit happened after that transition so this has to be from the new asm back to the transition version.
Re: Show HN: Tiny 1k Rust binary on Windows
#25I'm disappointed. In C I can get a 1k Hello Word binary simply be writing int main(void) { puts("Hello World!"); return 0; } The Rust version is a mess [0] [0] https://github.com/mcountryman/min-sized-rust-windows/blob/m...
Can you, though? Compiling that source on Windows using visual studio produces a 98K executable.
/MD /link /align:4096 /filealign:512 /merge:.rdata=.text /merge:.data=.text
That produced a 1.5K binary, which with a custom stub should reach 1K.Re: Show HN: Tiny 1k Rust binary on Windows
#26Zig actually does a good job of cutting final output bytes automatically , even with standard library use. I think (?) its compiler sort-of implements a more advanced version of Unix strip. Andrew Kelley could probably chime in more on it.
It's very easy and fast to try: using 0.7.0+51d7c14ce from https://ziglang.org/download/ and: const std = @import("std"); pub fn main() void { std.debug.print("Hello, world!\n", .{}); } ---------- zig build-exe hello.zig -O ReleaseSmall --strip --single-threaded -target x86_64-windows Resulting hello.exe is 3072 bytes. From these 2560 are zero bytes . The only system calls from the resulting binary are to ExitProcess…
Re: Show HN: Tiny 1k Rust binary on Windows
#27I'm disappointed. In C I can get a 1k Hello Word binary simply be writing int main(void) { puts("Hello World!"); return 0; } The Rust version is a mess [0] [0] https://github.com/mcountryman/min-sized-rust-windows/blob/m...
Re: Show HN: Tiny 1k Rust binary on Windows
#28Re: Show HN: Tiny 1k Rust binary on Windows
#29On Windows the smallest exe can be achieved by writing in assembly and compiling as .com executable. These literally just contain the compiled assembly and are usually used on, for example, the 4k demoscene. 1k with rust is certainly a nice squeeze.
There are plenty of 1k demos too, on Windows and otherwise:
Re: Show HN: Tiny 1k Rust binary on Windows
#30Earlier quoted context omitted.
Can you, though? Compiling that source on Windows using visual studio produces a 98K executable.
You just need to add some compiler options; /MD should get the size down quite a bit already, then decrease the file alignment and merge sections together: /MD /link /align:4096 /filealign:512 /merge:.rdata=.text /merge:.data=.text That produced a 1.5K binary, which with a custom stub should reach 1K.