perf(scanner): speed up root-kind lookup on emit path#11
Open
ajayk wants to merge 1 commit into
Open
Conversation
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
newRootKindLookupreturns a closure invoked once per emitted record. The previous implementation, on every call:filepath.Abseven when the input was already absolute.r.Path + string(filepath.Separator)inside the inner loop for each root.bestLen.This PR pre-computes the
path + separatorprefix once per root at construction time, sorts roots longest-first (stable, so equal-length ties match the prior strict-greater tie-break), and returns on the first prefix hit. The inner closure also skipsfilepath.Abswhen the input is already absolute.Benchmark (Apple M2 Pro, 64 lookups per op, 6 configured roots)
~3.3x faster on the per-record emit path. Allocations were already 0 in both (escape analysis eats the inline concat), so the win is pure CPU from avoiding repeated string building and linear scans after the longest match.
Test plan
TestNewRootKindLookupcovers exact match, deep child match, longest-match tie-break, outside-all-roots, and the prefix-overlap-without-separator edge case.go test ./...— all packages pass.BenchmarkNewRootKindLookupadded for future regression tracking.