gui: a provider's row reads the vendor's list once
providerInfoWith read p.Available() four times for one row: for the picks (Picks), for what is exposed (Exposed, twice: the exposed set and the picks the list doesn't know) and for the newest of each family. Each read builds the list whole with its names, levels and plugin models. In a profile of v0.1.2432 against the owner's real config (20 rounds of the window's polls), providersState was 1.34 GB of 2.62 GB allocated, Exposed 390 MB, Picks 155 MB and the row's own Available 146 MB of it. Provider gains ExposedOf(avail) and PicksOf(avail), Exposed and Picks of a list the caller has read already; Exposed and Picks call them with p.Available(), so every other caller is unchanged. The row reads the list once and hands it to both. TestProviderInfoReadsTheListOnce: a 2000-model catalog provider with three picks, one of them unlisted, read the way the page's request reads it (provider.Hold and filememo.Hold). The row's Chosen equals Picks(), its exposed count equals len(Exposed()) and the unlisted pick goes first. A row takes 28.6 MB against the list's 8.0 MB (each model's own price, window and protocol look-ups besides); on the old code 54.4 MB, red at the 5x bar. Run on mote at 27401edd6 (go test -timeout 45m): internal/provider ok 161s. internal/gui: three reds, each already filed and failing the same on main: TestSettingsSaveKeepsWhatItDoesNotSend (#4514), TestMainWindowCorners (#3949), TestColdTurnsTaken (#4510). go vet darwin and windows ok, gofmt -l clean.
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package gui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// A provider's row on the Providers page reads the vendor's list once:
|
||||
// it was read four times (the picks, what is exposed twice, the newest
|
||||
// of each family), each built whole with its names and levels, for each
|
||||
// provider on every poll. What the row says is the same.
|
||||
func TestProviderInfoReadsTheListOnce(t *testing.T) {
|
||||
h := sandboxHome(t)
|
||||
var b strings.Builder
|
||||
b.WriteString(`{"relayco": {"models": {`)
|
||||
for i := range 2000 {
|
||||
fmt.Fprintf(&b, `"m-%d": {"id":"m-%d","name":"M %d"},`, i, i, i)
|
||||
}
|
||||
b.WriteString(`"x": {"id":"x"}}}}`)
|
||||
os.MkdirAll(h+"/.cache/magpie", 0o755)
|
||||
if err := os.WriteFile(catalog.CachePath(), []byte(b.String()), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
catalog.Reset()
|
||||
t.Cleanup(catalog.Reset)
|
||||
p := provider.Provider{ID: "r", Name: "R", Key: "k", Chat: "http://127.0.0.1:1/v1", Catalog: "relayco", Models: []string{"m-3", "m-1", "gone"}}
|
||||
if err := provider.Save(p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := time.Now().Add(-time.Hour)
|
||||
os.Chtimes(provider.Path(), old, old)
|
||||
os.Chtimes(catalog.CachePath(), old, old)
|
||||
if len(p.Available()) < 2000 {
|
||||
t.Fatalf("the catalog's models not listed: %d", len(p.Available()))
|
||||
}
|
||||
|
||||
// as a request to the page reads it (held)
|
||||
defer provider.Hold()()
|
||||
defer filememo.Hold()()
|
||||
got := providerInfo(p, nil)
|
||||
if !slices.Equal(got.Chosen, p.Picks()) {
|
||||
t.Fatalf("chosen %v, picks %v", got.Chosen, p.Picks())
|
||||
}
|
||||
if got.Exposed != len(p.Exposed()) || got.Models[0].ID != "gone" {
|
||||
t.Fatalf("exposed %d of %d, first %+v", got.Exposed, len(p.Exposed()), got.Models[0])
|
||||
}
|
||||
|
||||
bytesPer := func(f func()) uint64 {
|
||||
f()
|
||||
var a, b runtime.MemStats
|
||||
runtime.ReadMemStats(&a)
|
||||
for range 5 {
|
||||
f()
|
||||
}
|
||||
runtime.ReadMemStats(&b)
|
||||
return (b.TotalAlloc - a.TotalAlloc) / 5
|
||||
}
|
||||
list := bytesPer(func() { p.Available() })
|
||||
row := bytesPer(func() { providerInfo(p, nil) })
|
||||
t.Logf("a row %d bytes, the list %d", row, list)
|
||||
// each model's own look-ups besides (its price, window, protocols)
|
||||
// are about 2.5 lists here; reading it four times over made 6.8
|
||||
if row > list*5 {
|
||||
t.Fatalf("a row took %d bytes, the list %d: read more than once", row, list)
|
||||
}
|
||||
}
|
||||
@@ -651,12 +651,15 @@ func providerInfo(p provider.Provider, agents []agentUse) providerJSON {
|
||||
// providerInfoWith is providerInfo with the Claude Desktop numbers read
|
||||
// once for the whole list (desktopNumbers).
|
||||
func providerInfoWith(p provider.Provider, agents []agentUse, desktop map[string]string) providerJSON {
|
||||
// the vendor's list read once for the picks, what is exposed and the
|
||||
// newest of each family: each read builds it whole
|
||||
avail := p.Available()
|
||||
out := providerJSON{
|
||||
ID: p.ID, Name: p.Name, Icon: p.Icon, Preset: p.Preset, Host: p.Host(),
|
||||
Chat: p.Chat, Responses: p.Responses, Anthropic: p.Anthropic, Gemini: p.Gemini, Decide: p.Decide, BaseAPI: p.BaseAPI, ModelTest: p.ModelTest(), TestsAs: p.TestClients(), DecideTest: p.AsksDecideModels(), MarksKinds: p.MarksKinds(),
|
||||
Catalog: p.Catalog, Website: p.Website, KeysURL: p.KeysURL,
|
||||
Proxy: p.Proxy, AccountProxies: p.AccountProxies, AccountModels: p.AccountModels, AccountCaps: p.AccountCaps, AccountWindowCaps: p.AccountWindowCaps, AccountNames: p.AccountNames, Headers: p.Headers, ExtraBody: p.ExtraBody, ReplyHeaders: p.ReplyHeaders, Searches: p.Searches, Cline: p.ClinePinnable(), PinUpstream: p.PinUpstream, Unredacted: p.Unredacted, CountFree: p.CountFree, InsecureTLS: p.InsecureTLS, TakesInsecureTLS: p.TakesInsecureTLS(), LimitRestOff: p.LimitRestOff, NativeCompaction: p.NativeCompaction, CarriesCompaction: p.CarriesCompaction(), PassUserAgent: p.PassUserAgent, OwnClient: p.OwnClient(), AuthHeader: p.AuthHeader, BothAuth: p.BothAuth(), BalanceURL: p.BalanceURL, BalancePath: p.BalancePath, BalanceMethod: p.BalanceMethod, ModelsURL: p.ModelsURL,
|
||||
Ready: p.Ready(), Chosen: p.Picks(), Models: []modelJSON{}, Agents: []providerAgent{},
|
||||
Ready: p.Ready(), Chosen: p.PicksOf(avail), Models: []modelJSON{}, Agents: []providerAgent{},
|
||||
Fallback: p.Fallback, Routing: p.Routing, Sink: p.Sink, Affinity: p.Affinity, KeepLogin: p.KeepLogin, KeepLoginAs: p.KeepLoginAs, SwitchOnly: p.SwitchOnly, CapStop: p.CapStop, CapCareful: p.CapCareful, Unlisted: p.Unlisted, NoModels: p.ServesNone(), BestInFamily: p.BestInFamily && len(p.Models) == 0 && !p.NoModels, Off: p.Off, Contexts: p.Contexts,
|
||||
MaxConcurrency: p.MaxConcurrency, PluginConcurrency: p.PluginConcurrency(), PriceRate: p.PriceRate,
|
||||
RateWindows: p.RateWindows, RateNow: rateNowOf(p, time.Now()),
|
||||
@@ -786,10 +789,10 @@ func providerInfoWith(p provider.Provider, agents []agentUse, desktop map[string
|
||||
}
|
||||
}
|
||||
exposed := map[string]bool{}
|
||||
for _, m := range p.Exposed() {
|
||||
exposedList := p.ExposedOf(avail)
|
||||
for _, m := range exposedList {
|
||||
exposed[m.ID] = true
|
||||
}
|
||||
avail := p.Available()
|
||||
newest := map[string]bool{}
|
||||
for _, m := range p.NewestInFamily(avail) {
|
||||
newest[m.ID] = true
|
||||
@@ -897,7 +900,7 @@ func providerInfoWith(p provider.Provider, agents []agentUse, desktop map[string
|
||||
out.Deciders = &deciders
|
||||
}
|
||||
// picks the vendor list does not know go first, so they are visible
|
||||
for _, m := range p.Exposed() {
|
||||
for _, m := range exposedList {
|
||||
if !seen[m.ID] {
|
||||
out.Models = append([]modelJSON{named(m, true)}, out.Models...)
|
||||
}
|
||||
|
||||
@@ -1527,7 +1527,16 @@ func (p Provider) Exposed() []catalog.Model {
|
||||
if p.ServesNone() || p.waitsForPicks() {
|
||||
return nil
|
||||
}
|
||||
avail := p.Available()
|
||||
return p.ExposedOf(p.Available())
|
||||
}
|
||||
|
||||
// ExposedOf is Exposed of avail, p.Available() as the caller has already
|
||||
// read it: an editor's look at a provider read the vendor's list four
|
||||
// times over, each built afresh with its names and levels.
|
||||
func (p Provider) ExposedOf(avail []catalog.Model) []catalog.Model {
|
||||
if p.ServesNone() || p.waitsForPicks() {
|
||||
return nil
|
||||
}
|
||||
byID := make(map[string]catalog.Model, len(avail))
|
||||
for _, m := range avail {
|
||||
byID[m.ID] = m
|
||||
@@ -1632,6 +1641,15 @@ func (p Provider) Picks() []string {
|
||||
return p.withNewlyListed(p.Models, p.Available())
|
||||
}
|
||||
|
||||
// PicksOf is Picks of avail, p.Available() as the caller has already read
|
||||
// it (ExposedOf).
|
||||
func (p Provider) PicksOf(avail []catalog.Model) []string {
|
||||
if len(p.Models) == 0 {
|
||||
return p.Models
|
||||
}
|
||||
return p.withNewlyListed(p.Models, avail)
|
||||
}
|
||||
|
||||
// withNewlyListed is picks with the models the vendor has listed since
|
||||
// they were saved put first, when they held every model listed then. A
|
||||
// Claude account whose user picked every model (Select all, or the TUI's
|
||||
|
||||
Reference in New Issue
Block a user