provider: a look at one model of the vendor's list reads it without a copy
ListedAPIs, the Kilo and Cline free marks, a model's fast tiers and a remote magpie's price each look up one model of the provider's fetched list (liveByID), for every model a page lists. Each look copied the models of that id out of the list read, to read one field. With the owner's real config the copies were 32 MB of the 2.6 GB a v0.1.2432 magpie allocated over 20 window polls. catalog.LiveByIDShared is LiveByID without the copy (clipped, so an append is the caller's own); liveByID uses it, and still copies a model the user gave its own id, to set that id. Every caller only reads it. TestLiveModelReadWithoutACopy (a 200-model list, under provider.Hold and filememo.Hold as a request runs): a look costs less than a model over the list's read itself, a caller's append doesn't reach the list, and LiveByID still hands back its own copy. Broken by hand on mote: liveByID back on LiveByID is red (1112 bytes a look against 520 for the read; a model is 432). Run on mote (go test -timeout 45m): internal/catalog ok 0.7s, internal/provider ok 112s. go vet of both on darwin, windows and linux ok; gofmt -l clean.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user