diff --git a/internal/edit/toml_idle.go b/internal/edit/toml_idle.go index 4942193e7..65ae4784f 100644 --- a/internal/edit/toml_idle.go +++ b/internal/edit/toml_idle.go @@ -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. diff --git a/internal/edit/toml_idle_test.go b/internal/edit/toml_idle_test.go index f50aedaf2..ad6de688d 100644 --- a/internal/edit/toml_idle_test.go +++ b/internal/edit/toml_idle_test.go @@ -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) + } + } +}