Hacker News new | ask | show | jobs
by brainspider 3547 days ago
I like the look of this language, and loaded it up to play around with for the first time yesterday. This was the first thing that struck me when I compiled the obligatory hello world..

  09/29/2016  04:51 PM         1,746,638 main.exe
  09/29/2016  04:51 PM                43 main.rs
1.7Mb for the exe from 43 bytes of code on windows 10. :/

AllTheLibs? I'm hoping I can improve on that a bit.

5 comments

Thanks for the link. It's a great read.
Note that Rust statically links in everything it needs by default, which is why hello world is so much larger in Rust than in C. :) If you want a hello world binary that's as small as C, there is a compiler flag for dynamically linking the generated binary.
A statically linked hello world in C using the MS toolchain gives me a 77Kb binary. The C++ version, using std::cout, is 176Kb.
5344 byte statically linked hello world in C:

    $ musl-gcc -static -Os -s -o hello hello.c
    $ ls -la hello
    -rwxrwxr-x. 1 sebastian sebastian 5344 Sep 30 06:22 hello
    $ ldd hello
	    not a dynamic executable
    $ ./hello 
    Hello, World
384 Byte hello world using NASM and ld on linux/amd64:

    BITS 64
    global _start
    
    %define SYS_write		1
    %define SYS_exit		60
    %define STDOUT_FILENO	1
    
    section .text
    _start:
    	call	after
    strHello:	db 'Hello, World!',10
    lenHello	equ $-strHello
    after:
    	pop		rsi
    	mov		rdx, lenHello
    	mov		rax, SYS_write
    	mov		rdi, STDOUT_FILENO
    	syscall
    
    	mov		rax, SYS_exit
    	xor		rdi, rdi
    	syscall
Most of the size overhead in the latter example comes from the ELF header, so by paying a bit more attention to the linker scripts, or just inlining a small ELF header with db in the asm file, it would be even smaller.

Just for size comparison. I am sure that it's quite possible to generate small Rust binaries as well.

  > I am sure that it's quite possible to generate small Rust 
  > binaries as well.
Indeed, here's a 151-byte hello world[0] in Rust: http://mainisusuallyafunction.blogspot.com/2015/01/151-byte-...
It's not clear (though it seems like no?) that the parent used options like LTO that would reduce the binary size.
I just literally installed the compiler toolset and did the first demo in the tutorial. I have never seen anything so large before, but I'm not a coder and not a windows coder.

The only windows code I have compiled has been FreePascal and that wasn't this crazy.

Similar to what sebcat showed above, I've done a bit of assembly on systems 8bit and up including nasm on linux, so not used to binaries with this sort of baggage. Wasn't trying to paint rust in a negative light, just came as a surprise when I ran that first compile.

It's all good! I'm not assuming any malice. First impressions are also very important.
Did you build in release mode?

    cargo build --release
No I wasn't aware of that, so thanks. I literally had just typed in the first example in the tute. :)

Working through more of it today.

Looks like you're using the GNU toolchain- for reference, the MSVC toolchain gives me a 107Kb exe from a 43-byte hello world.
1.7 megabytes is essential nothing in both modern disk/flash space and network costs. This isn't to say that efficient, small code isn't important, but it seems really common for a lot of programmers to freak out when there's some minimum executable size required for general "runtime" stuff, even when practically speaking it doesn't really matter in the real world outside of niche embedded system applications.

If each 43 bytes you compiled expanded to ANOTHER 1.7mb, then, yeah, that's not good, but that isn't the case here.

It might not matter to an individual user or developer, but if Rust is to be successful in the long run I think it's important for Rust-based desktop applications to be adopted.

If an application wants to be included in the standard install of, say, Ubuntu, one of the biggest costs from the Ubuntu maintainer's point of view is going to be "how much space will this take up on the iso, and what are we going to have to remove if we include this?" If the binaries are very small, it's a lot easier to make the case that they should be included.

It's easier for a distro to do dynamic linking for Rust binaries, since they can mandate a single compiler version across the ecosystem.
True. If dynamic linking is all that's needed to get tiny binaries, then that's probably sufficient from the distro's point of view.
I come from an assembly background (but spent the last 20 years doing scripting languages), so this came as a shock to me.
Now add up all the runtime stuff added by whatever scripting language you use and compare. Not many would come out ahead, Lua comes to mind.

The rust generated binary is statically linked and self-contained. It is pretty neat that it takes less than 2MB. In debug mode even, by the looks of it.