Commit 08cd543
perf(spawner): compile redaction regexes once via OnceLock
redact() and verify_redacted() recompiled all 7 DEFAULT_REDACTION_PATTERNS
on every call via Regex::new(pattern). With MAX_CAPTURED_EVENTS = 4096 the
unit test test_captured_events_bounded calls redact() 4,106 times -> 28,742
regex compilations and a >60s test warning. In production every spawned-agent
output line flows through redact(), so this is a hot path.
Cache the compiled regexes in a static OnceLock<Vec<Regex>>, initialised once
on first use via compiled_patterns(). Both functions now iterate the cached
slice. Replacement-closure logic (preserve prefix groups, redact last group)
and the silent-drop-on-compile-error behaviour are preserved verbatim, so
external behaviour is identical.
std::sync::OnceLock is stable since Rust 1.70; workspace MSRV is 1.91, so no
new dependency is required. Single-file change to redaction.rs only.
This is a clean re-PR of the validated OnceLock core from #2967, which was
blocked for branch contamination only (unrelated #2884 report artefact,
#2941 AgentValidator tests, unused proptest lockfile churn, unsafe async
env-var mutation). The core OnceLock fix was unanimously approved by
pr-validator/pr-reviewer/pr-verifier. This branch carries only that fix,
cut from pristine origin/main.
Verified:
- cargo test -p terraphim_spawner --lib redaction:: -> 11 passed, 0 failed
- cargo clippy -p terraphim_spawner --lib -- -D warnings -> clean
- cargo fmt -p terraphim_spawner -- --check -> clean
- diff vs origin/main: 1 file, +49/-27, no Cargo.lock/Cargo.toml change
Refs #2961
Co-Authored-By: Claude <noreply@anthropic.com>1 parent 236d747 commit 08cd543
1 file changed
Lines changed: 49 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
| |||
28 | 30 | | |
29 | 31 | | |
30 | 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 | + | |
31 | 57 | | |
32 | 58 | | |
33 | 59 | | |
34 | 60 | | |
35 | 61 | | |
36 | 62 | | |
37 | 63 | | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
55 | 79 | | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
61 | 85 | | |
62 | 86 | | |
63 | 87 | | |
| |||
74 | 98 | | |
75 | 99 | | |
76 | 100 | | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
82 | 104 | | |
83 | 105 | | |
84 | 106 | | |
| |||
0 commit comments