Files
magpie/internal/settings/shared_checked_test.go
T
yetone 47f70a077f perf: the allow list and state read settings without a copy of their own
Every request served by the GUI and the gateway goes through AllowGuard,
and AllowGuard read the allow list through settings.LoadChecked. That
call reflect-copies the whole Settings each time. /api/state did the same
through Load, then only encoded the result.

Changes:
- New settings.SharedChecked(). It is Shared plus LoadChecked's error.
  While settings.json parses whole it returns the settings parse already
  keeps, with no copy. Otherwise, or while a write is in progress, it
  falls back to loadChecked, so an unreadable file is still unknown,
  never an empty list.
- allowList reads AllowIPs through SharedChecked.
- state() reads settings through Shared. Nothing in gui writes into
  stateJSON.Settings (grepped for field, map and append writes).

Measured on mote with the real-account sandbox, allocations under
Settings.clone/deepCopy over 40 /api/state polls:
- before: 1.5 MB and 1 MB
- after: 0 and 0
The totals vary run to run (t32 34.6-35.1 MB, t33 24.8-30.8 MB).

Run on mote:
- go vet on settings, gateway and gui.
- go test, all of internal/settings.
- go test, internal/gateway -run "Allow|Remote|LAN|Lan|Origin|Guard".
- go test, internal/gui -run "State|Settings|Allow".
All ok.

New test: TestSharedCheckedReadsOnceAndKnowsUnknown checks that a change
is read and that a file that doesn't parse is an error. With the shared
path broken by hand (made to fall through to loadChecked), it goes red:
24 allocations against LoadChecked's 24.

Not tried: the full gateway and gui suites.
2026-11-10 08:50:13 +08:00

41 lines
1.5 KiB
Go

package settings
import (
"fmt"
"strings"
"testing"
)
// The gateway's allow list asks SharedChecked on every request: an
// unchanged settings.json gives its settings without a copy, a change is
// read at once, and a file that doesn't parse is unknown (an error), as
// LoadChecked says, never an empty list.
func TestSharedCheckedReadsOnceAndKnowsUnknown(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
var images []string
for i := range 60 {
images = append(images, fmt.Sprintf(`"p/m%d":true`, i))
}
writeRaw(t, `{"allowIPs":["10.0.0.0/8","192.168.1.5"],"redactWords":["a","b"],"pickedModels":{"codex":["p/m1"]},"modelImages":{`+strings.Join(images, ",")+`}}`)
s, err := SharedChecked()
if err != nil || len(s.AllowIPs) != 2 || s.AllowIPs[0] != "10.0.0.0/8" {
t.Fatalf("read %v, %v", s.AllowIPs, err)
}
shared := testing.AllocsPerRun(50, func() { SharedChecked() })
copied := testing.AllocsPerRun(50, func() { LoadChecked() })
if shared > 6 || shared*4 > copied {
t.Errorf("an unchanged file: %.0f allocations a read (LoadChecked's copy: %.0f), want no copy", shared, copied)
}
writeRaw(t, `{"allowIPs":["127.0.0.1"]}`)
if s, err := SharedChecked(); err != nil || len(s.AllowIPs) != 1 || s.AllowIPs[0] != "127.0.0.1" {
t.Fatalf("a change: %v, %v", s.AllowIPs, err)
}
writeRaw(t, `{"allowIPs":["127.0.0.1"`)
if _, err := SharedChecked(); err == nil {
t.Fatal("a file that doesn't parse read as known")
}
if _, err := LoadChecked(); err == nil {
t.Fatal("LoadChecked disagrees")
}
}