perf: replace sort-based epssPercentile with O(n) linear scan#3316
Open
matiasinsaurralde wants to merge 1 commit intoanchore:mainfrom
Open
perf: replace sort-based epssPercentile with O(n) linear scan#3316matiasinsaurralde wants to merge 1 commit intoanchore:mainfrom
matiasinsaurralde wants to merge 1 commit intoanchore:mainfrom
Conversation
Finding max of a slice doesn't require sorting. The previous implementation called sort.Slice (O(n log n), 4 heap allocations) on every sort comparison. The linear scan is O(n) with zero allocations. It also removes a subtle mutation side-effect: sort.Slice was sorting the caller's EPSS slice in-place, silently modifying the match struct during output rendering. In practice EPSS slices are almost always length 0–1 (both paths remain O(1) for those), so the main value of this change is correctness (no mutation) and the allocation reduction on the multi-entry path. Benchmark (Apple M4): small (3 items): 100.6 ns/op → 2.2 ns/op (45x), 4 allocs → 0 large (100 items): 1131 ns/op → 62 ns/op (18x), 4 allocs → 0 Signed-off-by: Matías Insaurralde <matias@insaurral.de>
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
epssPercentilewas callingsort.Sliceto find the maximum value in a slice — O(n log n) with 4 heap allocations — when a single linear pass suffices.This replaces it with an O(n), zero-allocation loop.
Bonus fix:
sort.Slicesorted the caller's slice in-place, silently mutatinga.Vulnerability.EPSSduring output rendering as a sideeffect. The linear scan is pure (read-only).Benchmarks (Apple M4 Pro)
In practice, EPSS slices are length 0–1 per CVE (both implementations are already O(1) there), so the primary value of this change is the removal of the mutation side-effect and the allocation savings on any multi-entry path
Test plan
TestEPSSPercentileunit tests pass unchangedgrype/presenter/modelspassepss_bench_test.goFeel free to leave the benchmark out from the PR if it's not relevant