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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//! Fast-path option-state cache — a Rust-only optimization backing
//! `isset()`.
//!
//! # Why
//!
//! `isset(X)` is the single most-called primitive in a shell. In zsh it is
//! `#define isset(X) (opts[X])` (Src/zsh.h:2557) — one load from a
//! `char opts[OPT_SIZE]` array indexed by the option constant. zshrs stores
//! option state authoritatively in `ported::options::OPTS_LIVE`, a
//! `RwLock<HashMap<String, bool>>`. Routing every `isset` through that map
//! meant, PER CHECK: a constant→name linear match (`opt_name`), a
//! name→constant scan (`optlookup`), an RwLock acquisition, and a String
//! hash. Every parameter expansion checks a fistful of options
//! (`SHWORDSPLIT`, `KSHARRAYS`, `NOMATCH`, `EXTENDED_GLOB`, …), so a real
//! config's startup performs millions of these — and profiling showed the
//! option lookups dominating even a tight function-call loop.
//!
//! This module restores C's representation: a flat array indexed by option
//! number that `isset` reads with a single relaxed atomic load. The
//! `HashMap` remains the source of truth for name-based access (`setopt`,
//! `unsetopt`, `$options`); this cache is a read-through mirror for the hot
//! numeric read only.
//!
//! # Coherence
//!
//! Every option-store mutation funnels through exactly three functions —
//! `opt_state_set`, `opt_state_unset`, `opt_state_restore` (and
//! `opt_state_set_via_alias`, which routes through the first two). Each of
//! those calls [`invalidate_all`] after mutating the map, so the next read
//! of any slot repopulates from the authoritative value. Reads outnumber
//! writes by orders of magnitude, so a full invalidate on write is far
//! cheaper than doing the name lookup on every read.
use ;
use crate;
/// Slot values: `-1` = unknown (repopulate lazily), `0` = unset, `1` = set.
/// Indexed by option number, mirroring C's `char opts[OPT_SIZE]`.
static OPTS_CACHE: =
;
/// Drop the entire cache. Used when a wholesale option-store change makes
/// per-slot tracking impossible (e.g. an unfiltered snapshot restore).
/// Invalidate a SINGLE option's cached value by canonical number. Used by
/// the mutators so setting one option (e.g. `doshfunc`'s per-call
/// `printexitvalue` flip) doesn't cold-invalidate every other option's
/// slot — which is what made the cache useless inside function calls.
/// `optno` may be negative (a `no…` alias); the sign is dropped since the
/// alias and its canonical share a slot.
/// O(1) option read backing `isset(optno)` — mirrors C `opts[optno]`.
///
/// Reads the cache; on a miss, resolves the authoritative value once (the
/// old `opt_state_get(&opt_name(optno))` path) and memoizes it. Preserves
/// the historical `unwrap_or(false)` semantics for unset / out-of-range
/// option numbers (e.g. `OPT_INVALID`).
/// The pre-cache read: number → canonical name → authoritative map lookup.