edit: a TOML file's table names are let go after 5 minutes unlisted
Codex's real config.toml (1.5 MB, ~11k project tables) had its table
names kept for the process's life once the start-up rename
(agent.RenameLegacy) listed them: 1.16 MB of packStrings in an exact
heap profile (memprofilerate=1) after 8 minutes idle in the real-account
sandbox, the largest holder left under cli.init. The whole parse was
already let go after a minute (tomlIdle); the names now go after
namesIdle (5 min) with no list, the other kept answers (GetTOMLTop,
GetTOMLTable, polled every 3s) stay, and a list a caller holds is not
touched.
Cost of a list after the let-go: BenchmarkTOMLTablesCold on that config,
9.0 ms and 21.5 MB of garbage per list (mote, M5 Pro).
Run: go test ./internal/edit (mote), TestTOMLNamesLetGoWhenIdle red with
the drop made a no-op by hand ("the names were kept past namesIdle"),
green with it.
Real-account sandbox (mote, real Codex config.toml), 8 minutes idle after
the page closed. Exact heap (memprofilerate=1): liveHeap 19.47 MB before
(v0.1.4082) -> 18.00 MB after, and RenameLegacy's held bytes 2.24 MB ->
0.86 MB, with no packStrings left. Without the profiler: liveHeap 19.57
-> 18.01 MB, heapInuse 33.1 -> 31.0 MB; footprint 71 vs 75 MB, within
one run's noise of each other. Also run on mica: gofmt, go vet for
darwin, windows and linux, go test -race ./internal/edit, and go test
./internal/agent ./internal/library ./internal/cli, all ok.
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
"weak"
|
||||
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
)
|
||||
@@ -74,6 +75,7 @@ type tomlAnswers struct {
|
||||
tables map[string]map[string]string // nil for a table the file hasn't
|
||||
names []string // TOMLTables' answer, once haveNames
|
||||
haveNames bool
|
||||
namesHeld bool // in namesKept.held
|
||||
}
|
||||
|
||||
type tomlTop struct {
|
||||
@@ -133,6 +135,9 @@ func (a *tomlAnswers) setTable(name string, t map[string]string) {
|
||||
func (a *tomlAnswers) getNames() ([]string, bool) {
|
||||
a.Lock()
|
||||
defer a.Unlock()
|
||||
if a.haveNames {
|
||||
namesUsed(a)
|
||||
}
|
||||
return a.names, a.haveNames
|
||||
}
|
||||
|
||||
@@ -148,6 +153,70 @@ func (a *tomlAnswers) setNames(names []string) {
|
||||
a.Lock()
|
||||
defer a.Unlock()
|
||||
a.names, a.haveNames = kept, true
|
||||
namesUsed(a)
|
||||
}
|
||||
|
||||
// A file's kept table names are let go too once no one has listed them
|
||||
// for namesIdle: Codex's ~11k are 1.2 MB, listed by a one-time rename at
|
||||
// start and then only by the Agents page and a sync, and were held for
|
||||
// as long as magpie ran. The next list parses the file again: 9 ms and
|
||||
// 21 MB of garbage for that config on an M5 Pro.
|
||||
const namesIdle = 5 * time.Minute
|
||||
|
||||
var namesKept struct {
|
||||
last atomic.Int64 // UnixNano of the last list answered
|
||||
armed atomic.Bool
|
||||
mu sync.Mutex
|
||||
held []weak.Pointer[tomlAnswers]
|
||||
}
|
||||
|
||||
// namesUsed notes a list of a's names, and arms the timer that lets them
|
||||
// go once lists stop. a.Lock is held.
|
||||
func namesUsed(a *tomlAnswers) {
|
||||
if tomlKept.off {
|
||||
return
|
||||
}
|
||||
namesKept.last.Store(time.Now().UnixNano())
|
||||
if !a.namesHeld {
|
||||
a.namesHeld = true
|
||||
namesKept.mu.Lock()
|
||||
namesKept.held = append(namesKept.held, weak.Make(a))
|
||||
namesKept.mu.Unlock()
|
||||
}
|
||||
if namesKept.armed.CompareAndSwap(false, true) {
|
||||
time.AfterFunc(namesIdle, namesIdleCheck)
|
||||
}
|
||||
}
|
||||
|
||||
func namesIdleCheck() {
|
||||
idle := time.Since(time.Unix(0, namesKept.last.Load()))
|
||||
if idle < namesIdle {
|
||||
time.AfterFunc(namesIdle-idle, namesIdleCheck)
|
||||
return
|
||||
}
|
||||
namesKept.armed.Store(false)
|
||||
dropTOMLNames()
|
||||
// a list between the check and the drop armed nothing: look again
|
||||
if time.Since(time.Unix(0, namesKept.last.Load())) < namesIdle && namesKept.armed.CompareAndSwap(false, true) {
|
||||
time.AfterFunc(namesIdle, namesIdleCheck)
|
||||
}
|
||||
}
|
||||
|
||||
// dropTOMLNames lets go of every file's kept table names; the other
|
||||
// answers stay.
|
||||
func dropTOMLNames() {
|
||||
namesKept.mu.Lock()
|
||||
held := namesKept.held
|
||||
namesKept.held = nil
|
||||
namesKept.mu.Unlock()
|
||||
for _, w := range held {
|
||||
if a := w.Value(); a != nil {
|
||||
a.Lock()
|
||||
a.names, a.haveNames, a.namesHeld = nil, false, false
|
||||
a.Unlock()
|
||||
}
|
||||
}
|
||||
debug.FreeOSMemory()
|
||||
}
|
||||
|
||||
// packStrings copies ss into one buffer; the copies share nothing with ss.
|
||||
|
||||
@@ -110,3 +110,67 @@ func TestTOMLTablesKeptWhenIdle(t *testing.T) {
|
||||
t.Fatalf("after a change the tables = %q", names)
|
||||
}
|
||||
}
|
||||
|
||||
// A file's table names no one has listed for namesIdle are let go, and
|
||||
// the next list parses the file again; a list a caller holds stays whole.
|
||||
func TestTOMLNamesLetGoWhenIdle(t *testing.T) {
|
||||
tomlKept.off = false
|
||||
t.Cleanup(func() { tomlKept.off = true })
|
||||
p := filepath.Join(t.TempDir(), "config.toml")
|
||||
if err := os.WriteFile(p, []byte("a = 1\n\n[projects.\"/x\"]\ntrust_level = \"trusted\"\n\n[model_providers.magpie]\nbase_url = \"http://x\"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := time.Now().Add(-time.Hour)
|
||||
os.Chtimes(p, old, old)
|
||||
filememo.Forget()
|
||||
want := []string{`projects."/x"`, "model_providers.magpie"}
|
||||
held, err := TOMLTables(p)
|
||||
if err != nil || !slices.Equal(held, want) {
|
||||
t.Fatalf("tables = %q, %v", held, err)
|
||||
}
|
||||
if v, _ := GetTOMLTop(p, "a"); v != "1" {
|
||||
t.Fatalf("a = %q", v)
|
||||
}
|
||||
tomlKept.last.Store(time.Now().Add(-2 * tomlIdle).UnixNano())
|
||||
tomlIdleCheck()
|
||||
|
||||
// listed within namesIdle: kept
|
||||
namesKept.last.Store(time.Now().Add(-namesIdle / 2).UnixNano())
|
||||
n := tomlParses.Load()
|
||||
if names, _ := TOMLTables(p); !slices.Equal(names, want) || tomlParses.Load() != n {
|
||||
t.Fatalf("tables %q after %d parses; want them kept", names, tomlParses.Load()-n)
|
||||
}
|
||||
|
||||
namesKept.last.Store(time.Now().Add(-2 * namesIdle).UnixNano())
|
||||
namesIdleCheck()
|
||||
a := tomlAnswersOf(p)
|
||||
if _, ok := a.getNames(); ok {
|
||||
t.Fatal("the names were kept past namesIdle")
|
||||
}
|
||||
if !slices.Equal(held, want) {
|
||||
t.Fatalf("a caller's list = %q after the let-go", held)
|
||||
}
|
||||
if v, _ := GetTOMLTop(p, "a"); v != "1" || tomlParses.Load() != n {
|
||||
t.Fatalf("a = %q after %d parses; the other answers should stay", v, tomlParses.Load()-n)
|
||||
}
|
||||
if names, _ := TOMLTables(p); !slices.Equal(names, want) || tomlParses.Load() != n+1 {
|
||||
t.Fatalf("tables %q after %d parses; want one parse", names, tomlParses.Load()-n)
|
||||
}
|
||||
if names, _ := TOMLTables(p); !slices.Equal(names, want) || tomlParses.Load() != n+1 {
|
||||
t.Fatalf("listed again: %q after %d parses; want them kept again", names, tomlParses.Load()-n)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkTOMLTablesCold(b *testing.B) {
|
||||
src := os.Getenv("MAGPIE_BENCH_TOML")
|
||||
if src == "" {
|
||||
b.Skip("MAGPIE_BENCH_TOML unset")
|
||||
}
|
||||
for b.Loop() {
|
||||
filememo.DropKind("toml file")
|
||||
filememo.DropKind("toml answers")
|
||||
if _, err := TOMLTables(src); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user