Skip to content

Instantly share code, notes, and snippets.

@lifthrasiir
Last active September 27, 2017 14:17
Show Gist options
  • Select an option

  • Save lifthrasiir/b9f8ff94d0431cbe4e549eccba77c855 to your computer and use it in GitHub Desktop.

Select an option

Save lifthrasiir/b9f8ff94d0431cbe4e549eccba77c855 to your computer and use it in GitHub Desktop.
Why is a Rust executable large? (DRAFT)
Suppose that you are a programmer primarily working with compiled languages. Somehow you’ve got tired of those languages, there may be multiple valid reasons, and heard of a trendy new programming language called [Rust](https://rust-lang.org/). Looking at some webpages and the [official forum](https://user.rust-lang.org/), it looks great and you decides to try it out. It seems that Rust was a bit cumbersome to install in the past, but thanks to [rustup](https://rustup.rs/) the problem seems gone by now. Cargo seems to be great, so you follow the [first sections of the Book](https://doc.rust-lang.org/book/) and put a small greeting to the new language:
```rust
fn main() {
println!(“Hello, world!”);
}
```
Amazingly `cargo run` runs without a hassle. It is kind of a miracle as you used to configure the build script, Makefile, projects or whatever before building things. Impressed, you realize that the executable is available in `target/debug/hello`. You instinctively type `ls -al` out (or is it `dir`?) and you cannot believe your eyes:
```
$ ls -al target/debug/hello
-rwxrwxr-x 1 lifthrasiir 650711 May 31 20:00 target/debug/hello*
```
650 *kilobytes* to print anything?! You remember that Rust is probably a sole language that may possibly displace C++, and C++ is noted of the code bloat; would that mean Rust failed to fix one of C++’s big problems? Out of curiosity, you make the same program in C and compile it. The result is eye-opening:
```
$ ls -al hello-c
-rwxrwxr-x 1 lifthrasiir 8551 May 31 20:03 hello-c*
```
*Maybe C has a benefit of having bare-metal libraries*, you think. This time you try a C++ program using `iostream`, which should be much safer than C’s naive `printf`. But surprisingly it still seems tiny compared to Rust:
```
$ ls -al hello-cpp
-rwxrwxr-x 1 lifthrasiir 9168 May 31 20:06 hello-cpp*
```
What is wrong with Rust?
----
It seems that the surprisingly large size of Rust binary is a massive concern for many. This question is by no means new; there is a well-known, albeit year-old, [question](https://stackoverflow.com/questions/29008127/why-are-rust-executables-so-huge) on StackOverflow, and searching for [“why is rust binary large”](https://duckduckgo.com/?q=why+is+rust+binary+large) gives several more. Given the frequency of such questions, it is a bit surprising that we don’t yet have a definitive article or page dealing with them. So this is my attempt to provide one.
For wondering readers: All examples are tested in Rust 1.9.0. Unless noted, the primary operating system used is Linux 3.13.0 on x86-64. Your mileage may vary.
## Linkage
First, I have to admit that I was cheating with the size of C and C++ binaries. The *fair* comparison would be as follows:
```
$ make hello-c CFLAGS='-static'
cc -static hello-c.c -o hello-c
$ make hello-cpp CXXFLAGS='-static -static-libstdc++'
g++ -static -static-libstdc++ hello-cpp.cpp -o hello-cpp
$ ls -al hello-c hello-cpp
-rwxrwxr-x 1 lifthrasiir 877175 May 31 20:10 hello-c*
-rwxrwxr-x 1 lifthrasiir 1653135 May 31 20:10 hello-cpp*
```
Okay, so it seems that Rust is actually far better than C and C++. But… why is it “fair”? Isn’t an 1 MB executable too much for such a simple program, no matter the language is, after all?
A binary executable is not a simple data format. It is normally processed and often altered by an OS routine called a “dynamic linker” (not to be confused a “linker”, that combines assembled binaries into a single executable).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment