Compilers like LLVM can optimize away code during benchmarking by replacing computations with closed-form expressions or eliminating unused results entirely. Instead of relying on functions like Rust's `hint::black_box` or Zig's `mem.doNotOptimizeAway`, a more reliable approach is to make input parameters runtime-overridable (preventing constant folding) and print computation results or their hashes (preventing dead code elimination). This method prevents compiler optimizations from invalidating benchmarks while providing bonus features like easy parameter tuning and immediate detection of incorrect optimizations through hash mismatches.

3m read timeFrom matklad.github.io
Post cover image

Sort: