forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

envflags.rs53 lines, 2.2 KiBHistoryRawDownload
1//! Process-constant snapshot of the environment (lane-perf1).
2//!
3//! Every `EC_*` debug/trace flag in this crate is read on per-block, per-TU or
4//! even per-MC-call paths, and `std::env::var*` is a `getenv` + `strlen` +
5//! `CStr` validation every time: 11.6% of `decode_probe`'s self time. Nothing
6//! in the crate or its tests calls `set_var` (`grep -rn set_var crates/ec-av1`
7//! = 0 hits), so each name is a process constant and may be read once.
8//!
9//! [`env_flag!`] is the zero-syscall form for `is_some()`/`is_none()` checks:
10//! it keeps one `LazyLock<bool>` per CALL SITE, so a hot check compiles to an
11//! atomic load. [`var`] keeps the `Result<String, VarError>` shape of
12//! `std::env::var` for the sites that want the value, off one snapshot map.
13
14use std::collections::HashMap;
15use std::env::VarError;
16use std::sync::LazyLock;
17
18static ENV: LazyLock<HashMap<String, String>> = LazyLock::new(|| std::env::vars().collect());
19
20/// `std::env::var`'s contract, served from the snapshot. Non-UTF-8 values are
21/// dropped by `std::env::vars()`; the crate only reads `EC_*` flags and paths.
22pub(crate) fn var(name: &str) -> Result<String, VarError> {
23 ENV.get(name).cloned().ok_or(VarError::NotPresent)
24}
25
26/// `true` iff `name` is present in the environment. Prefer [`env_flag!`] on
27/// hot paths -- this one still hashes the name per call.
28pub(crate) fn is_set(name: &str) -> bool {
29 ENV.contains_key(name)
30}
31
32/// One `LazyLock<bool>` per call site: `std::env::var_os(N).is_some()` with no
33/// syscall and no hashing after the first call.
34macro_rules! env_flag {
35 ($name:literal) => {{
36 static FLAG: std::sync::LazyLock<bool> =
37 std::sync::LazyLock::new(|| $crate::envflags::is_set($name));
38 *FLAG
39 }};
40}
41pub(crate) use env_flag;
42
43#[cfg(test)]
44mod tests {
45 #[test]
46 fn snapshot_matches_process_env() {
47 // PATH is set for every test process; a name no one exports is absent.
48 assert_eq!(super::is_set("PATH"), std::env::var_os("PATH").is_some());
49 assert_eq!(super::var("PATH").ok(), std::env::var("PATH").ok());
50 assert!(!super::is_set("EC_AV1_ENVFLAGS_NEVER_SET"));
51 assert!(!env_flag!("EC_AV1_ENVFLAGS_NEVER_SET"));
52 }
53}