diff --git a/internal/appdir/appdir.go b/internal/appdir/appdir.go index 452548b15..269fcf0a4 100644 --- a/internal/appdir/appdir.go +++ b/internal/appdir/appdir.go @@ -228,9 +228,9 @@ func Config() string { return p } if x := os.Getenv("XDG_CONFIG_HOME"); rooted(x) { - return filepath.Join(x, "magpie") + return Join(x, "magpie") } - return filepath.Join(mustHome(), ".config", "magpie") + return Join(Join(mustHome(), ".config"), "magpie") } // Redirected is the folder magpie keeps its files in when it is opened @@ -257,12 +257,12 @@ func Redirected() string { // catalog, exchange rates, market lists). func Cache() string { if p := Portable(); p != "" { - return filepath.Join(p, "cache") + return Join(p, "cache") } if x := os.Getenv("XDG_CACHE_HOME"); rooted(x) { - return filepath.Join(x, "magpie") + return Join(x, "magpie") } - return filepath.Join(mustHome(), ".cache", "magpie") + return Join(Join(mustHome(), ".cache"), "magpie") } // Logs is the folder the desktop app writes its log in, where each system diff --git a/internal/appdir/join.go b/internal/appdir/join.go new file mode 100644 index 000000000..e92117f67 --- /dev/null +++ b/internal/appdir/join.go @@ -0,0 +1,37 @@ +package appdir + +import ( + "path/filepath" + "sync" +) + +// joins are the paths Join has made, by what they were made of. magpie +// names its files from these folders on every read of them, and a look +// at the providers reads them for each provider and model: the joins +// were 70 MB of the 1.4 GB twenty window polls allocated. +var joins struct { + sync.Mutex + by map[[3]string]string +} + +// Join is filepath.Join(dir, name), made once for each pair and kept +// while the folders it is asked for stay the same. +func Join(dir, name string) string { return JoinFile(dir, name, "") } + +// JoinFile is filepath.Join(dir, name+ext), made once for each, as Join: +// a provider's list is models/.json. +func JoinFile(dir, name, ext string) string { + k := [3]string{dir, name, ext} + joins.Lock() + defer joins.Unlock() + if p, ok := joins.by[k]; ok { + return p + } + // a bound, for folders that change under a test, not a magpie + if joins.by == nil || len(joins.by) >= 4096 { + joins.by = map[[3]string]string{} + } + p := filepath.Join(dir, name+ext) + joins.by[k] = p + return p +} diff --git a/internal/appdir/join_test.go b/internal/appdir/join_test.go new file mode 100644 index 000000000..ad20c10fa --- /dev/null +++ b/internal/appdir/join_test.go @@ -0,0 +1,41 @@ +package appdir + +import ( + "path/filepath" + "testing" +) + +// magpie's folders and the files named from them are joined once, not on +// every read of a file, and a folder changed since is seen at once. +func TestFoldersJoinedOnce(t *testing.T) { + t.Setenv("APPIMAGE", "") + r := root(t) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(r, "cfg")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(r, "cache")) + t.Cleanup(func() { UseExecutable("") }) + UseExecutable("") + + for name, f := range map[string]func(){ + "Config": func() { Config() }, + "Cache": func() { Cache() }, + "JoinFile": func() { JoinFile(Cache(), "openai", ".json") }, + } { + f() + if n := testing.AllocsPerRun(20, f); n > 0 { + t.Errorf("%s with the folders unchanged: %.0f allocations", name, n) + } + } + if got, want := JoinFile(Join(Cache(), "models"), "openai", ".json"), filepath.Join(r, "cache", "magpie", "models", "openai.json"); got != want { + t.Errorf("JoinFile = %q, want %q", got, want) + } + + t.Setenv("XDG_CACHE_HOME", filepath.Join(r, "other")) + if got, want := Cache(), filepath.Join(r, "other", "magpie"); got != want { + t.Errorf("a cache folder changed since: %q, want %q", got, want) + } + t.Setenv("XDG_CONFIG_HOME", "") + t.Setenv(homeVar(), filepath.Join(r, "home")) + if got, want := Config(), filepath.Join(r, "home", ".config", "magpie"); got != want { + t.Errorf("under the home folder: %q, want %q", got, want) + } +} diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index a5ecb7caa..3bc208e81 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -586,7 +586,7 @@ var ( ) // CachePath is where `magpie sync` stores the models.dev catalog. -func CachePath() string { return filepath.Join(appdir.Cache(), "models.json") } +func CachePath() string { return appdir.Join(appdir.Cache(), "models.json") } func opencodeCache() string { home, _ := os.UserHomeDir() diff --git a/internal/catalog/live.go b/internal/catalog/live.go index 8599505b3..ece415903 100644 --- a/internal/catalog/live.go +++ b/internal/catalog/live.go @@ -16,6 +16,7 @@ import ( "strings" "time" + "github.com/yetone/magpie/internal/appdir" "github.com/yetone/magpie/internal/filememo" ) @@ -26,7 +27,7 @@ import ( // LivePath is where the fetched model list of one provider is kept. func LivePath(provider string) string { - return filepath.Join(filepath.Dir(CachePath()), "models", provider+".json") + return appdir.JoinFile(appdir.Join(filepath.Dir(CachePath()), "models"), provider, ".json") } type liveFile struct { diff --git a/internal/catalog/livepath_once_test.go b/internal/catalog/livepath_once_test.go new file mode 100644 index 000000000..ac9877cdc --- /dev/null +++ b/internal/catalog/livepath_once_test.go @@ -0,0 +1,17 @@ +package catalog + +import ( + "path/filepath" + "testing" +) + +// A provider's list file is named once, not on each read of the list: a +// look at the providers reads it for every model. +func TestLivePathNamedOnce(t *testing.T) { + if got, want := LivePath("openai"), filepath.Join(filepath.Dir(CachePath()), "models", "openai.json"); got != want { + t.Fatalf("LivePath = %q, want %q", got, want) + } + if n := testing.AllocsPerRun(20, func() { LivePath("openai") }); n > 0 { + t.Errorf("LivePath with the folders unchanged: %.0f allocations", n) + } +} diff --git a/internal/provider/logins.go b/internal/provider/logins.go index a1cd9b6e8..430ed43b5 100644 --- a/internal/provider/logins.go +++ b/internal/provider/logins.go @@ -180,7 +180,7 @@ func LoginID(agent, user string) string { return loginID(savedLogin{Agent: agent, User: user}) } -func loginsPath() string { return filepath.Join(filepath.Dir(Path()), "logins.json") } +func loginsPath() string { return appdir.Join(filepath.Dir(Path()), "logins.json") } // lastLogins is the accounts last read from logins.json: a read that fails // (a file half there, one magpie can't open for a moment) is them, not no diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 2648c45cc..ee4d482f2 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -563,7 +563,7 @@ type file struct { } // Path is the file the user's providers live in. -func Path() string { return filepath.Join(appdir.Config(), "providers.json") } +func Path() string { return appdir.Join(appdir.Config(), "providers.json") } // load is the file for a read that goes on without it: start-up, the // gateway, the catalog. One that can't be read is taken as empty there; diff --git a/internal/settings/settings.go b/internal/settings/settings.go index e85525065..d646e3ffc 100644 --- a/internal/settings/settings.go +++ b/internal/settings/settings.go @@ -16,7 +16,6 @@ import ( "math" "net/url" "os" - "path/filepath" "reflect" "regexp" "runtime" @@ -1401,7 +1400,7 @@ func renameInMap(m reflect.Value, from, to string) bool { } // Path is the settings file. -func Path() string { return filepath.Join(Dir(), "settings.json") } +func Path() string { return appdir.Join(Dir(), "settings.json") } // Dir is the folder every magpie file lives in: the data folder beside a // portable magpie (appdir.Portable), else ~/.config/magpie.