GroupFinder reads every provider's models once for many lookups, but
each lookup's membersIn built entriesBy, a map over the whole list,
again. The Providers page looks up every group of every agent on each
poll: with real accounts, 40 polls spent 23.2 MB in GroupFinder, 13.6
MB of it in that index.
The finder makes the index with the list, and membersFound takes it;
membersOff, for one lookup, still makes its own. Members are unchanged.
Run on mote (macOS), at this tree:
- gofmt clean; go vet ./internal/provider for darwin, windows and linux.
- TestGroupFinderIndexesTheListOnce alone 3/3 ok (1744 bytes a lookup,
2000 models).
- Broken by hand (index made per lookup again): "a group looked up
again cost 198576 bytes: the list of 2000 models was indexed again".
- go test ./internal/provider ./internal/agent ./internal/gateway: ok.
- go test ./internal/gui: red only on TestSettingsSaveKeepsWhatItDoesNotSend
(#4514) and TestMainWindowCorners (#3949), as before.
- Real-account sandbox, 40 window polls against v0.1.2505's tree:
GroupFinder 23.2 -> 12.6 MB; /api/state and drift output
byte-identical; phys_footprint_peak 135 MB.
Not run: the whole ./... suite.
Picked onto 697335d8e (native: pf-serve-base for x86); not run again there.
42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
package provider
|
|
|
|
import (
|
|
"runtime"
|
|
"strconv"
|
|
"testing"
|
|
)
|
|
|
|
// GroupFinder reads every provider's models once and looks up many
|
|
// groups in them; the list's index is made once too, not for each group.
|
|
func TestGroupFinderIndexesTheListOnce(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", t.TempDir())
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
models := make([]string, 2000)
|
|
for i := range models {
|
|
models[i] = "model-with-a-longer-name-" + strconv.Itoa(i)
|
|
}
|
|
if err := Save(Provider{ID: "a", Name: "a", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: models}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := SaveGroup(Group{ID: "g", Name: "G", Members: []string{"a/" + models[7]}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
find := GroupFinder()
|
|
if _, ms, ok := find("group/g"); !ok || len(ms) != 1 || ms[0].Model != models[7] {
|
|
t.Fatalf("group/g: %+v %v", ms, ok)
|
|
}
|
|
var before, after runtime.MemStats
|
|
runtime.ReadMemStats(&before)
|
|
const n = 20
|
|
for range n {
|
|
find("group/g")
|
|
}
|
|
runtime.ReadMemStats(&after)
|
|
per := (after.TotalAlloc - before.TotalAlloc) / n
|
|
t.Logf("%d bytes a lookup", per)
|
|
if per > 16<<10 {
|
|
t.Errorf("a group looked up again cost %d bytes: the list of %d models was indexed again", per, len(models))
|
|
}
|
|
}
|