When performance-critical code interacts with OS/process state or remote services, optimize by reducing unnecessary work only when correctness is preserved.
Apply: 1) Gate expensive scans behind capability probes, and cache the probe result once per process.
fn feature_supported() -> bool {
static SUPPORTED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*SUPPORTED.get_or_init(|| {
std::fs::metadata("/some/feature/file").is_ok()
})
}
2) Prefer streaming enumeration over materializing large collections when you’re going to inspect every candidate anyway.
fn all_ids_streaming() -> impl Iterator<Item=u32> {
std::fs::read_dir("/proc")
.into_iter()
.flatten()
.flatten()
.filter_map(|e| numeric_file_name(&e))
}
3) If you introduce limits, define the “fail-closed” behavior precisely (including off-by-one), and unit-test the bound + filtering logic. Don’t cap using iteration order when you don’t have an index to ensure correctness.
Net effect: you cut work (gating/streaming) without introducing silent correctness regressions (unsafe caps/order reliance) or stale-server hazards (unsafe caching).