The collector that every encoder embeds (and conversations, hooks,
relay status, search, the Claude subscription path) added each text or
thinking delta to its part with `p.Text += ev.Text`, copying the whole
reply so far on every delta. Quadratic in the reply: a 4000-delta reply
copied ~57 KB per delta. It shows most on a translated stream (a Chat
upstream served to an Anthropic client), where every chunk is one delta.
Now the last part's text grows in a strings.Builder, written into the
part when another part is touched or at finish().
Guard: TestCollectorTextLinear fails when collecting a reply allocates
more than 16x its size. Before the fix: "collecting a 116014-byte reply
allocated 247395704 bytes (2132x)". TestCollectorKeepsEveryDelta checks
that every part's text, the signature and the tool args are kept whole.
Numbers (a fake upstream, no network, 4000 events, chat -> anthropic
stream, CPU from rusage since the box sits at load ~350):
before: 166.9 cpu-ms/op, 231 MB/op, 391k allocs/op
after: 95.4 cpu-ms/op, 31.3 MB/op, 387k allocs/op
BenchmarkCollectorText (4000 deltas): 667 KB/op, 8048 allocs/op after.
Passthrough cases were unchanged (small-stream-pass 42 ms, chat pass 94 ms).
Run (on ccb2eea15 with this commit and the next one, jsonValid's memo,
both in the tree, sandbox HOME, load ~350):
- go test -tags nogui ./... -timeout 45m -count=1: 62 packages ok,
internal/gateway ok (2694s). Two reds, both in untouched packages and
both filed:
- internal/gui TestSetAnswersWhileDevinAndProvidersHang: set slow/slow-1
took 10.67s against its 10s bound (#1375). Rerun alone with -count=3,
it passed at 4.7s, 2.8s and 7.1s.
- internal/agent ran past 45m, with TestWSLMoreAgentsPick running at the
deadline (#2544).
- gofmt -l internal/: clean.
- go vet ./internal/gateway passed.
- GOOS=linux and GOOS=windows go vet -tags nogui ./... passed.
- go build passed.
- TestCollector* pass, and TestCollectorTextLinear is red on the old ir.go.
89 lines
2.8 KiB
Go
89 lines
2.8 KiB
Go
package gateway
|
||
|
||
import (
|
||
"runtime"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// collectStream is a streamed reply as the collector takes it: n deltas
|
||
// of thinking, then n of text, then a tool call and more text.
|
||
func collectStream(c *collector, n int) {
|
||
c.add(Event{Kind: KStart, MsgID: "m", Model: "x"})
|
||
for i := range n {
|
||
c.add(Event{Kind: KThink, Text: "think " + string(rune('a'+i%26)) + " "})
|
||
}
|
||
c.add(Event{Kind: KSig, Text: "sig"})
|
||
for i := range n {
|
||
c.add(Event{Kind: KText, Text: "word " + string(rune('a'+i%26)) + " of the reply, "})
|
||
}
|
||
c.add(Event{Kind: KToolStart, ID: "t1", Name: "Read"})
|
||
c.add(Event{Kind: KToolArgs, Text: `{"path":"/a"}`})
|
||
c.add(Event{Kind: KText, Text: "after "})
|
||
c.add(Event{Kind: KText, Text: "the call"})
|
||
c.add(Event{Kind: KStop, Stop: "stop"})
|
||
}
|
||
|
||
// The collector's reply is the stream's, delta for delta: each part's
|
||
// text whole, in its own part, a part begun after a call its own.
|
||
func TestCollectorKeepsEveryDelta(t *testing.T) {
|
||
var c collector
|
||
collectStream(&c, 500)
|
||
res := c.finish()
|
||
var think, text strings.Builder
|
||
for i := range 500 {
|
||
think.WriteString("think " + string(rune('a'+i%26)) + " ")
|
||
text.WriteString("word " + string(rune('a'+i%26)) + " of the reply, ")
|
||
}
|
||
if len(res.Parts) != 4 {
|
||
t.Fatalf("parts %+v", res.Parts)
|
||
}
|
||
if p := res.Parts[0]; p.Kind != Thinking || p.Text != think.String() || p.Signature != "sig" {
|
||
t.Errorf("thinking %q sig %q", p.Text[:min(40, len(p.Text))], p.Signature)
|
||
}
|
||
if p := res.Parts[1]; p.Kind != Text || p.Text != text.String() {
|
||
t.Errorf("text %d bytes, want %d", len(p.Text), text.Len())
|
||
}
|
||
if p := res.Parts[2]; p.Kind != ToolCall || string(p.Args) != `{"path":"/a"}` {
|
||
t.Errorf("call %+v", p)
|
||
}
|
||
if p := res.Parts[3]; p.Kind != Text || p.Text != "after the call" {
|
||
t.Errorf("last text %q", p.Text)
|
||
}
|
||
}
|
||
|
||
// A streamed reply's text is gathered in time linear in its length: the
|
||
// collector used to add each delta to the part's string, copying the whole
|
||
// reply so far every time, about 57 KB a delta on a 4000-delta reply (a
|
||
// Chat upstream to an Anthropic client copied 229 MB to stream 88 KB).
|
||
func TestCollectorTextLinear(t *testing.T) {
|
||
const n = 4000
|
||
var ms runtime.MemStats
|
||
runtime.GC()
|
||
runtime.ReadMemStats(&ms)
|
||
before := ms.TotalAlloc
|
||
var c collector
|
||
collectStream(&c, n)
|
||
res := c.finish()
|
||
runtime.ReadMemStats(&ms)
|
||
got := ms.TotalAlloc - before
|
||
size := 0
|
||
for _, p := range res.Parts {
|
||
size += len(p.Text)
|
||
}
|
||
// the strings themselves, the builder's doublings, the parts: a few
|
||
// times the reply. The quadratic copy was ~150× it here.
|
||
if got > uint64(16*size) {
|
||
t.Fatalf("collecting a %d-byte reply allocated %d bytes (%.0f×)", size, got, float64(got)/float64(size))
|
||
}
|
||
}
|
||
|
||
func BenchmarkCollectorText(b *testing.B) {
|
||
b.ReportAllocs()
|
||
for b.Loop() {
|
||
var c collector
|
||
collectStream(&c, 4000)
|
||
c.finish()
|
||
}
|
||
}
|