
In Process Caches in Rust Moka Quick Cache and Cached
Compare moka, quick_cache and cached for in-memory caching in Rust: size limits, eviction, time to live, concurrency, async support and a single-flight loader.
Profilers, flamegraphs, benchmarks and caches: find the slow part first, then fix only that.

Compare moka, quick_cache and cached for in-memory caching in Rust: size limits, eviction, time to live, concurrency, async support and a single-flight loader.

Cache the result of an expensive pure function in a HashMap or with the cached crate. Includes the recursion pattern, key design, bounded size and when memoizing is the wrong fix.

Benchmarks fail by measuring nothing: the optimizer deletes the work, the input is unrealistic, the machine is noisy or the harness measures setup. Here are the traps and how to avoid each.

sort is stable and allocates, sort_unstable does not and is usually faster. Learn when stability matters, sort_by_key pitfalls and the cached-key variant for expensive keys.

Choose how a cache learns that data changed: TTL, explicit invalidation on write, versioned keys, events, stale while revalidate, and how to stop stampedes.

Microbenchmarks mislead when the optimizer deletes work, inputs are unrealistic or caches are warm. Learn the usual lies and how to write benchmarks you can trust.

Safe Rust can leak memory. See how Rc cycles form, how Weak breaks them, how to detect a leak with counts, and why Rust's guarantee excludes leaks.

Find where a Rust service's memory goes: read RSS and allocator stats, profile allocations with dhat or heaptrack, tell a leak from growth, and fix the usual causes.

Generic code compiles once per type, and that can bloat a binary and slow builds. Find the biggest instantiations with cargo llvm-lines and cut them with inner functions and dyn.

Profile guided optimization compiles twice: once to collect a profile from a real workload, once to optimize with it. Do it by hand or with cargo-pgo, and judge the gain.

The default HashMap hasher is DoS resistant and slower than it needs to be for trusted keys. See what changes with FxHash, ahash and foldhash, and when not to switch.

Data layout and access patterns decide speed more than instructions. Learn cache lines, contiguous iteration, struct of arrays, branch prediction effects and how to test them.
We send one email when a new post is published. No spam.