Skill 13 · Rust Best Practices
Subchapter 13.3
references/chapter_03.mdMarkdown9 KBView on GitHub
The golden rule of performance work:
Don’t guess, measure.
Rust code is often already pretty fast - don’t “optimize” without evidence. Optimize only after finding bottlenecks.
--release flag on you builds (might sound dummy, but it is quite common to hear people complaining that their Rust code is slower than their X language code, and 99% of the time is because they didn’t use the --release flag).$ cargo clippy -- -D clippy::perf gives you important tips on best practices for performance.cargo bench (opens in a new tab) is a cargo tool to create micro-benchmarks and test different code solutions. Write a test scenario and bench you solution against the original code, if your improvement is larger than 5%, might be a good performance improvement.cargo flamegraph (opens in a new tab) a powerful profiler for Rust code. For MacOS, samply (opens in a new tab) might be a better DX option.Flamegraph helps you visualize how much time CPU spent on each task.
# Installing flamegraph
cargo install flamegraph
# cargo support provided through the cargo-flamegraph binary!
# defaults to profiling cargo run --release
cargo flamegraph
# by default, `--release` profile is used,
# but you can override this:
cargo flamegraph --dev
# if you'd like to profile a specific binary:
cargo flamegraph --bin=stress2
# Profile unit tests.
# Note that a separating `--` is necessary if `--unit-test` is the last flag.
cargo flamegraph --unit-test -- test::in::package::with::single::crate
cargo flamegraph --unit-test crate_name -- test::in::package::with::multiple:crate
# Profile integration tests.
cargo flamegraph --test test_name
# Run criterion benchmark
# Note that the last --bench is required for `criterion 0.3` to run in benchmark mode, instead of test mode.
cargo flamegraph --bench some_benchmark --features some_features -- --bench
# Run workspace example
cargo flamegraph --example some_example --features some_features❗ Always run your profiles with
--releaseenabled, the--devflag isn’t realistic as it doesn’t have optimizations enabled.
The result will look like a flame graph where:
The y-axis shows the stack depth number. When looking at a flamegraph, the main function of your program will be closer to the bottom, and the called functions will be stacked on top, with the functions that they call stacked on top of them.
The width of each box shows the total time that that function is on the CPU or is part of the call stack. If a function’s box is wider than others, that means that it consumes more CPU per execution than other functions, or that it is called more than other functions.
❗ The color of each box isn’t significant, and is chosen at random.
Cloning is cheap… until it isn’t
In sections Borrowing over Cloning and Important Clippy lints to respect we mentioned the impacts of cloning and the relevant clippy lint redundant_clone (opens in a new tab), so in this section we will explore a bit “when to pass ownership”.
.clone() if you truly need a new owned copy. A few examples:
std::ops but still need ownership to the old data:use std::ops::Add;
#[derive(Debug, Copy, Clone, PartialEq)]
struct Point {
x: i32,
y: i32,
}
impl Add for Point {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
Point { x: 3, y: 3 });fn snapshot(a: &MyValue, b:&MyValue) -> MyValueDiff {
a - b
}
impl Sub for MyValue {
type Output = MyValueDiff;
fn sub(self, other: Self
Arc, Rc).Copy but as costly as std::collections. An example is HTTP client like hyper_util::client::legacy::Client that cloning allows you to share the connection pool.pub fn with_xyz(&mut self, value: Xyz) -> &mut Self.// Inline `HashMap` insertion extension
fn insert_owned(mut self, key: K, value: V) -> Self {
self.insert(key, value);
self
}let not_validated: String = ...;// some user source
let validated = Validate::try_from(not_validated)?;
// Technically that `try_from` maybe didn't need ownership, but taking it lets us model intentfn process(values: &[T])), instead of ownership (fn process(values: Vec<T>))..iter or slices:for item in &some_vec {
...
}&mut MyStruct.Sometimes you don’t actually need owned data, but that is not clear from the API perspective, so using std::borrow::Cow (opens in a new tab) is a way to efficiently address this case:
use std::borrow::Cow;
fn hello_greet(name: Cow<'_, str>) {
println!("Hello {name}");
}
hello_greet(Cow::Borrowed("Julia"));
hello_greet(Cow::Owned("Naomi".to_string()));impl Copy, usize, bool, etc) on the stack.> 512 bytes) by value or transferring ownership. Prefer pass by reference (e.g. &T and &mut T).enum OctreeNode<T> {
Node(T),
Children(Box<[Node<T>; 8]>),
}Copy or a cheaply Cloned are efficient to return by value (e.g. struct Vector2 {x: f32, y: f32}).#[inline] when benchmark proves beneficial, Rust is already pretty good at inlining without hints.let buffer: Box<[u8; 65536]> = Box::new(..) would first allocate [u8; 65536] on the stack then box it, a non-const solution to this would be let buffer: Box<[u8]> = vec![0; 65536].into_boxed_slice().const arrays, considering using crate smallvec (opens in a new tab) as it behaves like an array, but is smart enough to allocate large arrays on the heap.Rust iterators are lazy, but eventually compiled away into very efficient tight loops that are only called when consumed. Chaining .filter(), .map(), .rev(), .skip(), .take(), .collect() usually doesn’t cost extra and the compiler can reason well enough how to optimize them.
iterators over manual for loops when working with collections, the compiler can optimize them better than manually doing it..iter() only creates a reference to the original collection, this allows you to hold multiple iterators of the same collection.process accepts an iterator.let doubled: Vec<_> = items.iter().map(|x| x * 2).collect();
process(doubled);fn process(arg: impl Iterator<Item = T>)):let doubled_iter = items.iter().map(|x| x * 2);
process(doubled_iter);