1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//! Bench-safety helpers and the cardinal pattern for adaptive
//! primitive benches.
//!
//! # The cardinal rule
//!
//! Heavy setup that allocates [`SidecarBox<T>`](crate::SidecarBox)
//! lives OUTSIDE `b.iter()`. Only the cheap hot-path op runs inside.
//!
//! ```ignore
//! // CORRECT - build once, query many.
//! let map = build_adaptive_hashmap(N);
//! c.bench_function("get", |b| b.iter(|| map.get(&42)));
//!
//! // WRONG - rebuilds every iter; ~10^5 sidecar registrations.
//! c.bench_function("naive", |b| b.iter(|| build_adaptive_hashmap(N).get(&42)));
//! ```
//!
//! The `WRONG` pattern previously crashed the host while shipping the
//! `hashmap_trie_cascade` bench: each iter created ~100 SidecarBox
//! instances, criterion ran ~930 iters, the sidecar accumulated ~94k
//! registrations and exhausted threads / file descriptors / memory.
//!
//! # Defenses now in place
//!
//! 1. The substrate cap ([`crate::Sidecar::set_max_instances`]) panics with
//! a diagnostic naming the cap value and the likely cause well
//! before resource exhaustion.
//! 2. [`assert_capacity_fits`] lets a bench fail-fast at startup if
//! its planned workload would exceed the configured cap, instead
//! of trickling toward a panic mid-run.
/// Fail-fast guard: panic at bench startup if the configured global
/// instance cap is below `n_instances`. Call this once near the top
/// of a bench module before the heavy build runs.
///
/// Use this when a bench legitimately needs many simultaneous
/// SidecarBoxes (a multi-tenant simulation, a stress-test, an
/// adapter for an external workload). The panic message identifies
/// the gap so the operator can either raise the cap via
/// [`crate::Sidecar::set_max_instances`] or shrink the bench load.
/// Diagnostic: how many adaptive instances are currently registered
/// against the global sidecar. Useful inside a bench fixture to log
/// the high-water mark and verify the bench is not silently growing.