On 24/02/14 06:23 PM, S.A. wrote:
> Hello,
> 
> I am new to rust and have a question on what options to pass to rustc
> to reduce the size of the executable. I see a huge variation between the
> exec generated from a simple C vs Rust program:
> ::::::::::::::
> hello1.rs
> ::::::::::::::
> fn main () {
>     println!("Hello World!");
> }
> ::::::::::::::
> hello2.rs
> ::::::::::::::
> use std::io::stdio;
> 
> fn main () {
>     stdio::println("Hello World!");
> }
> ::::::::::::::
> hello.c
> ::::::::::::::
> #include <stdio.h>
> main () {
>     printf ("Hello World!\n");
> }
> 
> Compilations:
> $ gcc -o hello_c hello.c
> $ rustc hello1.rs
> $ rustc hello2.rs
> 
> Outputs from the above are:
> -rwxrwxr-x 1 admin admin    8377 Feb 24 17:25 hello_c*
> -rwxrwxr-x 1 admin admin 2874699 Feb 24 17:25 hello2*
> -rwxrwxr-x 1 admin admin 2879032 Feb 24 17:25 hello1*
> 
> As you can see the sizes of the rust execs (hello1 and hello2) are
> far more than the simple C's.
> 
> Set of linked libraries are:
> $ ldd hello_c hello1 hello2
> hello_c:
>     linux-vdso.so.1 =>  (0x00007fff00bfe000)
>     libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f440cfc3000)
>     /lib64/ld-linux-x86-64.so.2 (0x00007f440d397000)
> hello1:
>     linux-vdso.so.1 =>  (0x00007fff8effe000)
>     libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fbf2e1df000)
>     /lib64/ld-linux-x86-64.so.2 (0x00007fbf2e5b3000)
>     libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fbf2dfda000)
>     libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fbf2dcde000)
>     libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0
> (0x00007fbf2dac1000)
>     libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1
> (0x00007fbf2d8aa000)
>     librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fbf2d6a2000)
> hello2:
>     linux-vdso.so.1 =>  (0x00007fffef7fe000)
>     libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0
> (0x00007fc1853b2000)
>     librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fc1851a9000)
>     libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc184de9000)
>     /lib64/ld-linux-x86-64.so.2 (0x00007fc1855e3000)
>     libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc184be5000)
>     libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fc1848e8000)
>     libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1
> (0x00007fc1846d2000)
> 
> Can someone shed some light on this.
> 
> Thanks

You're statically linking against the whole Rust standard library, but
using only dynamic linking with C.

However, link-time optimization (-Z lto -O) should result in a binary as
slim as C but does not due to the design of the standard library. It's
more a framework than a library since it actually calls into your code
and it makes heavy use of virtual function tables which prevents
optimizing out most of the dead code.

You can use `#[no_std]` to opt-out of the standard library, but that's
far from a satisfactory answer. It's part of the cost of having support
for M:N threading and will hopefully be reduced.

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to