January 27, 2026
When “instant” means “optimized away”
Why my Rust benchmarks were wrong, or how to use std:hint:black_box? (2022)
The “instant” speed was just the compiler hiding the work — now everyone’s side‑eyeing a weird instruction
TLDR: A Rust dev found “instant” benchmarks were bogus because the compiler optimized the work away; `black_box` helps but isn’t foolproof. Comments zeroed in on a weird “useless” instruction and a trimmed title, sparking debates about compiler quirks and benchmark trust — a reminder that “fast” can be an illusion.
Rustland thought it had a need-for-speed moment… until the author noticed some “instant” benchmark results that felt too good to be true. Cue a deep dive into std::hint::black_box — the Rust “please don’t optimize this” hint — and a reality check: this trick is best‑effort, not a magic shield. Translation: the compiler can still play Houdini with your code. The author walks through how using black_box on the result (not the input) matters, and why nano-time tests can lie. Docs for the curious: black_box.
But the real show is the comments. One user shrugs that even the title had to be squeezed to fit, which somehow became its own mini-drama. Another squints at a mysterious mov rcx, rsp instruction and wonders why the compiler would do something “useless” at high optimization — and the crowd goes wild with armchair detective energy. Strong opinions split fast: one camp says “compilers are sneaky, that’s the job,” while others treat the odd instruction like Bigfoot caught on camera. Jokes fly about the compiler “gaslighting” developers and how “O3” stands for “Oh? Oh. Ohhh.”
Moral of the thread: your “blazing fast” benchmark might just be the compiler quietly deleting the work — and the community will absolutely audit your assembly for sport.
Key Points
- •The author observed micro-benchmarks in Rust showing “instantaneous” times for operations expected to take a few nanoseconds.
- •Rust benchmarks use the unstable #[bench] attribute and test::Bencher::iter to execute and measure code repeatedly.
- •Internally, ns_iter_inner loops k times, calls black_box on the result of the benchmarked function, and times the loop via Instant.
- •std::hint::black_box is an identity function intended to inhibit optimizations on a best-effort basis and should not be relied upon for correctness.
- •black_box uses inline assembly with a volatile memory clobber in bootstrap mode and a compiler intrinsic in normal mode, with effectiveness varying by platform/backend.