edit: the YAML looks keep a slim tree, not yaml.v3's nodes

GetYAML, GetYAMLMap and GetYAMLList read the file through filememo,
which kept yaml.v3's whole node tree for as long as magpie runs: ~160
bytes a node behind a pointer, with a tag, styles, comments and places
that no look reads. For dsh's 68 KB settings.yaml, read by RenameLegacy
at start and by dshGet on every poll, plus Hermes' config.yaml, that
tree was the largest thing left in an idle tray's heap. The looks now
keep a yamlLook instead: a node's kind, a scalar's value, and the nodes
under it held inline. GetYAMLText still marshals yaml.v3's tree, so it
keeps one under its own memo kind; only MiniMax's file is read that way.

Run (pushed head's tree, on mica and mote):
- go vet ./internal/edit ./internal/agent; go test ./internal/edit ok;
  go test ./internal/agent ok (190s, mica).
- TestYAMLLooksReadAsTheNodeTree holds every key path of a file with
  anchors, aliases, merge keys, flow and block lists, complex keys and
  comments to what lookupYAML on yaml.v3's tree reads. Broken by hand
  (a mapping key copied as its tag, not its value), it goes red.
- Idle tray, real account sandbox with the owner's 25,379 probe lines,
  at 150 s, three alternating runs: live heap 9.21 MB (w38, v0.1.3941)
  vs 8.39 and 8.41 MB (this); footprint 77 vs 75 and 74 MB.
  edit.parseYAMLRoot is gone from the inuse top.

Not tried: Windows or Linux (no OS-specific code here).
This commit is contained in:
yetone
2026-11-13 03:33:44 +08:00
parent f0571d7da5
commit 1f9d2c36c6
2 changed files with 189 additions and 20 deletions
+83 -20
View File
@@ -23,11 +23,11 @@ func GetYAML(path, keyPath string) (string, bool) {
if err != nil || root == nil {
return "", false
}
n := lookupYAML(root, strings.Split(keyPath, "."))
if n == nil || n.Kind != yaml.ScalarNode {
n := root.lookup(strings.Split(keyPath, "."))
if n == nil || n.kind != yaml.ScalarNode {
return "", false
}
return n.Value, true
return n.value, true
}
// GetYAMLMap reads the scalar entries of the mapping at a key path.
@@ -36,14 +36,14 @@ func GetYAMLMap(path, keyPath string) map[string]string {
if err != nil || root == nil {
return nil
}
n := lookupYAML(root, strings.Split(keyPath, "."))
if n == nil || n.Kind != yaml.MappingNode {
n := root.lookup(strings.Split(keyPath, "."))
if n == nil || n.kind != yaml.MappingNode {
return nil
}
out := map[string]string{}
for i := 0; i+1 < len(n.Content); i += 2 {
if v := n.Content[i+1]; v.Kind == yaml.ScalarNode {
out[n.Content[i].Value] = v.Value
for i := 0; i+1 < len(n.content); i += 2 {
if v := &n.content[i+1]; v.kind == yaml.ScalarNode {
out[n.content[i].value] = v.value
}
}
return out
@@ -55,14 +55,14 @@ func GetYAMLList(path, keyPath string) []string {
if err != nil || root == nil {
return nil
}
n := lookupYAML(root, strings.Split(keyPath, "."))
if n == nil || n.Kind != yaml.SequenceNode {
n := root.lookup(strings.Split(keyPath, "."))
if n == nil || n.kind != yaml.SequenceNode {
return nil
}
var out []string
for _, c := range n.Content {
if c.Kind == yaml.ScalarNode {
out = append(out, c.Value)
for i := range n.content {
if c := &n.content[i]; c.kind == yaml.ScalarNode {
out = append(out, c.value)
}
}
return out
@@ -70,9 +70,10 @@ func GetYAMLList(path, keyPath string) []string {
// GetYAMLText reads the value at a key path as the YAML it is written in —
// a list flow ("[a, b]") or block as it is — for SetYAML to put back as a
// YAMLText.
// YAMLText. It reads yaml.v3's whole tree, styles and comments, which the
// other looks don't keep (loadYAML).
func GetYAMLText(path, keyPath string) (string, bool) {
root, err := loadYAML(path)
root, err := filememo.Read("yaml text root", path, parseYAMLRoot)
if err != nil || root == nil {
return "", false
}
@@ -289,8 +290,8 @@ func JSONToYAML(src, dst string) error {
// loadYAML reads the first document's mapping without decoding its values.
// Unrelated duplicate keys, complex keys or invalid tags must not hide a
// readable model setting. Writers use loadYAMLForEdit to validate the whole file.
func loadYAML(path string) (*yaml.Node, error) {
root, err := filememo.Read("yaml root", path, parseYAMLRoot)
func loadYAML(path string) (*yamlLook, error) {
root, err := filememo.Read("yaml root", path, parseYAMLLook)
if errors.Is(err, fs.ErrNotExist) {
return nil, nil
}
@@ -300,9 +301,71 @@ func loadYAML(path string) (*yaml.Node, error) {
return root, nil
}
// parseYAMLRoot is loadYAML's parse, made once each time the file is read
// and shared by every look (GetYAML, GetYAMLMap, GetYAMLList,
// GetYAMLText), so read only. Each look parsed the file again: dsh's
// yamlLook is what the looks (GetYAML, GetYAMLMap, GetYAMLList) read of a
// node: its kind, a scalar's value and the nodes under it, held inline. A
// yaml.Node is ~160 bytes behind a pointer, with a tag, styles, comments
// and places none of them reads: kept for dsh's 68 KB settings.yaml it
// was ~4.5 MB of an idle tray's 10 MB heap.
type yamlLook struct {
kind yaml.Kind
value string
content []yamlLook
}
// parseYAMLLook is parseYAMLRoot's tree as a yamlLook.
func parseYAMLLook(b []byte) (*yamlLook, error) {
root, err := parseYAMLRoot(b)
if err != nil || root == nil {
return nil, err
}
l := lookOf(root)
return &l, nil
}
func lookOf(n *yaml.Node) yamlLook {
l := yamlLook{kind: n.Kind}
if n.Kind == yaml.ScalarNode {
l.value = n.Value
}
if len(n.Content) > 0 {
l.content = make([]yamlLook, len(n.Content))
for i, c := range n.Content {
if n.Kind == yaml.MappingNode && i%2 == 0 {
// a key is looked at by its value whatever its kind, as
// lookupYAML does
l.content[i] = yamlLook{kind: c.Kind, value: c.Value}
continue
}
l.content[i] = lookOf(c)
}
}
return l
}
// lookup is lookupYAML on a yamlLook.
func (n *yamlLook) lookup(parts []string) *yamlLook {
for _, p := range parts {
if n.kind != yaml.MappingNode {
return nil
}
var next *yamlLook
for i := 0; i+1 < len(n.content); i += 2 {
if n.content[i].value == p {
next = &n.content[i+1]
break
}
}
if next == nil {
return nil
}
n = next
}
return n
}
// parseYAMLRoot is the looks' parse, made once each time the file is read
// and shared by every look (GetYAML, GetYAMLMap, GetYAMLList through
// parseYAMLLook, GetYAMLText), so read only. Each look parsed the file again: dsh's
// 80 KB settings.yaml, read per field by every /api/drift poll, was
// 465 MB of what a live magpie allocated in 37 minutes.
func parseYAMLRoot(b []byte) (*yaml.Node, error) {
+106
View File
@@ -0,0 +1,106 @@
package edit
import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"gopkg.in/yaml.v3"
)
// TestYAMLLooksReadAsTheNodeTree holds GetYAML, GetYAMLMap and GetYAMLList,
// which read the slim tree loadYAML keeps, to what yaml.v3's own tree
// gives for every key path in a file with comments, anchors, aliases,
// flow and block lists, quoted and odd keys.
func TestYAMLLooksReadAsTheNodeTree(t *testing.T) {
src := `# head
base: &base
url: http://127.0.0.1:3425/v1 # line
key: "k"
providers:
magpie:
<<: *base
model: gpt-5.4
other: *base
"quoted.key": {a: 1, b: [x, y]}
? complex
: value
list:
- a
- {b: c}
- [d, e]
flow: [1, 2, three]
empty: {}
none:
num: 3
? [k1, k2]
: v
`
path := filepath.Join(t.TempDir(), "c.yaml")
if err := os.WriteFile(path, []byte(src), 0o600); err != nil {
t.Fatal(err)
}
root, err := parseYAMLRoot([]byte(src))
if err != nil {
t.Fatal(err)
}
var paths []string
var walk func(n *yaml.Node, pre string)
walk = func(n *yaml.Node, pre string) {
if n.Kind != yaml.MappingNode {
return
}
for i := 0; i+1 < len(n.Content); i += 2 {
p := n.Content[i].Value
if pre != "" {
p = pre + "." + p
}
paths = append(paths, p)
walk(n.Content[i+1], p)
}
}
walk(root, "")
paths = append(paths, "missing", "providers.missing", "num.deeper", "list.a")
if len(paths) < 20 {
t.Fatalf("walked only %d paths", len(paths))
}
for _, p := range paths {
n := lookupYAML(root, strings.Split(p, "."))
wantV, wantOK := "", false
var wantM map[string]string
var wantL []string
if n != nil {
switch n.Kind {
case yaml.ScalarNode:
wantV, wantOK = n.Value, true
case yaml.MappingNode:
wantM = map[string]string{}
for i := 0; i+1 < len(n.Content); i += 2 {
if n.Content[i+1].Kind == yaml.ScalarNode {
wantM[n.Content[i].Value] = n.Content[i+1].Value
}
}
case yaml.SequenceNode:
for _, c := range n.Content {
if c.Kind == yaml.ScalarNode {
wantL = append(wantL, c.Value)
}
}
}
}
if v, ok := GetYAML(path, p); v != wantV || ok != wantOK {
t.Errorf("GetYAML(%q) = %q, %v; the node tree reads %q, %v", p, v, ok, wantV, wantOK)
}
if m := GetYAMLMap(path, p); !reflect.DeepEqual(m, wantM) {
t.Errorf("GetYAMLMap(%q) = %v; the node tree reads %v", p, m, wantM)
}
if l := GetYAMLList(path, p); !reflect.DeepEqual(l, wantL) {
t.Errorf("GetYAMLList(%q) = %v; the node tree reads %v", p, l, wantL)
}
}
if v, ok := GetYAMLText(path, "flow"); !ok || v != "[1, 2, three]" {
t.Errorf("GetYAMLText(flow) = %q, %v; want the flow list as written", v, ok)
}
}