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.
38 lines
1.0 KiB
Go
38 lines
1.0 KiB
Go
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
|
|
}
|