diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index bc8af2fa8..0acf7f26f 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -1432,19 +1432,36 @@ func visibleLocal(chunks []*rowChunk) refSet { for _, k := range keys { n += len(k.keys) } - seen := make(map[[2]uint64]struct{}, n) + seen := keptSeen.Swap(nil) + if seen == nil { + m := make(map[[2]uint64]struct{}, n) + seen = &m + } + defer keepSeen(seen) for i, c := range chunks { for _, k := range keys[i].keys { - if _, ok := seen[k.k]; ok { + if _, ok := (*seen)[k.k]; ok { duplicates.add(rowRef{c, int(k.at)}) } else { - seen[k.k] = struct{}{} + (*seen)[k.k] = struct{}{} } } } return duplicates } +// keptSeen is visibleLocal's set of ids seen, kept empty for the next +// 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{}] + +func keepSeen(seen *map[[2]uint64]struct{}) { + clear(*seen) + keptSeen.Store(seen) + lingerScratch() +} + // msgKeys are a chunk's message ids as visibleLocal hashes them, each // with its row, kept by the chunk's serial as matchHints are: a pairing // decoded every session file of the period to read them, ~840 MB of the @@ -1667,6 +1684,7 @@ func mapFor[K comparable, V any](m map[K]V, n int) map[K]V { func ReleaseScratch() { keptMatch.Store(nil) keptPicks.Store(nil) + keptSeen.Store(nil) releaseRows() releasePriced() } @@ -1693,6 +1711,7 @@ func lingerScratch() { scratchSweep.t = time.AfterFunc(scratchLinger, func() { keptMatch.Store(nil) keptPicks.Store(nil) + keptSeen.Store(nil) releaseRows() }) return diff --git a/internal/usage/request_page_test.go b/internal/usage/request_page_test.go index b71ab1e43..a160cb4d4 100644 --- a/internal/usage/request_page_test.go +++ b/internal/usage/request_page_test.go @@ -366,6 +366,38 @@ func TestVisibleLocalKeptBySerial(t *testing.T) { } } +// The ids one call saw are kept as an empty set for the next (#2278): a +// call after another, on other chunks, leaves out what a first call does, +// and ReleaseScratch lets the set go. +func TestVisibleLocalSeenKeptEmpty(t *testing.T) { + a, b := &rowChunk{}, &rowChunk{} + a.add(Row{}, "msg_1", 0, false) + a.add(Row{}, "msg_2", 1, false) + b.add(Row{}, "msg_2", 0, false) + b.add(Row{}, "msg_3", 1, false) + a, b = a.pack(), b.pack() + t.Cleanup(ReleaseScratch) + ReleaseScratch() + if got := refMap(visibleLocal([]*rowChunk{a, b})); len(got) != 1 || !got[rowRef{b, 0}] { + t.Fatalf("left out %v, want b's row 0", got) + } + kept := keptSeen.Load() + if kept == nil || len(*kept) != 0 { + t.Fatal("the ids seen weren't kept empty") + } + // b alone repeats nothing; the last call's msg_2 doesn't stand + if got := refMap(visibleLocal([]*rowChunk{b})); len(got) != 0 { + t.Fatalf("after another call left out %v", got) + } + if keptSeen.Load() != kept { + t.Fatal("a call right after made a set of its own") + } + ReleaseScratch() + if keptSeen.Load() != nil { + t.Fatal("ReleaseScratch kept the ids seen") + } +} + // A page read again after a call takes each chunk's newest rows from its // part, refs and rows: decoding the newest chunks for every page was // ~390 MB of 90s while an agent worked, a long session's file among them,