Closed vlovich closed 7 months ago
Might be that you're not actually using anything from the lib and thus it gets completely removed from your dependency tree. Can you please check if either of the following fixes this?
#[used(linker)]
in the proc macroSemi-related, does putting benchmark code within the crate mean that downstream dependencies compile the benchmarks?
Yes. That is why Divan has an internal_benches
feature flag. See https://github.com/nvzqz/divan/issues/2#issuecomment-1785991600 for details.
I'm hoping it at least is dead-code stripped but would be good to call this out in the docs.
Dead code elimination doesn't apply here because the approaches used (pre-main
and linkme
) each have the symbols be required to exist in the final binary regardless of whether they're used.
Ah, yes. I'm probably not using anything from the lib. Will try again.
FWIW the workaround I'm using re feature flag is to have the library have a dev-dependency on itself with the benchmark feature enabled. It does mean that benchmarks get compiled into tests but that's generally a small price to pay considering that it does eliminate the benchmark support code for external users. Probably the best that can be done at this time until Cargo/Rust improve the benchmarking ecosystem.
Here's all I needed to change to make it work:
Cargo.toml:
[dependencies]
divan = { version = "0.1", optional = true }
[dev-dependencies]
my-crate = { path = ".", features = ["internal-benchmarks"] }
[features]
internal-benchmarks = ["dep:divan"]
lib.rs:
#[cfg(feature = "internal-benchmarks")]
pub const CRATE_USED: bool = true;
#[cfg(feature = "internal-benchmarks")]
mod benchmarks {
#[divan::bench]
fn my_benchmark() {}
}
in benchmark main:
fn main() {
let _ = memcache::CRATE_USED;
divan::main();
}
Not the best setup because it forces dependencies needed for running the benchmark itself to be in the [dependencies]
section (I mitigate that impact by making them optional & only imported by the internal-benchmarks
feature.
@vlovich In my tests it seems that you don't need to create the CRATE_USED
const. It's enough to use my_crate;
or extern crate my-crate;
at the top of the file with the fn main() {divan::main()}
I'm trying out Divan on Linux. My project is a workspace layout. Within that workspace I have a crate
foo
. Withinfoo/benches
I create afoo.rs
file that has:and register it within
foo/Cargo.toml
:Then within foo/src/lib.rs I put:
I see "Benchmark main" when I run
cargo bench -p foo
but not "Running registered benchmark". If I move thebench
function tofoo/benches/foo.rs
then everything works. Not sure what I need to do to make this work.Semi-related, does putting benchmark code within the crate mean that downstream dependencies compile the benchmarks? I'm hoping it at least is dead-code stripped but would be good to call this out in the docs.