Conditional compilation for benchmarks

I've seen suggestions that #[cfg(bench)] or #[cfg(test)] will conditionally compile items for benchmarks, but they haven't been activating for me when I try, either with cargo bench or cargo test --benches. Which profile gets used when compiling for a benchmark?

External tests/ and benches/ always get a non-test version of the library.

cfg(test) takes effect only for tests in the library itself.

1 Like

That's a bummer. Any ideas how someone might expose private functions to a benchmark?

Context: the public functions use x86_64 feature checks to decide whether to compile AVX versions or not, but it would be nice to be able to benchmark both versions of these functions without having to recompile the benchmark with & without --target_cpu=native

For exposing stuff to tests, there are two methods:

  1. Don't put tests in tests/, and instead use #[cfg(test)] mod tests; inside the library. This way it will have access to everything that's crate-private.

  2. Create #[doc(hidden)] pub mod _secret_do_not_use; in the library and put internals-exposing items in there.

It's the same with benches, except they'll complain about being a nightly-only feature, so you may want to hide them behind a feature flag. There's also copied-to-stable bencher crate.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.