Skip to content

perf(detection): charge the scan budget per pass and cut monitor overhead - #94

Merged
rennf93 merged 3 commits into
masterfrom
perf/detection-cpu
Oct 16, 2026
Merged

rennf93 merged 3 commits into
masterfrom
perf/detection-cpu

Conversation

@rennf93

@rennf93 rennf93 commented Oct 16, 2026

Copy link
Copy Markdown
Member

Closes #93

Port of the reference fix in Guard-Core/guard-core#173. checkRegexPatterns opens a pass with one cpuSeconds() reading (only when a budget is active), scanPatternCharged compares hrtime against the request-scoped slice start after each pattern and charges the CPU clock only when 2 ms have elapsed, and a finally makes the final charge; beginScan starts the slice so it spans values, and scanBudgetExhausted() stays arithmetic only. The $cpuClock hook still works, and a $wallClockNs test hook drives the slice deterministically. The performance monitor returns before sanitizing when no callback is registered. Tests cover the clock reads per pass and a long pass stopping within about one slice. On a ~1.1 KB JSON-like body plus 10 headers detection went from 17.6 ms to 15.1 ms of CPU.

…attern

The per-request scan budget read getrusage() twice per pattern. It now reads the CPU clock when a pass opens, at each 2 ms wall-clock slice boundary and when the pass closes, matching the reference engine. The exhausted check stays arithmetic-only, so the budget lags by at most one slice plus one pattern.
@github-actions github-actions Bot added area: detection Touches src/Detection/ or src/Support/ (detectors, pattern tables, NFKC) tests Test suite changes labels Oct 16, 2026
@github-actions github-actions Bot added the ci GitHub Actions workflows label Oct 16, 2026
@rennf93
rennf93 merged commit 80a0c4e into master Oct 16, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: detection Touches src/Detection/ or src/Support/ (detectors, pattern tables, NFKC) ci GitHub Actions workflows tests Test suite changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scan time budget reads the CPU clock twice per pattern check

1 participant