diff --git a/internal/usage/codex_identity.go b/internal/usage/codex_identity.go index ac417cac0..4a19cded6 100644 --- a/internal/usage/codex_identity.go +++ b/internal/usage/codex_identity.go @@ -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 { diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index f97360a25..4b70e94e3 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -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 { diff --git a/internal/usage/request_snapshot.go b/internal/usage/request_snapshot.go index bb60f5d21..36c2290c1 100644 --- a/internal/usage/request_snapshot.go +++ b/internal/usage/request_snapshot.go @@ -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++ {