Repository navigation
perf(detection): charge the scan budget per pass and cut monitor overhead - #63
Merged
Merged
Conversation
…variance in one pass
…attern The per-request scan budget read the worker thread CPU clock twice per pattern. It now reads it 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. The fallback scan opens a pass and charges after each pattern.
This was referenced Oct 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #62
Port of the reference fix in Guard-Core/guard-core#173. ScanTimeBudget takes injectable CPU and wall clocks and charges per pass: openPass reads the worker thread's CPU clock once, sliceCheck charges it only when the 2 ms wall slice (started with the request budget, so it spans values) has elapsed, and closePass in a finally makes the final charge; exhausted() stays arithmetic only, and the fallback scan opens a pass and charges after each pattern like the reference. The performance monitor iterates the window directly under its lock instead of snapshot copies, computes the variance with a compensated sum and no list allocation, and returns before sanitizing when no callback is registered. Tests cover the clock reads per pass and a long pass stopping within cap plus one slice. On a ~1.1 KB JSON-like body plus 10 headers detection went from 14.7 ms to 10.4 ms of CPU.