turnOf (affinity), dirOf (the trace's folder) and tellWorkPlace (a plugin's work-place headers) each parsed the whole conversation for a number and a folder. A parse decodes every message and tool output into strings of its own, so a long conversation was decoded twice over on every request for them. In the 60-min soak before (dee5c9d75, 4746 requests), parse was 26.4 GB of 106.7 GB allocated: turnOf 11.5 GB and dirOf 11.4 GB. factsOf parses once for the turn count, the folder, and whether the text says it is a git repository (bodyFacts). For a body being served (holdBody), it keeps those facts, a few words, while the body is held. The pause-by-folder check reads the same facts. It keeps the facts, not the parse. A first version kept the *Request weakly. In the served benchmark the weak pointer was already collected between turnOf and dirOf on 1 read in 6, and under real load the collector runs more often. Keeping the parse strongly would keep a copy of the conversation for the whole request. The facts are kept only while their body is listed whole; the entry goes when the body is released, so a body's memory given to another never reads as this one. A body not held, or read in another protocol, is parsed for its own. BenchmarkLargeRequest, ~2 MB conversation, B/op before → after: anthropic 53.81 → 45.84 MB anthropic-group 65.26 → 57.30 MB chat 43.27 → 36.61 MB responses 53.81 → 46.04 MB 6.7 to 8.0 MB less per request, about 3.5× the body. Tests (parse_memo_test.go): - TestReadersOfAHeldBodyParseItOnce: what the three readers say equals a parse of their own. Together they allocate at most 1.5× one parse, and readers after the first allocate almost nothing. A Chat read of the same bytes is its own, and a copy not held is parsed again. - TestHeldFactsGoWithTheirBody: the entry goes when the request ends, a released body is not listed again, and a prefix of a held body is not the body. Broken by hand: - factsOf always parsing: red, "turnOf, dirOf and tellWorkPlace allocate 446064 bytes on a held body, a parse 135096", and "a held body's facts were not kept". - The delete at release removed: red, "a body's facts are still listed after its request ended". TestPauseByFolder, TestRuleByFolder, the WorkDir and WorkPlace tests and the affinity and turn tests pass, with -race too. Under -race the readers' byte bound is skipped (raceOn, as request_zstd_pool_test does): sync.Pool drops a quarter of what is put in it there, and workDirIn's regexps take their ~37 KB matchers from one. Five tries read 1.3x to 2.1x a parse, while the parse stayed at 135304 bytes; -race -count=3 went red at 1.57x before the skip. Run (darwin arm64, go1.26.8, sandbox HOME, load 40 to 90 from other sessions' suites): - go test ./... -count=1 -timeout 45m on the series' final tree atba319a758: exit 0, all 68 packages ok (gateway 1498s, agent 1633s). - After the rebase ontob855cfee9(two redact commits): go test ./internal/redact ./internal/gateway -count=1 -timeout 45m, both ok (gateway 917s). - go test -race -count=3 on the Held, ReadersOfAHeldBody, RecentRing, RequestBody, CaptureRequest, folder, WorkPlace, WorkDir and Turn tests: ok, after TestReadersOfAHeldBodyParseItOnce's byte bound was skipped under -race (the third commit says why); that test and TestHeldFactsGoWithTheirBody then passed -count=30 with and without -race. - Each commit alone: go vet ./internal/gateway and gofmt -l clean. - Final tree: gofmt -l clean; go vet ./... for darwin, GOOS=windows and GOOS=linux -tags nogui, all exit 0. Not tried: a second 60-minute soak after the series (the one before is the "before" here; the "after" numbers are the benchmark's), the GUI suite (nothing in it changed), Windows and Linux test runs (vet only).
78 lines
2.2 KiB
Go
78 lines
2.2 KiB
Go
package gateway
|
|
|
|
import (
|
|
"unsafe"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// bodyFacts is what the readers of a request's conversation that only
|
|
// read it take from it: the turn count (turnOf), the folder its agent
|
|
// works in and whether that is a git repository, as the text says
|
|
// (dirOf, tellWorkPlace). A body that doesn't parse says none of them.
|
|
type bodyFacts struct {
|
|
turn int
|
|
within bool
|
|
dir, git string
|
|
}
|
|
|
|
// factsOf is bodyFacts of body, read in protocol proto. Each of its
|
|
// readers parsed the whole conversation, every message and tool output
|
|
// decoded into strings of its own, one just after the other: a 2 MB
|
|
// conversation was decoded twice over on every request for a number and
|
|
// a folder (yg6888 on X: ~1 GB).
|
|
//
|
|
// For a body being served (holdBody), the facts are kept while the body
|
|
// is held, so it is parsed for them once: they are a few words, kept
|
|
// whole, where keeping the parse itself would keep a copy of the
|
|
// conversation for the request's life.
|
|
func factsOf(proto provider.Protocol, body []byte) bodyFacts {
|
|
if len(body) < validMemoMin {
|
|
return readFacts(proto, body)
|
|
}
|
|
key := unsafe.SliceData(body)
|
|
heldBodies.Lock()
|
|
e, ok := heldBodies.facts[key]
|
|
heldBodies.Unlock()
|
|
if ok && e.n == len(body) && e.proto == proto {
|
|
return e.facts
|
|
}
|
|
f := readFacts(proto, body)
|
|
heldBodies.Lock()
|
|
defer heldBodies.Unlock()
|
|
// only while body is held, whole: one released may be freed, and its
|
|
// memory given to another body that would read as this one. A body
|
|
// read in two protocols keeps the last.
|
|
if l := heldBodies.list.Load(); l != nil {
|
|
for _, b := range *l {
|
|
if unsafe.SliceData(b) == key && len(b) == len(body) {
|
|
if heldBodies.facts == nil {
|
|
heldBodies.facts = map[*byte]heldFacts{}
|
|
}
|
|
heldBodies.facts[key] = heldFacts{n: len(body), proto: proto, facts: f}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return f
|
|
}
|
|
|
|
// readFacts parses body for its bodyFacts.
|
|
func readFacts(proto provider.Protocol, body []byte) bodyFacts {
|
|
req, err := parse(proto, body)
|
|
if err != nil {
|
|
return bodyFacts{}
|
|
}
|
|
var f bodyFacts
|
|
f.turn, f.within = turnIn(req)
|
|
f.dir, f.git = workPlace(req)
|
|
return f
|
|
}
|
|
|
|
// heldFacts is a held body's bodyFacts (factsOf).
|
|
type heldFacts struct {
|
|
n int
|
|
proto provider.Protocol
|
|
facts bodyFacts
|
|
}
|