Files
magpie/internal/provider/model_prefs_shared_test.go
T
yetone 05f6ca39b3 provider, gui: settings only read go through the shared copy
settings.Load clones the whole Settings, every provider's model prefs
included. CompactsOf, OutputsOf, ModelNames, ModelEfforts,
settings.SavedAddr and the GUI's providerInfoWith only read it, and
providerInfoWith took a copy for each provider: 16 MB of what twenty
window polls allocated in a real-account sandbox, more with each
model a user has set prefs for.

They read settings.Shared() now. ModelEfforts hands back a clone of
the slice it finds, so a caller's change never reaches the shared
settings.

TestModelPrefsReadTheSharedSettings: 3000 other models' prefs; the
values are right, a caller's change to ModelEfforts' list doesn't
reach the settings, and each look allocates at most a tenth of
Load's. Red on the old code: ~6058 allocations a look against 6056
for a Load.

Run at this head on mote: go vet (darwin, GOOS=windows, GOOS=linux
without the gui package) ok; go test ./internal/settings and
./internal/provider ok; ./internal/gui red only in
TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
TestMainWindowCorners (#3949), both red on main.
2026-11-01 04:05:38 +08:00

71 lines
2.4 KiB
Go

package provider
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/settings"
)
// The editor's looks at what the user set on a provider's models (names,
// efforts, compact and reply limits) and the gateway's saved address read
// the settings every caller shares, not a copy of their own: with a few
// thousand models named, a copy each was most of what they allocated. What
// they return is the caller's to change, and doesn't reach the settings.
func TestModelPrefsReadTheSharedSettings(t *testing.T) {
isolate(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"))
s := settings.Load()
s.ModelNames, s.ModelEfforts = map[string]string{}, map[string][]string{}
s.ModelCompacts, s.ModelOutputs = map[string]int{}, map[string]int{}
for i := range 3000 {
k := fmt.Sprintf("other/model-%d", i)
s.ModelNames[k], s.ModelEfforts[k] = fmt.Sprintf("Model %d", i), []string{"low", "high"}
s.ModelCompacts[k], s.ModelOutputs[k] = 1000+i, 2000+i
}
s.ModelNames["p/m"], s.ModelEfforts["p/m"] = "Mine", []string{"low", "high"}
s.ModelCompacts["p/m"], s.ModelOutputs["p/m"] = 50000, 8000
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
old := time.Now().Add(-time.Hour)
if err := os.Chtimes(settings.Path(), old, old); err != nil {
t.Fatal(err)
}
p := Provider{ID: "p"}
if got := p.ModelNames()["m"]; got != "Mine" {
t.Fatalf("ModelNames: %q", got)
}
if got := CompactsOf("p")["m"]; got != 50000 {
t.Fatalf("CompactsOf: %d", got)
}
if got := OutputsOf("p")["m"]; got != 8000 {
t.Fatalf("OutputsOf: %d", got)
}
es := p.ModelEfforts()["m"]
es[0] = "changed"
if again := p.ModelEfforts()["m"]; len(again) != 2 || again[0] != "low" {
t.Fatalf("a caller's change reached the settings: %v", again)
}
copied := testing.AllocsPerRun(5, func() { settings.Load() })
for name, look := range map[string]func(){
"ModelNames": func() { p.ModelNames() },
"ModelEfforts": func() { p.ModelEfforts() },
"CompactsOf": func() { CompactsOf("p") },
"OutputsOf": func() { OutputsOf("p") },
"SavedAddr": func() { settings.SavedAddr() },
} {
if n := testing.AllocsPerRun(10, look); n > copied/10 {
t.Errorf("%s: %.0f allocations, against %.0f for a copy of the settings", name, n, copied)
}
}
}