A start with the owner's real accounts (release build, mote) allocated 279 MB in its first seven minutes; three of the largest were buffers the size of a whole document that nothing needed whole: - yaml.v3's emitter appends every event to a queue it never empties, so encoding dsh's route of a few hundred models held ~24 MB of events (dshPutProviders' Node.Encode and dshNodeLines). That round runs on the first start of each version and on each catalog change. yaml.v3 is vendored in third_party/yaml.v3 (replace in go.mod, as jsonschema is) with the queue started again once it is drained; MAGPIE.md says what changed. go.yaml.in/yaml/v3 v3.0.5 has the same queue. - edit.TOMLWithout made its output as large as the input: 1.5 MB per read of Codex's config, which five readers make, to keep a few KB around the projects tables. It now grows to what is kept. - catalog.fileKey read the ~6 MB catalog whole to hash it; it is now hashed as it is read. Measured with the owner's real accounts (~/r2278/acct, tray only, no window), release ldflags and the footprint overlay, a version new to the HOME so dsh's full round runs, on mote: - allocations in the first 7 minutes 279 MB -> 260 MB; yaml_emitter_emit 24.9 MB -> under pprof's cut (1.3 MB), dshPutProviders 23.1 -> 8.1 MB, dshNodeLines 12 MB -> under the cut, edit.tomlUnder 12.2 -> 4.0 MB - footprint at +300 s 59 -> 58 MB, live heap 5.5 MB both; peak 84 vs 89 MB, within the run-to-run spread seen before (84-88 MB) Run at this tree: upstream yaml.v3's own tests on the patched emitter (go test ., ok); go test ./internal/agent ./internal/cli ./internal/edit ./internal/library ./internal/pluginrt ./internal/catalog on mica (ok), and ./internal/sessions on mote before the yaml change (ok); go vet ./... (mac), GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... ok; gofmt -l on the changed files prints nothing. Not run: the full suite.
80 lines
2.2 KiB
Go
80 lines
2.2 KiB
Go
package catalog
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// CacheKey names the catalog kept on disk, models.dev's provider catalog
|
|
// and the lab catalog beside it, by what the files hold, not by when they
|
|
// were written. A Claude account's model list fetches the catalog again
|
|
// every 45 minutes (Asked), and usage keyed its whole index on the file's
|
|
// mtime: each fetch, the same bytes, had the next usage query read every
|
|
// session file again, ~15 s of CPU over a long history.
|
|
func CacheKey() string { return fileKey(CachePath()) + "|" + fileKey(LabsPath()) }
|
|
|
|
// fileKeys holds each file's last key by the stat it was hashed at, so a
|
|
// key costs a stat until the file changes.
|
|
var fileKeys = struct {
|
|
sync.Mutex
|
|
m map[string]keyedFile
|
|
}{m: map[string]keyedFile{}}
|
|
|
|
type keyedFile struct {
|
|
size int64
|
|
mod time.Time
|
|
key string
|
|
}
|
|
|
|
// fileKey is a hash of path's bytes, "missing" when there is no file, and
|
|
// its stat when it can't be read: an unknown read is a change each time
|
|
// the file is written, as it was before.
|
|
func fileKey(path string) string {
|
|
st, err := os.Stat(path)
|
|
if err != nil {
|
|
return "missing"
|
|
}
|
|
fileKeys.Lock()
|
|
k, ok := fileKeys.m[path]
|
|
fileKeys.Unlock()
|
|
if ok && k.size == st.Size() && k.mod.Equal(st.ModTime()) {
|
|
return k.key
|
|
}
|
|
// hashed as it is read: the catalog is ~6 MB, and holding it whole
|
|
// for its sum added that much to the start's peak
|
|
h := sha256.New()
|
|
f, err := os.Open(path)
|
|
if err == nil {
|
|
_, err = io.Copy(h, f)
|
|
f.Close()
|
|
}
|
|
if err != nil {
|
|
return fmt.Sprintf("stat:%d:%d", st.Size(), st.ModTime().UnixNano())
|
|
}
|
|
key := hex.EncodeToString(h.Sum(nil))
|
|
fileKeys.Lock()
|
|
fileKeys.m[path] = keyedFile{size: st.Size(), mod: st.ModTime(), key: key}
|
|
fileKeys.Unlock()
|
|
return key
|
|
}
|
|
|
|
// keepFetched writes b to p, unless p holds b already: then only p's time
|
|
// is now, which Stale, Missing and Asked read as the fetch. It reports
|
|
// whether p's bytes changed.
|
|
func keepFetched(p string, b []byte) (bool, error) {
|
|
if old, err := os.ReadFile(p); err == nil && bytes.Equal(old, b) {
|
|
now := time.Now()
|
|
if err := os.Chtimes(p, now, now); err != nil {
|
|
return false, err
|
|
}
|
|
return false, nil
|
|
}
|
|
return true, writeWhole(p, b, time.Time{})
|
|
}
|