edit: a TOML file's table names are listed once a read, not on each look
TOMLTables walked the parsed file and built a fresh sorted list of table
names on every call. The window's polls ask it for every agent's config
several times a poll, and twenty polls of the real-account sandbox spent
46 MB on those lists. The names are now filled when the file is parsed
(the parse is already kept by mod time and size), clipped, and handed out
shared; a caller that appends gets its own copy.
TestTOMLTablesListedOnceARead holds the allocation of repeated looks at
the same file to a small bound. With the old code put back by hand it is
red: 158474 bytes a look.
Run at 554291774's tree (on 2597c9636):
- Windows (win-run): go vet ./internal/edit ok, go test ./internal/edit ok.
- Linux (wsl-run): gofmt, vet ok; internal/edit ok. internal/agent has
TestGrokInstalled, TestCursorFolderFromTheEditorIsNotTheCLI,
TestCopilotFolderFromAnIDEPluginIsNotTheCLI,
TestQoderSettingsFromAnIDEPluginIsNotTheCLI and TestOmpDetectedWithPiDir
red, the install-detection reds filed as #4033 / #3141; this change
doesn't touch install detection.
Not run: the full suite at the rebased head.
This commit is contained in:
+14
-8
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user