From 5a48a4251cd98477c0e9fc073898dc6f2e45a275 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 13 Nov 2026 15:18:25 +0800 Subject: [PATCH] edit: a TOML file's kept table names share one buffer (#2278) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TOMLTables' answer is kept for as long as magpie runs (tomlAnswers), and Codex's config.toml names a table for every project it trusted, ~11k on a long history. setNames cloned each name on its own: one allocation a name, rounded up to its size class, all scanned by every GC. They are now copied into one buffer and sliced from it; they still hold nothing of the file's text. Measured on mote, 11k names like projects."/Users/…/p-N", HeapAlloc and HeapObjects kept, separate processes, twice each: before: 841,680 B, 10,994 objects after: ~715,000 B, 2 objects -15% bytes, 11k fewer objects for the GC to scan. Run on mote: gofmt -l, go vet ./internal/edit, go test ./internal/edit (TestKeptNamesArePacked: names equal, none points into the source text, packing takes 2 allocations, nil stays nil). --- internal/edit/toml_idle.go | 29 ++++++++++++++++++--- internal/edit/toml_pack_test.go | 45 +++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 internal/edit/toml_pack_test.go diff --git a/internal/edit/toml_idle.go b/internal/edit/toml_idle.go index 6d94a24ea..4942193e7 100644 --- a/internal/edit/toml_idle.go +++ b/internal/edit/toml_idle.go @@ -137,15 +137,36 @@ func (a *tomlAnswers) getNames() ([]string, bool) { } // setNames keeps a copy of names that holds no piece of the file's text. +// The copies share one buffer: Codex's config.toml names ~11k tables, one +// allocation each, rounded up to its size class, for as long as magpie +// runs (#2278). func (a *tomlAnswers) setNames(names []string) { var kept []string if names != nil { - kept = make([]string, len(names)) - for i, n := range names { - kept[i] = strings.Clone(n) - } + kept = packStrings(names) } a.Lock() defer a.Unlock() a.names, a.haveNames = kept, true } + +// packStrings copies ss into one buffer; the copies share nothing with ss. +func packStrings(ss []string) []string { + n := 0 + for _, s := range ss { + n += len(s) + } + var b strings.Builder + b.Grow(n) + for _, s := range ss { + b.WriteString(s) + } + all := b.String() + out := make([]string, len(ss)) + off := 0 + for i, s := range ss { + out[i] = all[off : off+len(s)] + off += len(s) + } + return out +} diff --git a/internal/edit/toml_pack_test.go b/internal/edit/toml_pack_test.go new file mode 100644 index 000000000..4bfa80095 --- /dev/null +++ b/internal/edit/toml_pack_test.go @@ -0,0 +1,45 @@ +package edit + +import ( + "fmt" + "strings" + "testing" + "unsafe" +) + +// The kept table names of a Codex config.toml are one buffer, not an +// allocation a name, and hold nothing of the text they were read from. +func TestKeptNamesArePacked(t *testing.T) { + text := new(strings.Builder) + var names []string + for i := range 11000 { + fmt.Fprintf(text, "projects.\"/Users/someone/work/project-%05d\"\x00", i) + } + names = strings.Split(strings.TrimSuffix(text.String(), "\x00"), "\x00") + names = append(names, "", "model_providers.magpie") + src := text.String() + lo, hi := uintptr(unsafe.Pointer(unsafe.StringData(src))), uintptr(unsafe.Pointer(unsafe.StringData(src)))+uintptr(len(src)) + + a := &tomlAnswers{} + a.setNames(names) + got, ok := a.getNames() + if !ok || len(got) != len(names) { + t.Fatalf("got %d names (ok %v), want %d", len(got), ok, len(names)) + } + for i := range names { + if got[i] != names[i] { + t.Fatalf("name %d = %q, want %q", i, got[i], names[i]) + } + if p := uintptr(unsafe.Pointer(unsafe.StringData(got[i]))); got[i] != "" && p >= lo && p < hi { + t.Fatalf("name %d holds the file's text", i) + } + } + if n := testing.AllocsPerRun(5, func() { packStrings(names) }); n > 2 { + t.Fatalf("packing %d names took %v allocations, want 2", len(names), n) + } + var nilNames tomlAnswers + nilNames.setNames(nil) + if got, ok := nilNames.getNames(); !ok || got != nil { + t.Fatalf("nil names kept as %v (ok %v)", got, ok) + } +}