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

Optimization Iteration

Optimization is a scientific process: measure, hypothesize, change, measure again.

The Iteration Cycle

  1. Measure: Establish a baseline with Criterion
  2. Hypothesize: Form a testable prediction (“removing this allocation will improve throughput by 15%”)
  3. Change: Make exactly one change
  4. Measure: Compare with statistical rigor
cargo bench -- --save-baseline before
# Make the change
cargo bench -- --baseline before

Avoiding Premature Optimization

QuestionIf YesIf No
On the hot path?OptimizeSkip
Profiling shows > 5% of time?OptimizeSkip
Users notice the improvement?OptimizeSkip
Code already simple?Consider optimizingSimplify first

Common Patterns

Replace Allocation with Buffer Reuse

#![allow(unused)]
fn main() {
// Before: heap allocation per call
fn format_key(prefix: &str, id: u64) -> String {
    format!("{}_{}", prefix, id)
}

// After: reusable buffer
fn format_key(prefix: &str, id: u64, buf: &mut String) {
    buf.clear();
    buf.push_str(prefix);
    buf.push('_');
    buf.push_str(&id.to_string());
}
}

Enable SIMD via trueno

#![allow(unused)]
fn main() {
use trueno::Vector;
let v = Vector::from_slice(data);
let sum = v.sum();  // Automatic AVX2/AVX-512/NEON
}

Tracking Optimization History

DateTargetHypothesisResultKept?
2025-03matmulSIMD 4x throughput3.8xYes
2025-04parserPreallocate AST nodes2%No
2025-05inferenceReduce threads 48->162.05xYes

Failed optimizations are valuable data. Recording them prevents repeating experiments.


Navigate: Table of Contents