Files
magpie/internal/edit/toml_names_test.go
T
yetone 76c130b29c 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.
2026-11-01 04:13:05 +08:00

60 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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")
}
}