Concepts

Companion to the day-5 slides. Tests, build profiles, and parallelism — the three things that separate a working Rust prototype from a deployable bioinformatics tool.

Tests

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        assert_eq!(reverse_complement(b"ACGT"), b"ACGT");
    }

    #[test]
    #[should_panic]
    fn bad_input_panics() {
        complement_base(b'X');
    }
}

→ Book: Writing Tests

Property-style tests

“For many inputs, some invariant holds.” Hand-rolled today:

#[test]
fn revcomp_round_trip() {
    for seq in [b"A".as_slice(), b"ACGT", b"NACGTN", b""] {
        let twice = reverse_complement(&reverse_complement(seq));
        assert_eq!(&twice[..], seq);
    }
}

For randomly generated inputs with automatic shrinking, look at proptest and quickcheck (out of scope today).

Integration tests

A sibling tests/ directory next to src/:

my-crate/
├── src/main.rs
├── tests/
│   └── end_to_end.rs

Each file in tests/ is compiled as a separate crate that uses your library the way an external user would. cargo test runs them alongside the unit tests.

→ Book: Test Organisation

Build profiles

Profile Flag Optimisation Bounds checks Speed
dev (default) none minimal enabled slow
release --release full LLVM enabled 10×–100× faster
cargo build              # target/debug/<crate>
cargo build --release    # target/release/<crate>
cargo run --release      # always release for benchmarking

The --release flag is the single most important Rust-specific knob for performance. Always use it when measuring speed or shipping a binary. Reports that “Rust is slow on this benchmark” are almost always running the debug binary.

→ Cargo Book: Profiles

Measuring runtime — Instant

use std::time::Instant;

let start = Instant::now();
do_work();
let elapsed = start.elapsed();
println!("elapsed: {:.3?}", elapsed);

For order-of-magnitude comparisons, this is all you need. For statistically rigorous benchmarks (means, standard deviations, outlier detection) use criterion — out of scope today.

Detecting the build profile at compile time

let profile = if cfg!(debug_assertions) { "debug" } else { "release" };

cfg!(debug_assertions) is a compile-time boolean — true in debug builds, false in release. Useful for labelling benchmark output.

Rayon — data parallelism in three letters

use rayon::prelude::*;

let gcs: Vec<f64> = sequences.par_iter()
    .map(|seq| gc_content(seq))
    .collect();

.par_iter() instead of .iter() is usually the only change needed. Rayon distributes work across a thread pool using work-stealing. The compiler statically refuses any code that would race on shared mutable data — there is no way to write a data race in safe Rust + rayon.

Other useful tools in the same crate:

When parallelism helps

  • Yes — many independent items (millions of reads), each with non-trivial work (>a few microseconds).
  • No — a few items with very fast work; thread-pool overhead dominates.
  • Watch out — parallel .sum::<f64>() is non-deterministic at the last-bit level because float addition is not associative. Per-item .collect() (as in the example above) is deterministic, because each item is computed independently.

Send and Sync in two sentences

Types that are safe to move between threads implement Send; types that are safe to share between threads (via &T) implement Sync. Almost every type in safe Rust gets both automatically; the few that do not (like Rc<T> or Cell<T>) are the ones rayon refuses to parallelise — that refusal is the compile-time guarantee against data races.

Allocation as a performance bug

A common pattern in slow Rust code:

for record in records {
    let trimmed: String = record.sequence.to_string();  // allocates per record
    process(&trimmed);
}

Fix one or more of:

  • Take a &[u8] instead of &String.
  • Reuse a buffer with clear() + reserve() (the day-2 buffer-reuse pattern).
  • Use iter().filter(...) directly instead of building an intermediate Vec.

Profile first — the right place to optimise is usually not where you guessed.

Profiling and beyond (pointers, not required reading)

  • cargo flamegraph — Linux-only, one command, produces an SVG flamegraph.
  • perf + hotspot — the standard Linux profiler with a GUI.
  • samply — cross-platform sampling profiler that integrates with the Firefox profiler UI.
  • cargo asm — see the actual assembly Rust generates for a function.

You will not need any of these for the course, but they are good to know exist.