diff --git a/internal/catalog/live.go b/internal/catalog/live.go index 5191b69eb..8599505b3 100644 --- a/internal/catalog/live.go +++ b/internal/catalog/live.go @@ -92,6 +92,18 @@ func LiveByID(provider, model string) []Model { return slices.Clone(f.byID[model]) } +// LiveByIDShared is LiveByID without the copy, for a caller that only +// reads it: asked for a model's APIs, price or tiers for every model a +// page lists, the copies were 32 MB of a live magpie's 2.6 GB in a minute +// and a half. +func LiveByIDShared(provider, model string) []Model { + f, err := readLive(provider) + if err != nil { + return nil + } + return slices.Clip(f.byID[model]) +} + // LiveListed is whether Live lists any model, without the list copied. func LiveListed(provider string) bool { f, err := readLive(provider) diff --git a/internal/provider/live_shared_test.go b/internal/provider/live_shared_test.go new file mode 100644 index 000000000..fe8846a5d --- /dev/null +++ b/internal/provider/live_shared_test.go @@ -0,0 +1,71 @@ +package provider + +import ( + "fmt" + "os" + "path/filepath" + "runtime" + "slices" + "testing" + "time" + "unsafe" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/filememo" +) + +// A look at one model of the vendor's list (its APIs, price, tiers), asked +// for every model a page lists, reads the model as the list has it, not a +// copy: the copies were 32 MB of a live magpie's 2.6 GB in a minute and a +// half. A caller's append is its own, and a model the user gave its own id +// is still a copy with that id. +func TestLiveModelReadWithoutACopy(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")) + var ms []catalog.Model + for i := range 200 { + ms = append(ms, catalog.Model{ID: fmt.Sprintf("m-%d", i), Name: fmt.Sprintf("M %d", i), APIs: []string{"openai-responses"}}) + } + if err := catalog.SaveLive("r", "http://127.0.0.1:1/v1", ms); err != nil { + t.Fatal(err) + } + old := time.Now().Add(-time.Hour) + if err := os.Chtimes(catalog.LivePath("r"), old, old); err != nil { + t.Fatal(err) + } + p := Provider{ID: "r", Name: "R", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m-7"}} + defer Hold()() + defer filememo.Hold()() + if got := p.ListedAPIs("m-7"); !slices.Equal(got, []Protocol{"openai-responses"}) { + t.Fatalf("m-7's APIs: %v", got) + } + bytesPer := func(f func()) uint64 { + var before, after runtime.MemStats + runtime.ReadMemStats(&before) + for range 200 { + f() + } + runtime.ReadMemStats(&after) + return (after.TotalAlloc - before.TotalAlloc) / 200 + } + // the read of the list itself (its path, the file's look) is the + // same either way; what is over it is the copy + read := bytesPer(func() { catalog.LiveListed("r") }) + looked := bytesPer(func() { p.liveByID("m-7") }) + if size := uint64(unsafe.Sizeof(catalog.Model{})); looked >= read+size { + t.Fatalf("a look at one model took %d bytes, the list's read %d; a model is %d", looked, read, size) + } + + got := p.liveByID("m-7") + _ = append(got, catalog.Model{ID: "added"}) + if again := p.liveByID("m-7"); len(again) != 1 || again[0].ID != "m-7" { + t.Fatalf("a caller's append reached the list: %+v", again) + } + if ms := catalog.LiveByID("r", "m-8"); len(ms) != 1 || ms[0].ID != "m-8" { + t.Fatalf("LiveByID: %+v", ms) + } +} diff --git a/internal/provider/models.go b/internal/provider/models.go index d47fc7777..d9f2743de 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -160,6 +160,7 @@ func (p Provider) live() ([]catalog.Model, time.Time, bool) { // liveByID are the models of live with this id: what a look at one // model, asked for each of a list's, reads instead of the whole list. +// They are the list read, not a copy: a caller only reads them. func (p Provider) liveByID(model string) []catalog.Model { if !p.lists() { return nil @@ -167,7 +168,7 @@ func (p Provider) liveByID(model string) []catalog.Model { // a model listed by an id the user gave it is in the vendor's list // under its own (SetModelID) own := p.OwnModelID(model) - ms := catalog.LiveByID(p.ID, own) + ms := catalog.LiveByIDShared(p.ID, own) if own == model { return ms }