usage: the passes that pair a page's calls read a chunk's strings in place (#3493)
A Requests page decodes each packed session chunk in five passes, one chunk at a time (chunkRows). Every decode copied the chunk's whole string section out of the pooled buffer, though four of the passes (folding Codex's repeated calls, pairing Codex response ids, Claude's repeated message ids, request-id matching) only look strings up and clone the few they keep. That copy was the largest allocation of a warm start's page. Those four passes now read through lentRows: the chunk is decoded into a buffer the reader owns, and its strings are of that buffer until the next chunk is decoded over it. The cell pass and every reader that keeps rows still copy. What was run, at this head: - Real data (sbpairs: 421,206 calls, 15.6k session files), warm-up steps three runs each, before / after: step 2 (first Requests page): 0.88-1.03s / 0.76-0.79s, ~560 MB / 446 MB allocated; whole warm-up 1.41-1.45 GB / 1.21 GB allocated; maxrss 320-336 MB / 318-319 MB. - Real pages, base and this change alternately: QueryPage of All, today, 7d and 30d at offsets 0 and 500, and CodexModelEffort(All). Totals, rows and every string are the same; the only differences are float costs' last digits, from summation order, which differ between two base runs as well (and one new live call). - Broken by hand: storing the lent message id and request id without their clones turns TestRequestPageSameFromPackedLocalChunks red. - go test ./internal/usage ./internal/gui ./internal/sessions ok; -race -run 'TestRequest|Codex|Packed|Visible|Match' ./internal/usage ok; GOOS=linux and GOOS=windows go vet -tags nogui of usage and gui clean; gofmt clean. Not tried: the full ./... suite (one suite per machine; only the packages this reaches were run).
This commit is contained in:
@@ -213,8 +213,9 @@ func codexChunkResponsePairs(gateways, locals []*rowChunk, duplicateLocal map[ro
|
||||
p.gatewayOK = isCodexRow(row) && !row.IsRejected() && row.Source != SourceReported
|
||||
}
|
||||
}
|
||||
lent := lentRows() // picks are looked up by a local's id, not keyed
|
||||
for _, chunk := range locals {
|
||||
open := rows.of(chunk)
|
||||
open := lent.of(chunk)
|
||||
for i := range open.Rows {
|
||||
ref := rowRef{chunk, i}
|
||||
if duplicateLocal[ref] {
|
||||
@@ -248,7 +249,7 @@ func foldCodexRows(locals []*rowChunk) map[rowRef]bool {
|
||||
seed := maphash.MakeSeed()
|
||||
first := map[uint64]rowRef{}
|
||||
again := map[uint64][]rowRef{}
|
||||
var rows chunkRows
|
||||
rows := lentRows() // an id is kept as its hash
|
||||
for _, chunk := range locals {
|
||||
open := rows.of(chunk)
|
||||
for i := range open.Rows {
|
||||
|
||||
@@ -403,8 +403,18 @@ type chunkRows struct {
|
||||
buf []packedRow
|
||||
strs []string
|
||||
shapes [][rowTexts]uint32
|
||||
// lent: the strings are of the decoded block, not a copy of them, and
|
||||
// the next chunk is decoded over them. For a pass that keeps nothing of
|
||||
// a row past the next chunk, and clones a string it keeps: the passes
|
||||
// that pair a page's calls copied every chunk's strings five times,
|
||||
// ~250 MB of a warm start's page (#3493).
|
||||
lent bool
|
||||
decoded []byte
|
||||
}
|
||||
|
||||
// lentRows is a chunkRows whose strings are lent (chunkRows.lent).
|
||||
func lentRows() chunkRows { return chunkRows{lent: true} }
|
||||
|
||||
// of is c's rows to read. What the page set on c since (a Codex actor or
|
||||
// compaction, the identities) is read through c.
|
||||
func (r *chunkRows) of(c *rowChunk) *rowChunk {
|
||||
@@ -412,7 +422,11 @@ func (r *chunkRows) of(c *rowChunk) *rowChunk {
|
||||
return c
|
||||
}
|
||||
if r.c != c {
|
||||
open := c.unpackReusing(r.buf, r.strs, r.shapes)
|
||||
var lent *[]byte
|
||||
if r.lent {
|
||||
lent = &r.decoded
|
||||
}
|
||||
open := c.unpackReusing(r.buf, r.strs, r.shapes, lent)
|
||||
if open == nil { // never written so: pack wrote it in this process
|
||||
open = &rowChunk{Source: c.Source, Computer: c.Computer}
|
||||
}
|
||||
@@ -1091,7 +1105,7 @@ func (idx *requestIndex) trim(bound int64) {
|
||||
func visibleLocal(chunks []*rowChunk) map[rowRef]bool {
|
||||
duplicates := map[rowRef]bool{}
|
||||
seen := map[string]bool{}
|
||||
var rows chunkRows
|
||||
rows := lentRows() // a kept message id is cloned
|
||||
for _, c := range chunks {
|
||||
open := rows.of(c)
|
||||
for i := range open.Rows {
|
||||
@@ -1183,7 +1197,7 @@ func matchedBlocks(gateways []*rowChunk, chunks []*rowChunk, skip map[rowRef]boo
|
||||
}
|
||||
}
|
||||
byRequest := map[string][]timedRef{}
|
||||
var rows chunkRows
|
||||
rows := lentRows() // a kept request id is cloned; a key is looked up
|
||||
for _, c := range chunks {
|
||||
open := rows.of(c)
|
||||
for i := range open.Rows {
|
||||
|
||||
@@ -134,18 +134,23 @@ func (k keptStrings) of(s string) string {
|
||||
// unpackInto is unpack decoding the rows into rows when it is large enough:
|
||||
// every field of a row is written, so a reused slice needs no clearing.
|
||||
func (c *rowChunk) unpackInto(into []packedRow) *rowChunk {
|
||||
return c.unpackReusing(into, nil, nil)
|
||||
return c.unpackReusing(into, nil, nil, nil)
|
||||
}
|
||||
|
||||
// unpackReusing is unpackInto decoding the strings' and shapes' slices into
|
||||
// strs and shapes too, when they are large enough: for a reader that lets
|
||||
// go of a chunk's rows before it opens the next (chunkRows).
|
||||
func (c *rowChunk) unpackReusing(into []packedRow, strs []string, shapes [][rowTexts]uint32) *rowChunk {
|
||||
// go of a chunk's rows before it opens the next (chunkRows). With lent, the
|
||||
// block is decoded into *lent and the strings are of it, not a copy: they
|
||||
// are good until *lent is decoded into again.
|
||||
func (c *rowChunk) unpackReusing(into []packedRow, strs []string, shapes [][rowTexts]uint32, lent *[]byte) *rowChunk {
|
||||
if c == nil || len(c.Archive) == 0 {
|
||||
return c
|
||||
}
|
||||
buf := rowDecodes.Get().(*[]byte)
|
||||
defer rowDecodes.Put(buf)
|
||||
buf := lent
|
||||
if buf == nil {
|
||||
buf = rowDecodes.Get().(*[]byte)
|
||||
defer rowDecodes.Put(buf)
|
||||
}
|
||||
data, err := s2.Decode((*buf)[:cap(*buf)], c.Archive)
|
||||
if err != nil {
|
||||
return nil
|
||||
@@ -192,7 +197,12 @@ func (c *rowChunk) unpackReusing(into []packedRow, strs []string, shapes [][rowT
|
||||
if !valid {
|
||||
return nil
|
||||
}
|
||||
text := string(section[:len(section)-len(data)])
|
||||
var text string
|
||||
if lent != nil {
|
||||
text = unsafe.String(unsafe.SliceData(section), len(section)-len(data))
|
||||
} else {
|
||||
text = string(section[:len(section)-len(data)])
|
||||
}
|
||||
for i := range next.Strings {
|
||||
var n, shift uint64
|
||||
for k := 0; ; k++ {
|
||||
|
||||
Reference in New Issue
Block a user