Each /api/state and /api/drift poll asked the stash (stash.json) for one value hundreds of times, through Detected, Drift and the agents' wiring checks, and each ask read the file and parsed it again. In a real-account heap profile (20 rounds of drift, state and providers) stashLoad was 24-34 MB of what was allocated. stashShared reads it through filememo.Read, so every look shares one parse until the file changes, by magpie or by another process. Every read-only caller uses it now, the 24 files here and QwenPaw's unstashPeek. stashLoad, which callers change and write back, still gives each its own map, cloned from the same parse. Real use: the owner's real-account sandbox, offline (proxy to :9), 20 warm rounds of /api/drift, /api/state and /api/providers: - allocated 477 MB (d307aa648) -> 411 MB; stashLoad is out of the profile. - /api/state and /api/drift bytes identical to d307aa648's (cmp). Run on mote (mote-run, Go 1.26.8), at this head rebased ond307aa648: - gofmt clean; go vet ok, also GOOS=windows and GOOS=linux. - TestStashLooksShareOneRead alone, 3 times: ok. - Broken by hand (stashShared reading through stashLoad): red, "a look at the stash read and parsed it again". - go test -timeout 45m ./internal/agent ./internal/gateway: ok (263s, 691s).
37 lines
1.1 KiB
Go
37 lines
1.1 KiB
Go
package agent
|
|
|
|
import (
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
// A look at the stash's values reads and parses stash.json once, not once
|
|
// for each look, and still sees each change: magpie's own, and one another
|
|
// process wrote.
|
|
func TestStashLooksShareOneRead(t *testing.T) {
|
|
stashHome(t)
|
|
stash(map[string]string{"a.was": "1"})
|
|
first := reflect.ValueOf(stashShared()).UnsafePointer()
|
|
for range 20 {
|
|
if reflect.ValueOf(stashShared()).UnsafePointer() != first {
|
|
t.Fatal("a look at the stash read and parsed it again")
|
|
}
|
|
}
|
|
own := stashLoad()
|
|
own["a.was"] = "mine"
|
|
if got := stashShared()["a.was"]; got != "1" {
|
|
t.Fatalf("a change to a caller's own stash reached the shared one: %q", got)
|
|
}
|
|
stash(map[string]string{"b.was": "2"})
|
|
if got := stashShared()["b.was"]; got != "2" {
|
|
t.Fatalf("magpie's own change not seen: %q", got)
|
|
}
|
|
if err := os.WriteFile(stashPath(), []byte(`{"c.was": "theirs, longer"}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s := stashShared(); s["c.was"] != "theirs, longer" || s["a.was"] != "" {
|
|
t.Fatalf("another process's write not seen: %v", s)
|
|
}
|
|
}
|