diff --git a/internal/edit/toml.go b/internal/edit/toml.go index 075abdd0a..5f4c00bd0 100644 --- a/internal/edit/toml.go +++ b/internal/edit/toml.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io/fs" + "slices" "sort" "strconv" "strings" @@ -24,18 +25,16 @@ import ( // array-table headers. A name is the header's key path: a quoted part keeps its // quotes, and whitespace around the dots is not part of it, so `[ a . b ]` is // the name `a.b`. Missing files return (nil, nil); errors include the path. +// The list is made once each time the file is read and shared by every +// caller, so read only (an append makes a copy): a Codex config.toml with a +// table for each of 11,000 projects had it made again for each look, 46 MB +// of what twenty window polls allocated. func TOMLTables(path string) ([]string, error) { - tables, err := tomlTablesOf(path) + doc, err := tomlFileOf(path) if err != nil { return nil, err } - var out []string - for _, table := range tables { - if !table.array { - out = append(out, table.name) - } - } - return out, nil + return doc.names, nil } // GetTOMLTable returns the scalar keys of one table, or (nil, nil) when @@ -480,6 +479,12 @@ func tomlFileOf(path string) (tomlFile, error) { if err != nil { return tomlFile{}, fmt.Errorf("%s: %w", path, err) } + for _, table := range doc.tables { + if !table.array { + doc.names = append(doc.names, table.name) + } + } + doc.names = slices.Clip(doc.names) return doc, nil }) if errors.Is(err, fs.ErrNotExist) { @@ -501,6 +506,7 @@ func parseTOMLTables(lines []string) ([]tomlTableSpan, error) { type tomlFile struct { root tomlTableSpan tables []tomlTableSpan + names []string // TOMLTables } // parseTOMLFile is parseTOMLTables with the top-level keys too, located the diff --git a/internal/edit/toml_names_test.go b/internal/edit/toml_names_test.go new file mode 100644 index 000000000..0e9dc45a9 --- /dev/null +++ b/internal/edit/toml_names_test.go @@ -0,0 +1,59 @@ +package edit + +import ( + "fmt" + "os" + "path/filepath" + "runtime" + "slices" + "strings" + "testing" + "time" +) + +// A Codex config.toml keeps a table for each project it was opened in, and +// the agents' looks list its tables many times a page: a look at a file +// that hasn't changed makes no list of its own. A caller's append doesn't +// reach the list, and a write is seen. +func TestTOMLTablesListedOnceARead(t *testing.T) { + var b strings.Builder + b.WriteString("model = \"m\"\n\n[[skills]]\nname = \"a\"\n\n") + for i := range 3000 { + fmt.Fprintf(&b, "[projects.\"/Users/someone/project-%d\"]\ntrust_level = \"trusted\"\n\n", i) + } + b.WriteString("[desktop]\non = true\n") + path := filepath.Join(t.TempDir(), "config.toml") + if err := os.WriteFile(path, []byte(b.String()), 0o600); err != nil { + t.Fatal(err) + } + old := time.Now().Add(-time.Hour) + if err := os.Chtimes(path, old, old); err != nil { + t.Fatal(err) + } + names, err := TOMLTables(path) + if err != nil || len(names) != 3001 || names[3000] != "desktop" || slices.Contains(names, "skills") { + t.Fatalf("%d names, last %q, %v", len(names), names[len(names)-1], err) + } + _ = append(names, "mine") + if again, _ := TOMLTables(path); len(again) != 3001 { + t.Fatalf("a caller's append reached the list: %d", len(again)) + } + const looks = 20 + var m0, m1 runtime.MemStats + runtime.GC() + runtime.ReadMemStats(&m0) + for range looks { + TOMLTables(path) + } + runtime.ReadMemStats(&m1) + // the list itself is 3001 × 16 bytes, 47 KB + if per := (m1.TotalAlloc - m0.TotalAlloc) / looks; per > 4<<10 { + t.Errorf("a look at %d tables allocates %d bytes", len(names), per) + } + if err := SetTOMLTable(path, "extra", KV{Path: "a", Value: "b"}); err != nil { + t.Fatal(err) + } + if again, _ := TOMLTables(path); !slices.Contains(again, "extra") { + t.Errorf("after a write the list has no extra") + } +}