perf(core): compile secret patterns about 30x faster, redact line output 1.7x faster

Merged
KR
2d ago
+56-11

Building the pattern that redacts a run's secrets was slow for what it does: every compile worked out each character's spellings from scratch, six locale case mappings per character, and a ledger compiles again at every registration. StreamRedactor also ran the value pattern over each write twice, once to place the cut and once to redact.

Now each character's pattern is built once per process, and a stream of finished lines is searched once. What is redacted does not change.

  • character(ch) results are kept in characterPatterns, capped at 4096 entries. A pattern depends on its character alone, never on a ledger, a name, or a value.
  • SecretLedger.holdFrom returns as soon as the cut sits at either end of the text or right after a line break, and otherwise searches only the cut's own line. Both rest on one invariant: no occurrence holds a \n unless a value spans lines (spansLines), which the existing cut at the last line break already relied on. A value with inner whitespace or a line break takes the old path.
  • redact and redactCut skip the split on known markers when the text has no <secret:.
Scenariomainthis PRdelta
compile 20 ledgers x 12 secrets24.62 ms0.88 ms28x
redact 20,000 short strings2.89 ms1.58 ms1.8x
stream, 1000 one-line writes0.53 ms0.32 ms1.7x
stream, 568 KB in 64 KB writes3.08 ms1.61 ms1.9x
stream, 1000 lines, a secret holding a space2.07 ms1.97 ms1.1x

Micro-benchmarks in one Node 24 process, median of 15. These are milliseconds per run: a short suite's wall time does not move (see bench:ab below).

Verified

Ran it locally: yes

  • Web benchmark, the built CLI of main (71a2b795) and of this branch, on a scratch test that prints the benchmark account's password in whole lines, split across two writes, and upper-cased on stderr. The two outputs are identical, and no file under the output directory holds the value:
stdout | |web| tests/zz-redact-evidence.e2e.ts > prints the password in whole lines and across writes
line one
line two password=<secret:benchmark.password>
split <secret:benchmark.password> done
stderr | |web| tests/zz-redact-evidence.e2e.ts > prints the password in whole lines and across writes
stderr <secret:benchmark.password>�
   ✓ |web| tests/zz-redact-evidence.e2e.ts > prints the password in whole lines and across writes 442ms
  • Differential fuzz, main's redact.ts and this branch's loaded in one process: 140,000 random texts over random ledgers (short secrets, leading, trailing, and inner whitespace, CRLF, markers, JSON, URL, HTML-entity, and base64 spellings, random string and byte chunking). 0 differences in redact, redactCut, redactFragments, holdFrom, and StreamRedactor output.
  • Compiled pattern source, maxFormLength, and spansLines for 169,526 single-character values, including every ASCII character once the cache was full: identical to main.
  • pnpm bench:ab -- tests/login-form.e2e.ts (web benchmark, 4 pairs): every test ran the same steps with the same outcome on both builds. Suite time 1.34 s on both, verdict same.

A fresh-context review asked for a test of the spansLines side of the new holdFrom branches; it is in. One pre-existing StreamRedactor edge case turned up while fuzzing. It behaves the same on main and here, and is tracked separately.

Reviewed with sol-loop: 1 round with gpt-6.2-sol. Sol verdict: approved

Written by Claude Fable 5.2 in Claude Code (T3 Code).