Skip to content

perf(scanner): speed up root-kind lookup on emit path#11

Open
ajayk wants to merge 1 commit into
perplexityai:mainfrom
ajayk:perf/root-kind-lookup
Open

perf(scanner): speed up root-kind lookup on emit path#11
ajayk wants to merge 1 commit into
perplexityai:mainfrom
ajayk:perf/root-kind-lookup

Conversation

@ajayk
Copy link
Copy Markdown

@ajayk ajayk commented May 23, 2026

Summary

newRootKindLookup returns a closure invoked once per emitted record. The previous implementation, on every call:

  1. Ran filepath.Abs even when the input was already absolute.
  2. Built r.Path + string(filepath.Separator) inside the inner loop for each root.
  3. Did a full linear scan over all roots, tracking the longest match with bestLen.

This PR pre-computes the path + separator prefix 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 skips filepath.Abs when the input is already absolute.

Benchmark (Apple M2 Pro, 64 lookups per op, 6 configured roots)

ns/op allocs/op
before ~11,700 0
after ~3,540 0

~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

  • New TestNewRootKindLookup covers 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.
  • BenchmarkNewRootKindLookup added for future regression tracking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant