Skip to content

perf(app): avoid full catalogue copies for model metadata lookups - #1927

Merged
JAORMX merged 1 commit into
mainfrom
perf/catalog-model-lookup
Sep 25, 2026
Merged

JAORMX merged 1 commit into
mainfrom
perf/catalog-model-lookup

Conversation

@JAORMX

@JAORMX JAORMX commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Replace full-catalogue copies in two single-model metadata lookups with a read-only binary search over the catalogue's existing sorted model slice.

  • Add Provider.Model(id) (Model, bool) using the already-imported sort.Search.
  • Use it only in catalogContextWindow and catalogModalities.
  • Preserve exact matching, provider aliases, empty/unknown fallbacks, metadata precedence, and defensive copies from Models() and InputModalities().
  • Add functional, immutability, zero-allocation, and app hot-path allocation regression tests, plus reproducible hit/miss benchmarks.

No new cache, dependency, engine/public SDK API, or user-facing semantic change.

Measured impact

Paired uncached internal/app CPU/heap profiles on the same baseline, with only the catalogue optimization toggled:

Measurement Before After
Cumulative allocated memory (approximately) 19.79 GB 2.1 GB
Package test execution 55.011 s 48.301 s

The allocation reduction is about 90%. This is cumulative allocation churn, not peak RAM, and is not a claim of 90% wall-time improvement. Timing is one paired profiled run; host load can affect it.

Median microbenchmarks (-benchtime=200ms -count=3):

Lookup Before After Bytes/op before → after
Context hit 41,145 ns 178.3 ns 40,960 → 0
Context miss 39,918 ns 132.4 ns 40,960 → 0
Modalities hit 38,095 ns 300.2 ns 41,008 → 48
Modalities miss 44,874 ns 162.1 ns 40,960 → 0

The remaining modality-hit allocation is the intentional defensive copy of that model's modalities.

Benchmark command:

go test ./internal/app -run '^$' -bench '^BenchmarkCatalogMetadata$' -benchmem -benchtime=200ms -count=3

Verification

All final gates passed on commit 824950d59b5a90004fa9b5b4578023c493009b0b:

  • task test GOPROXY=off
  • task lint
  • task test:race GOPROXY=off
  • Offline demo: tool calls, permission ask/approval, and successful results observed.
  • git diff --check

Additional proof:

  • New accessor tests observed red before implementation, then green.
  • Restoring the two old helper implementations makes all four app allocation guards fail; restoring the fix passes 50 repetitions under -race.
  • Spec, Standards, Test adequacy, and Domain reviews completed. The allocation-guard coverage finding was addressed and re-reviewed.

PANEL: ship_blockers=0 important=0 advisory=0 reviewer_failures=0

Scope

Routine: mechanical optimization of an immutable internal catalogue, with unchanged observable metadata behavior. No acceptance-plan or engine compatibility-snapshot changes are needed.

A separate GPT-6 Astra investigation recommended keeping further timer/recovery-worker and catalogue optimizations out of this PR. Those follow-ups should be independently measured.

Search the immutable sorted catalogue directly for context and modality metadata while preserving defensive list and modality copies.

Co-Authored-By: mecatl <[email protected]>
@JAORMX
JAORMX merged commit 25c60a0 into main Sep 25, 2026
36 checks passed
@JAORMX
JAORMX deleted the perf/catalog-model-lookup branch September 25, 2026 12:00
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Sep 25, 2026
2 tasks
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