diff --git a/internal/gateway/tool_args_cr.go b/internal/gateway/tool_args_cr.go index bc8a1dec6..a741d86a6 100644 --- a/internal/gateway/tool_args_cr.go +++ b/internal/gateway/tool_args_cr.go @@ -236,6 +236,9 @@ type crTidy struct { buf []byte calls map[string]*crDropper // a streamed call's cut escape, by its index or item id texts map[string]*crBytes // a custom tool's input, by its item id + // looked is how many bytes were searched for a line's end or the + // escape, what a test bounds by the stream's length (#5282) + looked int } func (t *crTidy) write(b []byte) []byte { @@ -246,6 +249,7 @@ func (t *crTidy) write(b []byte) []byte { old := len(t.buf) t.buf = append(t.buf, b...) // the bytes before these were searched for a line's end when they came + t.looked += len(b) i := bytes.LastIndexByte(b, '\n') if i < 0 { return nil @@ -292,6 +296,7 @@ func (t *crTidy) cut() bool { // still has a tail waiting behind it, in whose room an unchanged region // would not survive being said from where it lies. func (t *crTidy) lines(b []byte, copy bool) []byte { + t.looked += len(b) if !bytes.Contains(b, crSeq) && !t.cut() { if copy { return append([]byte(nil), b...) @@ -332,6 +337,7 @@ func (t *crTidy) lines(b []byte, copy bool) []byte { // its event carries gone. A line with none of the escape, and no cut one // to answer, goes as it came. func (t *crTidy) line(l []byte) ([]byte, bool) { + t.looked += len(l) if !bytes.Contains(l, crSeq) && !t.cut() { return l, false } diff --git a/internal/gateway/tool_args_cr_test.go b/internal/gateway/tool_args_cr_test.go index 69ce8567e..4523100cd 100644 --- a/internal/gateway/tool_args_cr_test.go +++ b/internal/gateway/tool_args_cr_test.go @@ -1,6 +1,7 @@ package gateway import ( + "bytes" "encoding/json" "net/http/httptest" "strings" @@ -933,7 +934,11 @@ func TestGLMFlashScope(t *testing.T) { // line (a big response.completed) in 32KB reads. Without the escape the // line comes back as it came, at a scan's cost; with one, the call it // carries is read once and said again. The reads themselves were -// quadratic once (yetone's review of #4256 measured 2.43s). +// quadratic once (yetone's review of #4256 measured 2.43s). What is +// bounded is the work the code controls, the bytes searched and the bytes +// allocated, each a small multiple of the line's: a wall-clock bar +// measured the machine, and went red under -race beside heavy shards +// (16.9s on ubuntu CI, #5282). func TestCRTidyTwentyFourMBLine(t *testing.T) { unit := `{"type":"text","text":"hello \"world\" padding "}` rep := (24 << 20) / (len(unit) + 1) @@ -981,32 +986,43 @@ func TestCRTidyTwentyFourMBLine(t *testing.T) { name string line string want string + // times the line's size it may allocate: the held line grows a + // quarter at a time (~5x in all), and a mended line is also + // decoded into maps and said again (27x, 32x under -race); a + // copy of what is held on each read is ~400x + room uint64 }{ - {"no escape", big(false), big(false)}, - {"one call mended", big(true), ""}, + {"no escape", big(false), big(false), 8}, + {"one call mended", big(true), "", 40}, } { t.Run(c.name, func(t *testing.T) { tidy := &crTidy{proto: provider.Responses, sse: true} + line := []byte(c.line) + outs := make([][]byte, 0, len(line)/(32<<10)+2) start := time.Now() - var got []byte - for i := 0; i < len(c.line); i += 32 << 10 { - j := min(i+32<<10, len(c.line)) - if out := tidy.write([]byte(c.line[i:j])); out != nil { - got = append(got, out...) + made := allocated(func() { + for i := 0; i < len(line); i += 32 << 10 { + if out := tidy.write(line[i:min(i+32<<10, len(line))]); out != nil { + outs = append(outs, out) + } } - } - if out := tidy.flush(); out != nil { - got = append(got, out...) - } - elapsed := time.Since(start) - t.Logf("24MB line in 32KB reads: %s", elapsed) - if c.want == "" { + if out := tidy.flush(); out != nil { + outs = append(outs, out) + } + }) + t.Logf("24MB line in 32KB reads: %s, %d bytes searched, %d allocated", time.Since(start), tidy.looked, made) + if got := bytes.Join(outs, nil); c.want == "" { mended(t, string(got)) } else if string(got) != c.want { t.Fatal("the line came out other than wanted") } - if elapsed > 8*time.Second { - t.Fatalf("24MB took %s", elapsed) + // a read searched only for its own bytes, the line once + // for the escape, and the changed line once more + if n := len(line); tidy.looked > 3*n { + t.Errorf("a %d-byte line was searched through %d bytes, over 3 times its size", n, tidy.looked) + } + if n := uint64(len(line)); made > c.room*n { + t.Errorf("a %d-byte line allocated %d bytes, over %d times its size", n, made, c.room) } }) }