Repository navigation
perf(registry): cut avoidable per-candidate cost in the provider scan - #1340
Conversation
Autopilot eligibility read the clock for every candidate of every request. It now reads it only for a provider holding a matching control grant. Selection ranks the projected pool in place instead of copying each candidate on every pass, and Project takes the forecast and cost breakdown by pointer. Candidate arena chunks hold 22 candidates (was 20), which fills the allocator's 32 KiB size class. Routing outcomes are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Threat model review — advisoryReviewed head Review not completed for this head. Previous findings are superseded, not confirmed resolved. Models: budgeted Sonnet / selective Opus / selective Sol 6.1. Findings require human validation; this review never requests changes or blocks merging. Coverage: 0/0 source units; cross-file integration pending. 0 selected depth batch(es) pending. All threat definitions indexed; relevant full definitions selected. Unchanged callers are not scanned. Cost this run: $0.0000 reported, $0.0000 reserved with unknown cost; 0 paid request(s), 0 reused batch(es), 0 provider cache-hit tokens. Saved findings and earlier report · Historical findings are not confirmed resolved. |
|
@codex review this |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Summary
The coordinator reorganization (#1324) made the per-request provider scan slower:
BenchmarkReserveProviderEx_350x2went from about 227 µs to 392 µs. This PR removes three avoidable costs on that path and recovers roughly a quarter of the regression. Routing outcomes are unchanged.It is a partial fix. The rest of the gap comes from the detached-policy design itself and is described under "Notes for reviewers".
Linked issue
None. Follow-up to #1324.
What changed
time.Now(). After it, the clock was evaluated up front as an argument, up to three times per candidate.Lease.Activeis now split intograntMatchesplus the expiry comparison, and the per-request checks (RoutingBlocked,LegacyChangesBlocked,OrdinaryAllowed) take the clock as a function and call it only when a matching grant exists.Rank,ChooseandaffinityWinnercopied each 80-byte candidate on every pass. They now index the pool, andProjecttakes the forecast and cost breakdown by pointer.Candidategrew from 1,416 to 1,448 bytes, which pushed a 20-candidate chunk from the 28,672-byte size class into the 32,768-byte one and wasted 12% of every chunk.ChunkSizeis now 22, the most candidates that fit 32 KiB. A test fails when the struct size changes enough to need retuning.Before
Blue is the unchanged scan; red is avoidable per-candidate cost.
After
Green is the same decision at lower cost. Winner, runner-up, near-tie size and selection path are identical for every pool.
Benchmarks
Apple M3 Ultra, Go 1.25.0,
GOMAXPROCS=12. Medians of 6 runs, master and this branch interleaved. "Before #1324" is48d2be45.d3de4b4aBenchmarkReserveProviderEx_350x2BenchmarkFleetReserveProviderExBenchmarkRequestPathSerialBenchmarkFleetQuickCapacityCheckBenchmarkSelectRoutingCandidate/providers=350BenchmarkSelectRoutingCandidate/providers=32BenchmarkRequestPathParallelBenchmarkReserveProviderEx_350x2allocates 435 KB in 19 allocations per reservation, down from 475 KB in 20.Test plan
go test -race -p 1 -count=1 ./coordinator/tests/registry/...— all 8 packages pass.go test -p 1 -count=1 ./coordinator/...— 91 packages pass, including the layout guardTestCoordinatorTestsAreIsolated. PostgreSQL-backed tests skipped (noDATABASE_URL).gofmt -l coordinatorempty;go vet ./coordinator/...clean;git diff --checkclean.python3 scripts/docs-impact-check.py --base origin/master— coverage OK.TestAutopilotRoutingChecksSkipClockWithoutMatchingGrantfails if any routing check reads the clock without a matching grant (five provider states, both model kinds).TestAutopilotRoutingChecksHonorGrantExpirypins that a matching grant still stops managing the provider at exactly its expiry instant.TestCandidateArenaChunkFillsLargestSmallSizeClassfails atChunkSize = 20.TestRankKeepsFiniteBestWhenLaterRankIsNaNfails if the best-rank scan usesmin, which adopts NaN.golangci-lintwas not available locally; this relies on CI's Coordinator Lint.make docs-checkwas run on a partial checkout (nolanding/, submodules not initialized). Its only reported problems were paths under those; this relies on CI's Docs Lint.Refactor pass
A dedicated refactor subagent reviewed the first working version, as
AGENTS.mdrequires. It made these behaviour-preserving changes, and the validation above was rerun on the combined result:Lease(activeNow, besideActive) and renamedmanagedAttomanagedNow, becauseAtelsewhere in the codebase means "takes an instant".ChunkSizecomment and tightened three test and doc comments.It left
Rank,Choose,affinityWinnerand the call-site edits unchanged and found no behavioural risk.Components touched
Protocol / interface changes
Two internal Go signatures changed:
autopilotstate.State.OrdinaryAllowedtakesnow func() time.Time, andselection.Projecttakes*forecast.Estimateand*cachepolicy.ServiceBreakdown.Documentation impact
docs/architecture/routing.md, one sentence indocs/architecture/model-autopilot.md, a row indocs/developer/navigation.md, and aCHANGELOG.mdentry.Notes for reviewers
forecast.Evidenceis 408 bytes,performance.CalibrationEvidence248) that are built and copied per candidate, where the old code read snapshot fields directly. The largest pieces are the first-content estimate (3 µs → 18 µs per reservation), candidate build (7 → 19 µs) and the first-content snapshot fill (9 → 20 µs). Closing that means the registry no longer re-projecting per-candidate state by value, for example pointer-based inputs toforecast.Evaluate,deadline.BoundSlotsand theQualifiedcatalogs, plus their test call sites. I left that for a separate decision because it changes the component contracts refactor(coordinator): reorganize domain ownership and storage backends #1324 introduced.selection.Selectstill zeroes a 512-entry stack array on every call (about 0.7 µs), which dominates the single-provider micro-benchmark. It is under 1% of a fleet-sized reservation, so this PR leaves it.🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.