providers: a provider's model list reads the settings once, not three times per model (lml on Discord, Windows: 每次至少都得等五六秒…供应商或者额度列表还在加载) — each model's row asked for its image answer twice (ApplyImage, ImageOverride) and its reply limit once (ReplyLimit), and each ask parsed settings.json again, even inside a GET's hold, since filememo keeps its bytes, not the parse. providerInfo now reads the settings once and uses provider.ImageOverrideIn and Provider.ReplyLimitIn. This removes redundant work found while measuring lml's report; it does not explain the whole 5-6s, which needs their setup (see below).

Measured with a copy of the owner's config (23 providers, ~1500 models) under a sandbox HOME and the real server (magpie web, dead proxy): GET /api/providers on the Windows box 497-558ms → 148-214ms, on the Mac 442-525ms → 174-191ms; the JSON answer is byte-for-byte the same apart from the gateway port. Everything else the window waits on at open answered locally in under 0.6s on Windows, so the rest of a 5-6s wait is network (the usage cards wait for every vendor on a cold start, and on Usage's own open), not this.

TestProviderListReadsSettingsOnce (300 models, a big settings file, held as a GET is) takes 5.3s without the change and 41ms with it, and checks an image answer and a reply limit the user set are still said. go vet, GOOS=windows go build, GOOS=linux go build -tags nogui and go test -tags nogui ./... under a temp HOME pass.
This commit is contained in:
yetone
2026-10-07 01:58:10 +08:00
parent 54c3cb06fc
commit 9db5753ba3
4 changed files with 98 additions and 4 deletions
+9 -3
View File
@@ -495,15 +495,21 @@ func providerInfo(p provider.Provider, agents []agentUse) providerJSON {
if p.Account != nil && p.Account.Agent == "codex" {
most = catalog.Codex()
}
// read once for the list: read for each model, three times over, it
// was most of the Providers page's wait with many models (lml on
// Discord, Windows)
set := settings.Load()
named := func(m catalog.Model, on bool) modelJSON {
images := m.Images || catalog.SeesImages(m.ID)
if m.ImageInput != nil {
images = *m.ImageInput
}
own := images
images, _ = provider.ApplyImage(p.ID, m.ID, images, m.ImageInput)
_, imageSet := provider.ImageOverride(p.ID, m.ID)
j := modelJSON{ID: m.ID, Name: m.Name, Efforts: provider.EffortsOf(m), On: on, Context: p.WindowOf(m), Output: p.ReplyLimit(m), Listed: provider.ListedWindow(m), Max: m.MaxContext, Free: m.Free, Rate: m.Rate, RateWas: m.RateWas, Images: images, ImageSet: imageSet, Own: own}
said, imageSet := provider.ImageOverrideIn(set, p.ID, m.ID)
if imageSet {
images = said
}
j := modelJSON{ID: m.ID, Name: m.Name, Efforts: provider.EffortsOf(m), On: on, Context: p.WindowOf(m), Output: p.ReplyLimitIn(m, set), Listed: provider.ListedWindow(m), Max: m.MaxContext, Free: m.Free, Rate: m.Rate, RateWas: m.RateWas, Images: images, ImageSet: imageSet, Own: own}
if i := slices.IndexFunc(most, func(c catalog.Model) bool { return c.ID == m.ID }); j.Max == 0 && i >= 0 {
j.Max = most[i].MaxContext
}
@@ -0,0 +1,79 @@
package gui
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/filememo"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// lml on Discord (Windows, about ten providers): the Providers page took
// seconds to come up. Each model's row read the settings three times over
// (its image answer twice, its reply limit once), parsing settings.json
// again each time even while the request held it: with the owner's 23
// providers that was most of /api/providers (~0.5s on Windows). A
// provider's list now reads them once, and still says what they hold.
func TestProviderListReadsSettingsOnce(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
var models []string
for i := range 300 {
models = append(models, fmt.Sprintf("model-%d", i))
}
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: models}); err != nil {
t.Fatal(err)
}
// a big settings file, as a long-used magpie's is, makes each read
// show: some 20000 image answers of other providers' models
images := map[string]bool{"relay/model-7": true}
for i := range 20000 {
images[fmt.Sprintf("other-%d/some-model-%d", i%50, i)] = i%2 == 0
}
raw, err := json.Marshal(map[string]any{
"modelImages": images,
"modelOutputs": map[string]int{"relay/model-9": 64000},
})
if err != nil {
t.Fatal(err)
}
os.MkdirAll(filepath.Dir(settings.Path()), 0o755)
if err := os.WriteFile(settings.Path(), raw, 0o600); err != nil {
t.Fatal(err)
}
p, err := provider.Find("relay")
if err != nil {
t.Fatal(err)
}
// as a GET /api/* is served (held)
defer provider.Hold()()
defer filememo.Hold()()
providerInfo(*p, nil) // the first read of each file
start := time.Now()
info := providerInfo(*p, nil)
took := time.Since(start)
if took > 2*time.Second {
t.Errorf("one provider's 300 models took %v: the settings were read for each", took)
}
byID := map[string]modelJSON{}
for _, m := range info.Models {
byID[m.ID] = m
}
if m := byID["model-7"]; !m.Images || !m.ImageSet {
t.Errorf("the user's image answer is lost: %+v", m)
}
if m := byID["model-8"]; m.ImageSet {
t.Errorf("an image answer nobody gave: %+v", m)
}
if m := byID["model-9"]; m.Output != 64000 {
t.Errorf("the user's reply limit is lost: %+v", m)
}
}
+7 -1
View File
@@ -543,7 +543,13 @@ func setModelSame(ref, same string) (bool, error) {
// ImageOverride is the user's answer for whether pid's model takes images.
func ImageOverride(pid, model string) (bool, bool) {
v, ok := settings.Load().ModelImages[pid+"/"+model]
return ImageOverrideIn(settings.Load(), pid, model)
}
// ImageOverrideIn is ImageOverride from settings s already read: a list of
// many models reads them once, not for each (lml on Discord, Windows).
func ImageOverrideIn(s settings.Settings, pid, model string) (bool, bool) {
v, ok := s.ModelImages[pid+"/"+model]
return v, ok
}
+3
View File
@@ -1353,6 +1353,9 @@ func (p Provider) ReplyLimit(m catalog.Model) int {
return p.replyLimit(m, settings.Load())
}
// ReplyLimitIn is ReplyLimit from settings s already read.
func (p Provider) ReplyLimitIn(m catalog.Model, s settings.Settings) int { return p.replyLimit(m, s) }
func (p Provider) replyLimit(m catalog.Model, s settings.Settings) int {
output := m.Output
if output == 0 {