Skip to content
This repository was archived by the owner on Sep 30, 2020. It is now read-only.

Improve size-optimizing FAQ docs #1212

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions en-US/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -1324,17 +1324,40 @@ fn main() {

Two distinct versions of `foo` will be in the final binary, one specialized to an `i32` input, one specialized to a `&str` input. This enables efficient static dispatch of the generic function, but at the cost of a larger binary.

__Release Mode__

By default, building Rust with `cargo build` builds a debug mode binary. Debug builds disable optimizations to allow for greater ease of debugging, but result in substantially larger binary sizes. To decrease binary size, it's imperative to first build in release mode using `cargo build --release`.

__Debug symbols__

Rust programs compile with some debug symbols retained, even when compiling in release mode. These are used for providing backtraces on panics, and can be removed with `strip`, or another debug symbol removal tool. It is also useful to note that compiling in release mode with Cargo is equivalent to setting optimization level 3 with rustc. An alternative optimization level (called `s` or `z`) [has recently landed](https://github.com/rust-lang/rust/pull/32386) and tells the compiler to optimize for size rather than performance.
Rust programs compile with some debug symbols retained, even when compiling in release mode. These are used for providing backtraces on panics, and can be removed with `strip`, or another debug symbol removal tool.

It is also useful to note that compiling in release mode with Cargo is equivalent to setting optimization level 3 with rustc. An alternative optimization level ([called `s` or `z`](https://doc.rust-lang.org/cargo/reference/manifest.html#the-profile-sections)) [has recently landed](https://github.com/rust-lang/rust/pull/32386) and tells the compiler to optimize for size rather than performance.

__Jemalloc__

Rust uses jemalloc as the default allocator, which adds some size to compiled Rust binaries. Jemalloc is chosen because it is a consistent, quality allocator that has preferable performance characteristics compared to a number of common system-provided allocators. There is work being done to [make it easier to use custom allocators](https://github.com/rust-lang/rust/issues/32838), but that work is not yet finished.
Rust uses jemalloc as the default allocator, which adds some size to compiled Rust binaries. Jemalloc is chosen because it is a consistent, quality allocator that has preferable performance characteristics compared to a number of common system-provided allocators. To remove jemalloc and instead use the system interpretter, add the following code to `main.rs`:

```rust
use std::alloc::System;

#[global_allocator]
static GLOBAL: System = System;

fn main() {
let mut v = Vec::new();
// This will allocate memory using the system allocator.
v.push(1);
}
```

__Link-time optimization__

Rust does not do link-time optimization by default, but can be instructed to do so. This increases the amount of optimization that the Rust compiler can potentially do, and can have a small effect on binary size. This effect is likely larger in combination with the previously mentioned size optimizing mode.
Rust does not do link-time optimization by default, but [can be instructed to do so](https://doc.rust-lang.org/cargo/reference/manifest.html#the-profile-sections). This increases the amount of optimization that the Rust compiler can potentially do, and can have a small effect on binary size. This effect is likely larger in combination with the previously mentioned size optimizing mode.

__Panic Strategy__

By default, Rust supports unwinding when a panic occurs. This provides a helpful traceback, but requires additional generated code to support unwinding. The Rust compiler [can be instructed to abort on panic](https://doc.rust-lang.org/cargo/reference/manifest.html#the-profile-sections), instead, which reduces the size of the compiled binary.

__Standard library__

Expand Down