use std::fmt::Write as FmtWrite;
use std::path::PathBuf;
const PRESET_INFO: &[(&str, &[u8])] = &[
("balanced", &[0, 1, 2, 4, 8, 16, 32, 64]),
];
const BLOCK_ALIGN: &[(u16, &str)] = &[
(31, "align::A64"),
(63, "align::A128"),
(127, "align::A256"),
(255, "align::A512"),
(511, "align::A1024"),
(1023, "align::A2048"),
(2047, "align::A4096"),
(4095, "align::A8192"),
];
const BACKOFF_INFO: &[(&str, &str)] = &[
("crossbeam", "backoff::Crossbeam"),
("yield", "backoff::Yield"),
];
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR not set");
let path = PathBuf::from(&out_dir).join("bench_registry.rs");
let feature_enabled = std::env::var("CARGO_FEATURE_BENCH_REGISTRY").is_ok();
if !feature_enabled {
std::fs::write(
path,
"// bench_registry feature not enabled — stub only.\n\
fn lookup_ubq_job_factory(\n\
\x20\x20\x20\x20_label: &str,\n\
\x20\x20\x20\x20_scenario: ScenarioConfig,\n\
\x20\x20\x20\x20_repeat_index: usize,\n\
\x20\x20\x20\x20_mode: Mode,\n\
\x20\x20\x20\x20_items_per_producer: u64,\n\
) -> Option<JobFactory> { None }\n",
)
.expect("failed to write bench_registry.rs stub");
return;
}
let mut code = String::with_capacity(256 * 1024);
writeln!(code, "// Auto-generated by build.rs — do not edit.").unwrap();
writeln!(code, "fn lookup_ubq_job_factory(").unwrap();
writeln!(code, " label: &str,").unwrap();
writeln!(code, " scenario: ScenarioConfig,").unwrap();
writeln!(code, " repeat_index: usize,").unwrap();
writeln!(code, " mode: Mode,").unwrap();
writeln!(code, " items_per_producer: u64,").unwrap();
writeln!(code, ") -> Option<JobFactory> {{").unwrap();
writeln!(code, " match label {{").unwrap();
for &(preset, pool_values) in PRESET_INFO {
for &pool in pool_values {
for &(block, align_ty) in BLOCK_ALIGN {
for &(backoff_name, backoff_ty) in BACKOFF_INFO {
let label_str = format!("{},{},{},{}", preset, pool, block, backoff_name);
let type_expr =
format!("ConfiguredUBQ<u64, {backoff_ty}, {pool}, {block}, {align_ty}>");
writeln!(
code,
" {:?} => Some(make_ubq_job_factory::<{type_expr}>(label, scenario, repeat_index, mode, items_per_producer)),",
label_str,
)
.unwrap();
}
}
}
}
writeln!(code, " _ => None,").unwrap();
writeln!(code, " }}").unwrap();
writeln!(code, "}}").unwrap();
std::fs::write(path, code).expect("failed to write bench_registry.rs");
}