Open MinusKelvin opened 1 year ago
I hit this issue today. Was benchmarking small functions that were defined in the same file the benchmarks were. I was getting results of 0.00 ps per benchmark run. The functions had #[inline]
annotations, so I removed those. For one function, it was so small I needed to add #[inline(never)]
to prevent it from being inlined.
The section for Benchmarking With One Input in the Criterion User Guide says:
However, this is not sufficient to prevent the compiler from optimizing the benchmark away. It produces code analogous to:
After
function
is inlined (and possibly without inlining via other kinds of analysis), the compiler may perform loop invariant code motion, transforming it into code analogous to:This can result in claimed running times on the order of 1 CPU cycle.