Files
magpie/internal/edit/toml_without.go
T
yetone 0caa09b3d9 perf: yaml's event queue, TOMLWithout's buffer and the catalog's hash stop holding whole documents
A start with the owner's real accounts (release build, mote) allocated
279 MB in its first seven minutes; three of the largest were buffers
the size of a whole document that nothing needed whole:

- yaml.v3's emitter appends every event to a queue it never empties,
  so encoding dsh's route of a few hundred models held ~24 MB of
  events (dshPutProviders' Node.Encode and dshNodeLines). That round
  runs on the first start of each version and on each catalog change.
  yaml.v3 is vendored in third_party/yaml.v3 (replace in go.mod, as
  jsonschema is) with the queue started again once it is drained;
  MAGPIE.md says what changed. go.yaml.in/yaml/v3 v3.0.5 has the same
  queue.
- edit.TOMLWithout made its output as large as the input: 1.5 MB per
  read of Codex's config, which five readers make, to keep a few KB
  around the projects tables. It now grows to what is kept.
- catalog.fileKey read the ~6 MB catalog whole to hash it; it is now
  hashed as it is read.

Measured with the owner's real accounts (~/r2278/acct, tray only, no
window), release ldflags and the footprint overlay, a version new to
the HOME so dsh's full round runs, on mote:
- allocations in the first 7 minutes 279 MB -> 260 MB;
  yaml_emitter_emit 24.9 MB -> under pprof's cut (1.3 MB),
  dshPutProviders 23.1 -> 8.1 MB, dshNodeLines 12 MB -> under the cut,
  edit.tomlUnder 12.2 -> 4.0 MB
- footprint at +300 s 59 -> 58 MB, live heap 5.5 MB both; peak 84 vs
  89 MB, within the run-to-run spread seen before (84-88 MB)

Run at this tree: upstream yaml.v3's own tests on the patched emitter
(go test ., ok); go test ./internal/agent ./internal/cli ./internal/edit
./internal/library ./internal/pluginrt ./internal/catalog on mica (ok),
and ./internal/sessions on mote before the yaml change (ok); go vet ./...
(mac), GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...
ok; gofmt -l on the changed files prints nothing.
Not run: the full suite.
2026-11-14 14:33:16 +08:00

131 lines
4.0 KiB
Go

package edit
import (
"bytes"
"github.com/pelletier/go-toml/v2"
"github.com/pelletier/go-toml/v2/unstable"
)
// TOMLWithout is raw without the tables under the top-level key name
// ([name], [name.…] and [[name.…]]), for a reader that wants nothing
// under it. Codex's config.toml holds a [projects."…"] table for each
// project it was trusted in, the owner's 10,933 in 1.5 MB, and
// toml.Unmarshal checks each table against its siblings one by one: a
// third of a second of CPU a decode, which every reader of the file paid
// each time Codex wrote it. The parser here is a single pass. raw itself
// is given back when there is no such table or it doesn't parse, so a
// broken file stays broken for the decode that follows.
func TOMLWithout(raw []byte, name string) []byte {
rest, _, _ := tomlUnder(raw, name, false)
return rest
}
// tomlUnder is raw without name's tables (rest) and, when group is set,
// each of those tables' text by the key under name it is for ([name.k]
// and [name.k.…] under "k"). bare is true when a table is name itself
// ([name] or [[name]]), which no group holds. rest is raw when there is
// no such table or raw doesn't parse.
func tomlUnder(raw []byte, name string, group bool) (rest []byte, groups map[string][]byte, bare bool) {
if !bytes.Contains(raw, []byte(name)) {
return raw, nil, false
}
var p unstable.Parser
p.Reset(raw)
var out []byte
cut := false // a table under name was found
kept := 0 // where the text kept since the last cut starts; -1 while cutting
from := -1 // where the table being cut starts
key := ""
end := func(at int) {
if from >= 0 && group {
if groups == nil {
groups = map[string][]byte{}
}
g := append(groups[key], raw[from:at]...)
groups[key] = append(g, '\n')
}
from = -1
}
for p.NextExpression() {
e := p.Expression()
if e.Kind != unstable.Table && e.Kind != unstable.ArrayTable {
continue
}
keys := e.Key()
if !keys.Next() {
continue
}
first := keys.Node()
// a header starts its own line, after any indent
at := bytes.LastIndexByte(raw[:first.Raw.Offset], '\n') + 1
end(at)
if string(first.Data) != name {
if kept < 0 {
kept = at
}
continue
}
cut = true
if kept >= 0 {
// out grows to what is kept, not raw's size: Codex's
// config is 1.5 MB of projects around a few KB of the rest
out = append(out, raw[kept:at]...)
kept = -1
}
from, key = at, ""
if keys.Next() {
key = string(keys.Node().Data)
} else {
bare = true
}
}
if p.Error() != nil || !cut {
return raw, nil, false
}
end(len(raw))
if kept >= 0 {
out = append(out, raw[kept:]...)
}
if out == nil {
out = []byte{}
}
return out, groups, bare
}
// validateTOMLDefinitions is toml.Unmarshal's check of data's definitions
// (no key or table defined twice). With many [projects."…"] tables, as a
// config of Codex's has, the rest of the file and each project's tables
// are decoded apart: they can't clash with one another, and the
// whole file at once took a third of a second (see TOMLWithout). A file
// where they could clash, or any error, is decoded whole, so the error
// is the one the whole file gives.
func validateTOMLDefinitions(data []byte) error {
if rest, groups, bare := tomlUnder(data, "projects", true); len(groups) > 64 && !bare {
var top map[string]any
ok := toml.Unmarshal(rest, &top) == nil
if _, set := top["projects"]; set {
ok = false // a dotted projects.… key outside the tables
}
// projects decoded 128 at a time: few enough siblings for the
// decoder's scan, and few decodes
var batch []byte
n, left := 0, len(groups)
for _, g := range groups {
batch, n, left = append(batch, g...), n+1, left-1
if n == 128 || left == 0 {
var m map[string]any // into a struct, a key defined twice passes
if ok = ok && toml.Unmarshal(batch, &m) == nil; !ok {
break
}
batch, n = batch[:0], 0
}
}
if ok {
return nil
}
}
var decoded map[string]any
return toml.Unmarshal(data, &decoded)
}