usage: a poll's small pairing lets a whole history's scratch go (#2278)
The matcher, the response picks and visibleLocal's set of ids seen are kept for the next pairing, and each pairing that puts one back holds it scratchLinger more. A poll's pairing of its last few days put back the scratch the cold start's pairing of the whole history made, sized for every call, so ~140 MB of maps and slabs stayed while the window polled: the heap profiles at 44, 50, 56, 62 and 68 s were alike to the MB, all of it under codexEffortOfIndex, with no pairing of the history running. Each scratch now records the rows of the largest call it served, and a call over a quarter of that or less (and over 4096 rows' floor) makes its own; the large one goes with the collection. A call alike in size takes what is kept, as before, so the polls while an agent works still reuse it. holdBigBuild counts its rows through the same rowsOfAll. Run on mote at this head (parent7d4a63148, rebased; usage ok 21.4s, usageapi ok 1.6s there): - go vet ./internal/usage: ok - go test ./internal/usage ./internal/usageapi: ok 18.0s, ok 0.8s - TestSmallPairingLetsLargeScratchGo with scratchFits always true: red ("a small pairing kept the match scratch of a large one"); green with it back, beside TestPairScratchGoesWhenUnused, -KeepsNothingBetweenCalls and TestVisibleLocalSeenKeptEmpty. - gofmt -l clean. Real history (the owner's full ledger and session files, sandbox HOME, caches deleted, the GUI's polls for 120 s), footprint every 5 s: - this head: peak 532 and 539 MB; after the peak 367–374 MB while it polled; CPU 3:29 both. -e31ccf230in the same session: peak 556 MB (548–566 before); after the peak 416–419 MB; CPU 3:38. The peak itself is the pairing of the whole history while it runs, beside the Month page and the session stats; this change doesn't cut that. Not tried: Windows and Linux runs.
This commit is contained in:
@@ -196,6 +196,7 @@ type responsePick struct {
|
||||
// slab were ~775 MB of what 80 s of polls allocated while calls came in
|
||||
// (#2278).
|
||||
type pickScratch struct {
|
||||
made int // the gateway's rows of the largest call it served
|
||||
picks map[string]int32
|
||||
slab []responsePick
|
||||
picked map[uint64]bool
|
||||
@@ -235,10 +236,12 @@ func codexChunkResponsePairs(gateways, locals []*rowChunk, duplicateLocal refSet
|
||||
}
|
||||
}
|
||||
}
|
||||
need := rowsOfAll(gateways)
|
||||
scratch := keptPicks.Swap(nil)
|
||||
if scratch == nil {
|
||||
if scratch == nil || !scratchFits(scratch.made, need) {
|
||||
scratch = &pickScratch{}
|
||||
}
|
||||
scratch.made = max(scratch.made, need)
|
||||
defer scratch.release()
|
||||
scratch.picks = mapFor(scratch.picks, withID)
|
||||
picks := scratch.picks
|
||||
|
||||
@@ -121,6 +121,42 @@ func TestPairScratchGoesWhenUnused(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A pairing much smaller than the one a scratch was made for makes its
|
||||
// own, and the large one goes (#2278): a poll's pairing of its last days
|
||||
// kept the whole history's ~140 MB at a cold start while the window polled.
|
||||
// One alike in size takes what is kept.
|
||||
func TestSmallPairingLetsLargeScratchGo(t *testing.T) {
|
||||
was := scratchFloor
|
||||
scratchFloor = 1
|
||||
t.Cleanup(func() {
|
||||
scratchFloor = was
|
||||
ReleaseScratch()
|
||||
})
|
||||
now := time.Date(2026, 10, 1, 13, 0, 0, 0, time.UTC)
|
||||
bg, bl := pairFixture(40, 20)
|
||||
sg, sl := pairFixture(2, 3)
|
||||
ReleaseScratch()
|
||||
pairRows(All, now, bg, bl, nil, nil)
|
||||
match, picks, seen := keptMatch.Load(), keptPicks.Load(), keptSeen.Load()
|
||||
if match == nil || picks == nil || seen == nil {
|
||||
t.Fatal("nothing kept for the next call")
|
||||
}
|
||||
pairRows(All, now, bg, bl, nil, nil)
|
||||
if keptMatch.Load() != match || keptPicks.Load() != picks || keptSeen.Load() != seen {
|
||||
t.Fatal("a pairing alike in size made scratch of its own")
|
||||
}
|
||||
pairRows(All, now, sg, sl, nil, nil)
|
||||
if keptMatch.Load() == match {
|
||||
t.Fatal("a small pairing kept the match scratch of a large one")
|
||||
}
|
||||
if keptPicks.Load() == picks {
|
||||
t.Fatal("a small pairing kept the picks of a large one")
|
||||
}
|
||||
if keptSeen.Load() == seen {
|
||||
t.Fatal("a small pairing kept the ids seen of a large one")
|
||||
}
|
||||
}
|
||||
|
||||
// A pass's decode buffer is kept for the next across collections, as the
|
||||
// matcher's scratch is (#2278), and goes on ReleaseScratch.
|
||||
func TestRowBufferKeptPastCollections(t *testing.T) {
|
||||
|
||||
@@ -991,13 +991,7 @@ var bigBuildRows = 1 << 17
|
||||
// holdBigBuild holds bigBuilds when the chunks have bigBuildRows rows or
|
||||
// more, and is what lets it go.
|
||||
func holdBigBuild(sets ...[]*rowChunk) (let func()) {
|
||||
n := 0
|
||||
for _, set := range sets {
|
||||
for _, c := range set {
|
||||
n += rowsIn(c)
|
||||
}
|
||||
}
|
||||
if n < bigBuildRows {
|
||||
if rowsOfAll(sets...) < bigBuildRows {
|
||||
return func() {}
|
||||
}
|
||||
bigBuilds.Lock()
|
||||
@@ -1488,10 +1482,12 @@ func visibleLocal(chunks []*rowChunk) refSet {
|
||||
}
|
||||
}
|
||||
seen := keptSeen.Swap(nil)
|
||||
if seen == nil {
|
||||
m := make(map[[2]uint64]struct{}, n)
|
||||
seen = &m
|
||||
// judged by the chunks' rows, an upper bound n leaves the recent out of
|
||||
need := rowsOfAll(chunks)
|
||||
if seen == nil || !scratchFits(seen.made, need) {
|
||||
seen = &seenScratch{ids: make(map[[2]uint64]struct{}, n)}
|
||||
}
|
||||
seen.made = max(seen.made, need)
|
||||
defer keepSeen(seen)
|
||||
rows := lentRows()
|
||||
defer rows.release()
|
||||
@@ -1503,10 +1499,10 @@ func visibleLocal(chunks []*rowChunk) refSet {
|
||||
k = &read
|
||||
}
|
||||
for _, k := range k.keys {
|
||||
if _, ok := (*seen)[k.k]; ok {
|
||||
if _, ok := seen.ids[k.k]; ok {
|
||||
duplicates.add(rowRef{c, int(k.at)})
|
||||
} else {
|
||||
(*seen)[k.k] = struct{}{}
|
||||
seen.ids[k.k] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1517,10 +1513,16 @@ func visibleLocal(chunks []*rowChunk) refSet {
|
||||
// call as matchScratch is: made to size for every pairing it was ~210 MB
|
||||
// of the ~2 GB 90 s of polls allocated (#2278). A cleared map keeps its
|
||||
// buckets.
|
||||
var keptSeen atomic.Pointer[map[[2]uint64]struct{}]
|
||||
var keptSeen atomic.Pointer[seenScratch]
|
||||
|
||||
func keepSeen(seen *map[[2]uint64]struct{}) {
|
||||
clear(*seen)
|
||||
// seenScratch is the set and the ids of the largest call it served.
|
||||
type seenScratch struct {
|
||||
ids map[[2]uint64]struct{}
|
||||
made int
|
||||
}
|
||||
|
||||
func keepSeen(seen *seenScratch) {
|
||||
clear(seen.ids)
|
||||
keptSeen.Store(seen)
|
||||
lingerScratch()
|
||||
}
|
||||
@@ -1714,6 +1716,7 @@ type matchCandidate struct {
|
||||
// kept until ReleaseScratch, which the window calls once its reads rest,
|
||||
// or until no pairing has used it for scratchLinger.
|
||||
type matchScratch struct {
|
||||
made int // the rows of the largest call it served
|
||||
reserved refSet
|
||||
byID map[string]int32
|
||||
calls []idCalls
|
||||
@@ -1733,13 +1736,38 @@ type matchScratch struct {
|
||||
|
||||
var keptMatch atomic.Pointer[matchScratch]
|
||||
|
||||
// matchScratchFor is the kept scratch, or a new one when another call has
|
||||
// it or none is kept
|
||||
func matchScratchFor() *matchScratch {
|
||||
if s := keptMatch.Swap(nil); s != nil {
|
||||
// matchScratchFor is the kept scratch for a call over need rows, or a new
|
||||
// one when another call has it, none is kept or the one kept doesn't fit
|
||||
// (scratchFits)
|
||||
func matchScratchFor(need int) *matchScratch {
|
||||
if s := keptMatch.Swap(nil); s != nil && scratchFits(s.made, need) {
|
||||
s.made = max(s.made, need)
|
||||
return s
|
||||
}
|
||||
return &matchScratch{}
|
||||
return &matchScratch{made: need}
|
||||
}
|
||||
|
||||
// scratchFits says a call over need rows takes a scratch kept from one
|
||||
// over made. A kept scratch stays scratchLinger after the last call put it
|
||||
// back, and the pairings of a poll's last few days kept the one a pairing
|
||||
// of the whole history made: at a cold start ~140 MB of maps and slabs
|
||||
// made for every call stayed while the window polled, the footprint at
|
||||
// 490–530 MB from 44 s to the end of 75 s of polls, of a 548 MB peak
|
||||
// (#2278). A call that much smaller makes its own, and the large one goes.
|
||||
func scratchFits(made, need int) bool { return made <= 4*max(need, scratchFloor) }
|
||||
|
||||
// scratchFloor is the rows below which every call takes what is kept.
|
||||
var scratchFloor = 1 << 12
|
||||
|
||||
// rowsOfAll is the rows of every chunk of sets, packed or not.
|
||||
func rowsOfAll(sets ...[]*rowChunk) int {
|
||||
n := 0
|
||||
for _, set := range sets {
|
||||
for _, c := range set {
|
||||
n += rowsIn(c)
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// mapFor is m, or a new map made for n when there is none yet
|
||||
@@ -1835,7 +1863,7 @@ func matchedLocal(gateway *rowChunk, chunks []*rowChunk, skip refSet, since time
|
||||
}
|
||||
|
||||
func matchedBlocks(gateways []*rowChunk, chunks []*rowChunk, skip refSet, responsePairs map[rowRef]rowRef, since time.Time, newestIDs bool) refSet {
|
||||
scratch := matchScratchFor()
|
||||
scratch := matchScratchFor(rowsOfAll(gateways, chunks))
|
||||
defer scratch.release()
|
||||
scratch.reserved = mapFor(scratch.reserved, 0)
|
||||
reserved := scratch.reserved
|
||||
|
||||
@@ -410,7 +410,7 @@ func TestVisibleLocalSeenKeptEmpty(t *testing.T) {
|
||||
t.Fatalf("left out %v, want b's row 0", got)
|
||||
}
|
||||
kept := keptSeen.Load()
|
||||
if kept == nil || len(*kept) != 0 {
|
||||
if kept == nil || len(kept.ids) != 0 {
|
||||
t.Fatal("the ids seen weren't kept empty")
|
||||
}
|
||||
// b alone repeats nothing; the last call's msg_2 doesn't stand
|
||||
|
||||
Reference in New Issue
Block a user