appdir: a folder and file name are joined once, not on every read
appdir.Config and Cache, catalog.CachePath and LivePath, settings.Path, provider.Path and the logins path each ran filepath.Join on every call, and they are called on every read of a cached file, many times a request. Those joins were 70 MB of the 1.4 GB twenty window polls allocated. appdir.Join and JoinFile keep each (dir, name, ext) they joined, up to 4096 of them, so a path named again is the same string. A changed HOME or folder is a different key, so a test's sandbox still gets its own path. TestFoldersJoinedOnce and TestLivePathNamedOnce: 0 allocations a call. Broken by hand on mote (the files before this change) they are red at 1 allocation a call, and 3 for LivePath. Run on mote at this tree (ond714c4216): gofmt clean; vet ok on darwin, windows and linux (appdir, catalog, settings, provider); go build ok; appdir, catalog, settings (49s), provider (187s), agent (324s) and gateway (1244s) ok. At the pushed head (on019bfc951, which adds provider.Held): vet of appdir, catalog, settings, provider and gui ok, go build ok, appdir, catalog and gui's TestAccountLabelsBuildTheAgentsOnce ok.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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/<id>.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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user