Live data from Hacker News

Show HN: Tiny 1k Rust binary on Windows

github.com

21–30 of 66 posts

Re: Show HN: Tiny 1k Rust binary on Windows

#21
post #14

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...

I couldn't get it working and was familiar with the `llvm_asm!` syntax. Although, I may have to go back and switch it around again.

Re: Show HN: Tiny 1k Rust binary on Windows

#22

Zig 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, 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

#23
post #19
post #14

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...

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

#24
post #19

Earlier 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.

Oops, my bad, I was working with an outdated understanding of the situation. Thanks for correcting me.

Re: Show HN: Tiny 1k Rust binary on Windows

#25
post #17

I'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.

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.

Re: Show HN: Tiny 1k Rust binary on Windows

#26
post #22

Zig 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…

Just for fun, the compiled rust exe has 480 non-zero bytes, the zig exe had 512 bytes.

Re: Show HN: Tiny 1k Rust binary on Windows

#27

I'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...

The smallest executable gcc (on Ubuntu) compiles that to is 8296 bytes (tried every -O optimization).

Re: Show HN: Tiny 1k Rust binary on Windows

#28
I know making tiny Windows executables like this is done just for fun, but I'm curious if there's technically a performance loss from merging sections. IIRC the sections are split up and 4k aligned because they have to be page aligned in memory anyway.

Re: Show HN: Tiny 1k Rust binary on Windows

#29

On 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.

These literally just contain the compiled assembly and are usually used on, for example, the 4k demoscene.

There are plenty of 1k demos too, on Windows and otherwise:

http://www.pouet.net/prodlist.php?type%5B%5D=1k

Re: Show HN: Tiny 1k Rust binary on Windows

#30
post #17

Earlier 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.

So the point is moot, you can't have a simple 1k C example.
Post reply on HN