Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rust

Performance

Allocators

Build Configuration - The Rust Performance Book

cargo add mimalloc
#![allow(unused)]
fn main() {
#[cfg(not(miri))]
#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
}

Cargo profiles

Profiles - The Cargo Book

# https://nnethercote.github.io/perf-book/build-configuration.html
[profile.release]
codegen-units = 1
lto = "fat"

# https://github.com/mstange/samply#turn-on-debug-info-for-full-stacks
[profile.profiling]
inherits = "release"
debug = true

Tools

See also this section of the Rust Performance Book.

dhat

dhat

Quickstart:

cargo add --optional dhat

In Cargo.toml:

[features]
dhat = ["dep:dhat"]

In main.rs:

#[cfg(feature = "dhat-heap")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;

Then:

cargo build --profile=profiling --features dhat -- your args here

Safety

cargo-careful

cargo-careful

cargo careful is a tool to run your Rust code extra carefully – opting into a bunch of nightly-only extra checks that help detect Undefined Behavior, and using a standard library with debug assertions.

cargo +nightly install cargo-careful
cargo +nightly fetch
cargo +nightly careful build -Zcareful-sanitizer=address --target=x86_64-unknown-linux-gnu --tests --frozen
cargo +nightly careful test -Zcareful-sanitizer=address --target=x86_64-unknown-linux-gnu --frozen

Scudo

Scudo

cargo add scudo
#![allow(unused)]
fn main() {
#[cfg(all(not(miri), debug_assertions))]
#[global_allocator]
static ALLOC: scudo::GlobalScudoAllocator = scudo::GlobalScudoAllocator;
}