edit, agent: Grok Build's own model tables are read in one pass with nothing built, and its efforts read the shared list

What /api/state still allocated for Grok Build with real accounts, forty
polls on mote (t30):

- grokOwnOptions asked GetTOMLTable for each [model."<id>"] table of
  ~/.grok/config.toml: a stat and a fresh map a table on every look
  (4.6 MB). edit.EachTOMLTable walks the file's kept parse once and
  hands each ordinary table, in TOMLTables' order, with Scalar reading
  a key as GetTOMLTable's map has it (the last of a key written twice).
- the model field's efforts took magpieModels("grok"), a copy of the
  whole list per look, to read one entry (4.5 MB). It reads
  sharedModels; catalog.Efforts already returned a slice of the shared
  entry.

Measured on mote, sandbox HOME with the owner's real account data, 5
warm-up then 40 calls, paired runs:
  /api/state  t30 42.3 / 43.2 MB  ->  t31 33.2 / 38.1 MB  (-16%)
No GetTOMLTable, grokOwnOptions or magpieModels frame is left in t31's
top list.

Tests:
- TestEachTOMLTableAgreesWithGetTOMLTable: 200 model tables, an array
  table and a plain one; names match TOMLTables, every Scalar matches
  GetTOMLTable's map, a look allocates at most 5 times, a missing file
  has no tables and no error.

Run on mote at this tree: gofmt -l clean; go vet of edit and agent;
go test -count=1 of internal/edit and internal/agent (247s): ok.
Not run: the full ./... suite and the GUI suite; no JS changed.
This commit is contained in:
yetone
2026-11-10 07:46:15 +08:00
parent ca5e537ecb
commit 216ac6f850
3 changed files with 109 additions and 17 deletions
+14 -17
View File
@@ -231,7 +231,7 @@ func grokIn(at place) *Agent {
if !ok {
return nil
}
return catalog.Efforts(magpieModels("grok"), ref)
return catalog.Efforts(sharedModels("grok"), ref) // only read
}
return atomic(&Agent{
ID: "grok", Name: "Grok Build", Icon: "xai", Aliases: []string{"grok-build", "grok-cli"}, Spelled: prefixed,
@@ -425,23 +425,20 @@ func grokOwnOptions(dir, path, cur string) []Option {
}
// the user's own [model."<id>"] tables, each on its own endpoint
if path != "" {
if tables, err := edit.TOMLTables(path); err == nil {
for _, t := range tables {
id, ok := strings.CutPrefix(t, "model.")
if !ok {
continue
}
if u, err := strconv.Unquote(id); err == nil {
id = u
}
kv, _ := edit.GetTOMLTable(path, t)
direct := "xAI"
if h := hostOf(kv["base_url"]); h != "" {
direct = h
}
add(id, kv["name"], direct)
_ = edit.EachTOMLTable(path, func(t edit.TOMLTable) {
id, ok := strings.CutPrefix(t.Name(), "model.")
if !ok {
return
}
}
if u, err := strconv.Unquote(id); err == nil {
id = u
}
direct := "xAI"
if h := hostOf(t.Scalar("base_url")); h != "" {
direct = h
}
add(id, t.Scalar("name"), direct)
})
}
if cur != "" && !seen[cur] && !strings.HasPrefix(cur, magpieID+"/") {
out = append([]Option{{Value: cur, Icon: modelIcon("", cur), Direct: "xAI"}}, out...)
+35
View File
@@ -38,6 +38,41 @@ func TOMLTables(path string) ([]string, error) {
return doc.names, nil
}
// TOMLTable is one ordinary table of a file EachTOMLTable read, shared
// with the file's kept parse, so read only.
type TOMLTable struct{ span *tomlTableSpan }
// Name is the table's name as TOMLTables lists it.
func (t TOMLTable) Name() string { return t.span.name }
// Scalar is the value of one scalar key as GetTOMLTable has it, "" when
// the table has none: the last of a key written twice, as the map holds.
func (t TOMLTable) Scalar(key string) string {
for i := len(t.span.keys) - 1; i >= 0; i-- {
if kv := &t.span.keys[i]; kv.scalar && kv.name == key {
return kv.value
}
}
return ""
}
// EachTOMLTable calls fn with each ordinary table, in the order
// TOMLTables lists them, from one look at the file and nothing built: an
// agent listing its config's tables asked GetTOMLTable for each, a stat
// and a map a table on every look.
func EachTOMLTable(path string, fn func(TOMLTable)) error {
tables, err := tomlTablesOf(path)
if err != nil {
return err
}
for i := range tables {
if !tables[i].array {
fn(TOMLTable{&tables[i]})
}
}
return nil
}
// GetTOMLTable returns the scalar keys of one table, or (nil, nil) when
// the file or table is absent. Read and parse errors are returned with the path.
// Strings are decoded; other scalars (booleans, numbers, dates, and times)
+60
View File
@@ -0,0 +1,60 @@
package edit
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
// EachTOMLTable gives what TOMLTables and GetTOMLTable give, table by
// table, without a map or a stat for each: Grok Build's model list reads
// every [model."<id>"] table of its config on each look.
func TestEachTOMLTableAgreesWithGetTOMLTable(t *testing.T) {
var b strings.Builder
b.WriteString("top = 1\n\n[[array]]\nname = \"skipped\"\n\n[plain]\nname = \"p\"\nlist = [1, 2]\n\n")
for i := range 200 {
fmt.Fprintf(&b, "[model.\"m-%d\"]\nname = \"Model %d\"\nbase_url = 'https://h%d.example/v1' # own\n\n", i, i, i)
}
path := filepath.Join(t.TempDir(), "config.toml")
if err := os.WriteFile(path, []byte(b.String()), 0o600); err != nil {
t.Fatal(err)
}
names, err := TOMLTables(path)
if err != nil {
t.Fatal(err)
}
var got []string
if err := EachTOMLTable(path, func(tb TOMLTable) {
got = append(got, tb.Name())
want, _ := GetTOMLTable(path, tb.Name())
for _, k := range []string{"name", "base_url", "list", "missing"} {
if v := tb.Scalar(k); v != want[k] {
t.Errorf("%s.%s: %q, GetTOMLTable has %q", tb.Name(), k, v, want[k])
}
}
}); err != nil {
t.Fatal(err)
}
if strings.Join(got, "|") != strings.Join(names, "|") {
t.Fatalf("tables %v, TOMLTables lists %v", got, names)
}
seen := 0
if n := testing.AllocsPerRun(10, func() {
seen = 0
EachTOMLTable(path, func(tb TOMLTable) {
if tb.Scalar("base_url") != "" {
seen++
}
})
}); n > 5 {
t.Errorf("a look at 201 tables: %.0f allocations, want a few", n)
}
if seen != 200 {
t.Errorf("tables with a base_url: %d", seen)
}
if err := EachTOMLTable(filepath.Join(t.TempDir(), "none.toml"), func(TOMLTable) { t.Error("a missing file has tables") }); err != nil {
t.Errorf("missing file: %v", err)
}
}