Thanks for the feedback.
> By the way, another thing about Rust - are you actually sure that build-std
> is needed for Rust? Nim has dead code elimination always enabled, so I'm not
> sure why do you mention the stdlib explicitly.
Afaik, by default Rust links the precompiled stdlib blob statically, which is
built for speed and not size. `build-std` let you choose your options, but
compile speed is slower. Without this, the output size is much larger. See:
cargo +nightly build \
-Z build-std-features=panic_immediate_abort \
--target="x86_64-unknown-linux-musl" \
--release
Run
= 370896 bytes
cargo +nightly build \
-Z build-std=std,panic_abort \
-Z build-std-features=panic_immediate_abort \
--target="x86_64-unknown-linux-musl" \
--release
Run
= 51296 bytes
> I'm not accustomed to Rust so I don't know how does it work regarding safety
> checks - are they enabled in release? If so, then it's fair, otherwise you
> need to use -d:danger as Nim has safety checks (like range checks) enabled
> with -d:release
I selected release instead of danger as I don't know about Rust runtime checks,
but according to what @ElegantBeef says seems not the wrong choice here.
> -d:useMalloc - make Nim use the default C malloc (in this case Musl's)
> instead of its own, which will bring the binary size down. Only relevant to
> ARC and ORC (there's a PR to add it to refc but it's unmerged)
I tried this flag, but it increased the binary size from 30392 to 34488 bytes
> \--os:any -d:posix - make Nim target the "any" OS with POSIX features
> enabled, also brings the size down but is only suitable for small CLI
> programs that don't interface with the OS that much
I flag this as unfair to Rust, but I tested it and reduced binary size from
30392 to 26296 bytes
> -d:noSignalHandler - disable signal handling - Nim always registers signal
> handlers by default to catch things like SIGINT, and those handlers take a
> little bit of binary size, so disabling them reduces it.
Surprisingly, this decreased the binary size the same amount as previous flag:
from 30392 to 26296 bytes
> \--threads:off - only applies to Nim 2.x as threads are disabled by default
> for Nim 1.x
No change in both stable and devel (if threads are not used it's not added by
default?)
> Yes, you can use Zig (its C compiler feature). Just install Zig itself and
> then install <https://github.com/enthus1ast/zigcc> , after that you can tell
> Nim to use zigcc as the Clang binary. Then you just have to specify a C
> compiler option -target x86_64-linux-musl for it to statically link with musl.
Thanks! Here some results with
nim \
--cc:clang --clang.exe="zigcc" --clang.linkerexe="zigcc" --forceBuild:on \
--passC:"-target x86_64-linux-musl" \
--passL:"-target x86_64-linux-musl" \
--passC:"-flto" \
--passL:"-flto" \
--passL:"-static" \
--panics:on \
--gc:arc \
-d:release \
--opt:size \
c hello \
&& strip -s hello
Run
The output seems larger than gcc+musl:
* Nim stable: 30392 -> 34768 bytes
* Nim devel: 30464 -> 35272 bytes