Repository navigation
Detectors allocate a findings slice only when they find something - #69
Merged
Merged
Conversation
Registry.DetectAll preallocated a slice sized to the whole registry on every call, and the Stripe detector preallocated room for four findings, even though almost every scanned line finds nothing. Both now start from a nil slice and append on a match. Findings, order and fields are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SUSTAPLE117
added this pull request to stack #71
October 2, 2026 20:52
SUSTAPLE117
marked this pull request as ready for review
October 2, 2026 20:54
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The allocation optimization preserves behavior and is covered by focused regression tests.
Review effort: Balanced
Findings: None
What changed in this PR
Reduces detector allocations on finding-free input without changing finding order or metadata.
Changes:
- Lazily allocate registry and Stripe finding slices.
- Add allocation, ordering, and benchmark coverage.
- Handle allocation tests correctly under race instrumentation.
| File | Description |
|---|---|
pkg/detector/detector.go |
Lazily initializes aggregate findings. |
pkg/detector/stripe_key.go |
Lazily initializes Stripe findings. |
pkg/detector/detector_test.go |
Tests allocations and ordering; adds benchmark. |
pkg/detector/race_on_test.go |
Identifies race-enabled tests. |
pkg/detector/race_off_test.go |
Identifies non-race tests. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
fproulx-boostsecurity
approved these changes
Oct 5, 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.
Summary
Registry.DetectAllruns once per scanned line, and it started every call with a findings slice sized to the whole registry (16 slots in the default registry). The Stripe detector did the same with room for four findings. Almost every line finds nothing, so nearly all of that was allocated for nothing.Both now start from a nil slice and append only when something matches. Returning nil for "no findings" is what callers already handle: every one ranges over the result, appends it, or checks
len. Findings, their order and their fields don't change.Detector audit (default registry)
Registry.DetectAllmake(0, len(r.detectors))per callmake(0, 4)per callvar findingsvar findingsvar findingsvar findingsvar findingsvar findingsvar findingsvar findingsmake(0, len(matches))make(0, len(matches))make(0, len(matches))make(0, len(matches))make(0, len(matches))make(0, len(ws)+len(app))make(0, len(matches))I also measured it:
testing.AllocsPerRunover each detector'sDetecton finding-free lines showed only stripe-key allocating (1 alloc per call). After the change, every detector andDetectAllallocate nothing on those lines.Tests
TestRegistry_DetectAllCleanLineAllocatesNothing:DetectAllon a set of finding-free lines (including near-miss prefixes such assk_,ghp_,AKIA) allocates 0 times. It is skipped under-race, because race instrumentation changes escape analysis.TestRegistry_DetectAllKeepsRegistrationOrder: findings from several detectors still come back in registration order, with probe and line metadata set.Benchmark
BenchmarkRegistry_DetectAllCleanLines(default registry, 5 finding-free lines per op, Apple M1 Pro, n=6):Time per op is unchanged within noise (91.7µs vs 92.6µs; regex matching dominates).
Checks
go test ./... -cover,go test -race -count=1 ./...andgolangci-lint run(0 issues) all pass.🤖 Generated with Claude Code