provider: Served is shared while a request holds the catalog, not copied for each look (#3725)

Served built a fresh copy of the whole catalog on every call: the
unlisted groups' entries plus every provider entry. An Entry is ~1.3KB,
and a request looks at Served from outputLimit, userOutput, askedOutput
and windowIn, so with 12 providers of 20 models each look copied ~380KB
and a request ~800KB, all garbage by the reply.

Served now goes through heldFrom like providerEntries: one slice, built
once while a request holds the catalog and kept across requests until a
file it is built from changes. It is clipped, so a caller's append makes
its own copy and never reaches the next caller. The four gateway loops
take each entry by pointer instead of copying 1.3KB a step. Every
caller of Served (gateway, gui, cli, tui, agent, provider) was read for
writes into the slice or its entries; all only read.

Measured on BenchmarkZZIO (12 providers x 20 models, an 8KB request,
not committed): 1.57MB allocated per request before, 640KB after.

TestServedSharedWhileHeld: under Hold two calls give the same backing
entry, and an append to one doesn't show in the next. Red on the old
code (a new copy each call), green now.

Run at f2aa7fe34 (origin/main plus this change): gofmt -l clean on
internal/gateway and internal/provider; GOOS=linux and GOOS=windows go
vet -tags nogui ./internal/gateway ./internal/provider ok; go test
-count=1 -timeout 45m ./internal/provider (skipping
TestUnreadLoginsWriteErrorsKeepCredentials, #3632) ./internal/gateway
./internal/gui ./internal/cli ./internal/gwmw all ok.
This commit is contained in:
yetone
2026-10-27 01:55:25 +08:00
parent b3b0e1649f
commit 1360689ef2
4 changed files with 41 additions and 7 deletions
+3 -1
View File
@@ -1428,7 +1428,9 @@ func windowIn(served func() []provider.Entry, p provider.Provider, model string)
if n := p.Contexts[model]; n > 0 {
return n
}
for _, e := range served() {
cat := served()
for i := range cat {
e := &cat[i]
if e.Group == "" && e.Provider.ID == p.ID && (e.Model == model || e.ID == model) && e.Context > 0 {
return e.Context
}
+9 -3
View File
@@ -16,7 +16,9 @@ import (
// known: its catalog entry's (the user's setting, else the vendor's list
// or models.dev).
func outputLimit(p provider.Provider, model string) int {
for _, e := range provider.Served() {
cat := provider.Served()
for i := range cat {
e := &cat[i] // an Entry is ~1.3KB, the catalog hundreds
if e.Group == "" && e.Provider.ID == p.ID && (e.Model == model || e.ID == model) && e.Output > 0 {
return e.Output
}
@@ -32,7 +34,9 @@ func outputLimit(p provider.Provider, model string) int {
// as max_tokens times the price turned every turn away, and the field the
// editor says caps max_tokens did nothing (YaCo on Discord).
func userOutput(p provider.Provider, model string) int {
for _, e := range provider.Served() {
cat := provider.Served()
for i := range cat {
e := &cat[i] // an Entry is ~1.3KB, the catalog hundreds
if e.Group == "" && e.Provider.ID == p.ID && (e.Model == model || e.ID == model) && e.OutputSet && e.Output > 0 {
return e.Output
}
@@ -48,7 +52,9 @@ func userOutput(p provider.Provider, model string) int {
// turn ran out of, Codex showing "Incomplete response returned, reason:
// max_output_tokens" (#1792, tensornull).
func askedOutput(p provider.Provider, model string) int {
for _, e := range provider.Served() {
cat := provider.Served()
for i := range cat {
e := &cat[i] // an Entry is ~1.3KB, the catalog hundreds
if e.Group == "" && e.Provider.ID == p.ID && (e.Model == model || e.ID == model) {
return e.PublishedOutput()
}
+22
View File
@@ -39,6 +39,28 @@ func groupHeldFixture(t *testing.T) {
Changed()
}
// A request asks Served for each model it may go to (the reply's length
// twice, the window), and each was a copy of the whole catalog: ~380KB a
// request with 12 providers of 20 models (#3725). While a request holds
// the catalog it is one, shared for reading.
func TestServedSharedWhileHeld(t *testing.T) {
groupHeldFixture(t)
release := Hold()
defer release()
first := Served()
if len(first) == 0 {
t.Fatal("nothing served")
}
if again := Served(); &again[0] != &first[0] {
t.Fatal("Served was copied again under one Hold")
}
// clipped: a caller's append is its own
grown := append(first, Entry{ID: "added"})
if &grown[0] == &first[0] || len(Served()) != len(first) {
t.Fatal("an append to Served reached the next caller's")
}
}
// A request to a group asks for the catalog's groups several times —
// Resolve's Catalog, and Served for the members' images, the reply's
// length and the window — and each built every group's entry again, from
+7 -3
View File
@@ -2467,10 +2467,14 @@ func unlistedByGroups(entries []Entry) map[string]bool {
}
// Served is the catalog with the unlisted providers' models as well: every
// model a routing group can be made of, or a request can name.
// model a routing group can be made of, or a request can name. Like
// providerEntries it is shared, only for reading: a request asks it for
// each model it may go to, and a copy each time was ~380KB with 12
// providers of 20 models (#3725).
func Served() []Entry {
entries := providerEntries()
return append(slices.Clone(groupEntries(entries)), entries...)
return heldFrom("served", providerEntries(), func(entries []Entry) []Entry {
return slices.Clip(append(slices.Clone(groupEntries(entries)), entries...))
})
}
// Unlisted are the models Served has and Catalog doesn't: those of the