jdc_core/dbg.rs
1//! Process-lifetime caches for debug environment flags.
2//!
3//! `std::env::var` takes a global lock and walks `environ` on every call; the
4//! structuring pipeline checks debug flags at many decision points, and that
5//! showed up as a measurable hotspot (`__findenv_locked` at ~2.4% of samples
6//! while rendering `rt.jar`). These flags are process-start configuration, so
7//! they are frozen on first use — semantics-preserving.
8
9/// Cached presence check for a debug env flag.
10#[macro_export]
11macro_rules! dbg_flag {
12 ($name:literal) => {{
13 static CACHED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
14 *CACHED.get_or_init(|| std::env::var($name).is_ok())
15 }};
16}
17
18/// Cached parsed value of a debug env flag.
19#[macro_export]
20macro_rules! dbg_value {
21 ($name:literal, $ty:ty) => {{
22 static CACHED: std::sync::OnceLock<Option<$ty>> = std::sync::OnceLock::new();
23 CACHED
24 .get_or_init(|| {
25 std::env::var($name)
26 .ok()
27 .and_then(|v| v.parse::<$ty>().ok())
28 })
29 .clone()
30 }};
31}