Skip to content

perf(context): Optimize subsystem prefix routing - #169

Merged
JordanCoin merged 1 commit into
JordanCoin:mainfrom
reneleonhardt:fix/context-lexical-routing
Sep 3, 2026
Merged

perf(context): Optimize subsystem prefix routing#169
JordanCoin merged 1 commit into
JordanCoin:mainfrom
reneleonhardt:fix/context-lexical-routing

Conversation

@reneleonhardt

Copy link
Copy Markdown
Contributor

perf(context): Optimize subsystem prefix routing

What does this PR do?

  • Reuses matchSubsystemRoutes for context routing, keeping scoring, ordering, and top-K selection in one place.
  • Builds a sorted normalized inventory index and uses binary-searched prefix ranges so subsystem routing visits only matching files.
  • Replaces repeated subsystem-prefix scans and per-directory prefix materialization with index construction plus matched candidates.
  • Skips duplicate configured prefixes while preserving prior path order, including bounded top-K results on case-insensitive platforms.
  • Keeps exact-path and basename resolution ahead of bounded subsystem suggestions.

This reduces subsystem routing work without changing its matching or confidence semantics.

Type of change

  • Bug fix
  • New feature
  • New language support
  • Documentation
  • Other (performance and maintainability)

Checklist

  • Tested locally with Go 1.27.1: go test ./...
  • Verified focused routing tests, including ordering, top-K, case handling, duplicate prefixes, and prefix boundaries.
  • Verified focused routing tests under -race.
  • Verified with go vet ./....
  • Updated the relevant unit coverage for shared route scoring and prefix indexing.

Additional notes

Routing remains inventory-only, bounded, deterministic, and free of semantic search or unbounded expansion.

On a stress fixture with 40 matched subsystems, five prefixes each, and 20,000 files, the sorted-range resolver measured 4.3 ms versus 14.1 ms for the previous prefix-map implementation (~3.3× faster), with 61% fewer allocated bytes and 80% fewer allocations.

Build a normalized inventory index and resolve subsystem paths through binary-searched component ranges. Reuse shared route scoring, preserve deterministic path order and case-insensitive top-K behavior, and cover duplicate prefixes and boundaries.

Copy link
Copy Markdown
Owner

Reviewed at 6f6e9ce. The duplication fix and the sorted-range resolver both hold up, and I went after the ordering semantics hard because binary-searched prefix ranges are exactly where this kind of optimization usually changes behavior quietly.

Behavior is identical to main. Built binaries from b713d5f and 6f6e9ce and ran them against a fixture designed to break the prefix boundary — overlapping subsystems, a duplicate prefix listed twice, scanner/deep/ nested under scanner/, a scannerx/ sibling, and a file named scanner.go sitting next to the scanner/ directory:

"why is parsing slow?"              SAME
"parsing and scanner/alpha.go"      SAME
"parsing scanner.go"                SAME
"look at scannerx"                  SAME

scanner.go is the interesting one — . (0x2E) sorts below / (0x2F), so a naive range scan from prefixKey would sweep it up. The separate exact-key range plus sort.Search on prefixKey + "/" gets it right, and scannerx/x.go is correctly excluded too.

The case-insensitive path preserves emission order. This is the subtle part: sortedPaths is sorted by folded key for binary search, but the old code emitted in raw-path sort.Strings order. Stashing order from a pre-sort and re-sorting matches by it does reproduce that. Verified directly:

files:  Scanner/Zeta.go scanner/alpha.go SCANNER/Beta.go
        scanner/deep/Nu.go Scanner/Deep/delta.go scanner/mu.go
prefix "scanner", caseInsensitive=true
got:    [SCANNER/Beta.go Scanner/Deep/delta.go Scanner/Zeta.go
         scanner/alpha.go scanner/deep/Nu.go scanner/mu.go]
        == sort.Strings(raw)  ✓

contextSubsystemMatches now delegates to matchSubsystemRoutes. That closes the duplicated-scoring finding from #167 properly — one scorer, one tie-break, and the Index field is a cleaner way to bridge it than what I'd have written.

go vet ./... clean, go test ./... at baseline (only the three root-permission failures).

On the red CI

Both failing legs are in packages this diff cannot reach, and codemap/cmd — the only package it touches — passed on both of them:

Locally on this branch: TestDebounce 10/10 pass, TestRustGraphContextHandlersDisclosePartialCoverage 5/5 pass, cmd 3/3 pass. I can't re-run the jobs myself (rerun-failed-jobs returns 403 for me), so I'm merging on the package-isolation evidence rather than a green re-run — flagging that explicitly rather than implying the run went green.

Two leftovers I'm still not blocking on: the redundant early duplicate check at cmd/context_routing.go:124 (duplicate && !caseInsensitive keys on path rather than pathKey, so it's inert when case-insensitive and identical to the check below it when not), and the case-insensitive forPrefix branch materializing all matches before sorting even when topK is small. Neither affects correctness.

Merging. Nice result on the benchmark, and thanks for taking the reuse half of the note as seriously as the perf half.


Generated by Claude Code

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.

2 participants