edit: a TOML file's kept table names share one buffer (#2278)

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).
This commit is contained in:
yetone
2026-11-13 15:18:39 +08:00
parent a814ee9907
commit 5a48a4251c
2 changed files with 70 additions and 4 deletions
+25 -4
View File
@@ -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
}
+45
View File
@@ -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)
}
}