From f856d381ac822ee94f6e8120ca042dd23fbf62f1 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 05:51:21 +0800 Subject: [PATCH] provider: Host finds the first protocol spoken without building a list Host read Speaks' whole list to take the first protocol with an address. IsBedrock, IsKilo, IsCline, IsOpenCode and IsBedrockMantle each ask Host, for every provider on every poll. Building that list was 8 MB of what twenty polls of the Providers page allocated with real accounts. Speaks' rule now lives in speaking, an iterator. Speaks collects it, and Host stops at the first protocol with an address. The order and the Code Assist, Vertex and Gemini cases are unchanged. Test: TestHostBuildsNoList. For six providers it checks that Host equals the host of the first of Speaks() with an address (else Decide's), and that a look allocates nothing. With range p.Speaks() put back by hand it fails at 3 to 5 allocations a look. Run on mote at this tree: - gofmt, go vet, and GOOS=windows and GOOS=linux vet of internal/provider: ok. - TestHostBuildsNoList, TestNoAPIFindsTheProvidersAPIsOnce, and the Speaks, Vertex and Gemini tests alone, 3 times: ok. - Not run with this change: the whole ./internal/provider suite. It passed on mote with only the market commit, before this change was made. --- internal/provider/host_speaking_test.go | 35 +++++++++++++++++++++++++ internal/provider/provider.go | 26 +++++++++++------- 2 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 internal/provider/host_speaking_test.go diff --git a/internal/provider/host_speaking_test.go b/internal/provider/host_speaking_test.go new file mode 100644 index 000000000..fcf039844 --- /dev/null +++ b/internal/provider/host_speaking_test.go @@ -0,0 +1,35 @@ +package provider + +import "testing" + +// Host is the host of the first protocol the provider speaks, as it was +// when it read Speaks' list; it builds no list now: one for each look was +// 8 MB of what twenty polls of the Providers page allocated with real +// accounts (IsBedrock, IsKilo, IsCline and IsOpenCode each look). +func TestHostBuildsNoList(t *testing.T) { + for _, p := range []Provider{ + {ID: "a", Chat: "https://api.example.com/v1"}, + {ID: "b", Anthropic: "https://api.anthropic.example/v1"}, + {ID: "c", Responses: "https://r.example.com/v1", Anthropic: "https://a.example.com"}, + {ID: "d", Gemini: "https://generativelanguage.example.com/v1beta"}, + {ID: "e", Decide: "https://decide.example.com/v1"}, + {ID: "f"}, + } { + want := "" + for _, pr := range p.Speaks() { + if u := p.Base(pr); u != "" { + want = HostOf(u) + break + } + } + if want == "" && p.Decide != "" { + want = HostOf(p.Decide) + } + if got := p.Host(); got != want { + t.Errorf("%s: Host = %q, want %q", p.ID, got, want) + } + if n := testing.AllocsPerRun(20, func() { p.Host() }); n != 0 { + t.Errorf("%s: Host allocated %.0f times a look", p.ID, n) + } + } +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index ebb06da04..8af7739e0 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -1687,19 +1687,26 @@ var speaksLooked atomic.Int64 // Speaks lists the protocols the vendor serves natively, preferred first. func (p Provider) Speaks() []Protocol { speaksLooked.Add(1) + return slices.Collect(p.speaking) +} + +// speaking is Speaks one protocol at a time, for a look that wants only +// the first (Host): a list built for it was 8 MB of what twenty polls of +// the Providers page allocated with real accounts. +func (p Provider) speaking(yield func(Protocol) bool) { // a Google sign-in speaks Code Assist, and only that if p.Account != nil && p.Account.codeAssist != "" && !p.IsPlugin() { - return []Protocol{CodeAssist} + yield(CodeAssist) + return } - var out []Protocol for _, pr := range Protocols { - if p.Base(pr) != "" { - out = append(out, pr) + if p.Base(pr) != "" && !yield(pr) { + return } } // a plugin's Gemini models, beside what else it serves - if p.IsPlugin() && p.Account.codeAssist != "" { - out = append(out, CodeAssist) + if p.IsPlugin() && p.Account.codeAssist != "" && !yield(CodeAssist) { + return } // Factory's Gemini models, on generateContent. A model droid didn't // list stays on the other three (factoryAPIs); this is not one of them. @@ -1710,12 +1717,11 @@ func (p Provider) Speaks() []Protocol { // one). if p.IsVertex() { if p.vertexBase() != "" { - out = append(out, Gemini) + yield(Gemini) } } else if p.FactoryGemini() || p.Gemini != "" { - out = append(out, Gemini) + yield(Gemini) } - return out } // FactoryGemini is Factory's sign-in, whose Gemini models are asked on its @@ -1850,7 +1856,7 @@ func (p Provider) Host() string { if p.Account != nil && p.Account.moved { return p.Account.wasHost // not plugin:// } - for _, pr := range p.Speaks() { + for pr := range p.speaking { if u := p.Base(pr); u != "" { return HostOf(u) }