usage: the matcher's scratch goes 30s after the last pairing used it (#2278)
With nothing new to pair, every poll of the window is answered from the pairings kept (pairMemo) and pairs nothing, so the scratch of one pairing of the whole history (keptMatch's groupOf/calls/ends/byID maps, keptPicks' picks and slab) sat unused while the window polled, until pageRest's ReleaseScratch 30s after polling stopped. On the real 65 GB history (52 GB Codex, 13 GB Claude) that was ~120 MB of the ~270 MB live heap over 60s of polls. Now each release of either scratch (re)arms one timer, and scratchLinger (30s) after the last pairing put it back both are let go. While an agent works, every poll pairs again inside 30s and finds it kept, as before. Run at this head (origin/main430005726+ this commit), on mote: - go test -count=1 -timeout 45m ./internal/usage/: ok 19.6s. - TestPairScratchGoesWhenUnused broken by hand (both lingerScratch calls removed): FAIL "the scratch was kept with no pairing using it" at 30.55s; green with them. - go test -count=1 -timeout 45m ./internal/gui/: one red, TestMainWindowCorners/macOS_26_SDK, which is #3949 (macOS 27, open, untouched by this change); everything else ok. - GOOS=linux/windows/darwin go vet -tags nogui ./internal/usage/ ./internal/gui/: clean. gofmt -l: clean. - A/B on the real full history, KEEP=1 warm start, 10 endpoints polled every 5s for 120s (fullwarmh.sh), baseline430005726vs this commit, back to back on mote at load ~45: footprint 60s/100s/120s: 507/506/500 MB -> 384/389/382 MB footprint peak: 602 MB -> 567 MB CPU: 29.9s -> 30.6s (same within load noise); poll avg 0.172s -> 0.159s A first round at load ~80 gave 442/437/434 -> 347/342/355 MB. Rebased ontoeed140c4d(no internal/usage changes since430005726); at the rebased head go test ./internal/usage/ ok 22.5s, linux vet clean. Not tried: the startup peak (~4s, first poll burst during warm-up) is the pricedRows pool and the chunks of a whole-history build; this commit doesn't change it.
This commit is contained in:
@@ -210,6 +210,7 @@ func (s *pickScratch) release() {
|
||||
clear(s.slab)
|
||||
s.slab = s.slab[:0]
|
||||
keptPicks.Store(s)
|
||||
lingerScratch()
|
||||
}
|
||||
|
||||
// codexChunkResponsePairs are the Codex calls the gateway and a session file
|
||||
|
||||
@@ -91,3 +91,31 @@ func TestMatchScratchForgetsTheLastCall(t *testing.T) {
|
||||
t.Fatal("the last call's ids were looked up")
|
||||
}
|
||||
}
|
||||
|
||||
// The scratch a pairing kept goes once no pairing has used it for
|
||||
// scratchLinger (#2278), and a pairing before then finds it kept.
|
||||
func TestPairScratchGoesWhenUnused(t *testing.T) {
|
||||
was := scratchLinger
|
||||
scratchLinger = 2 * time.Second
|
||||
t.Cleanup(func() {
|
||||
scratchLinger = was
|
||||
ReleaseScratch()
|
||||
})
|
||||
now := time.Date(2026, 10, 1, 13, 0, 0, 0, time.UTC)
|
||||
g, l := pairFixture(12, 9)
|
||||
ReleaseScratch()
|
||||
pairRows(All, now, g, l, nil, nil)
|
||||
kept := keptMatch.Load()
|
||||
if kept == nil || keptPicks.Load() == nil {
|
||||
t.Fatal("nothing kept for the next call")
|
||||
}
|
||||
pairRows(All, now, g, l, nil, nil)
|
||||
if keptMatch.Load() != kept {
|
||||
t.Fatal("a pairing right after made scratch of its own")
|
||||
}
|
||||
for deadline := time.Now().Add(30 * time.Second); keptMatch.Load() != nil || keptPicks.Load() != nil; time.Sleep(10 * time.Millisecond) {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("the scratch was kept with no pairing using it")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1423,7 +1423,8 @@ type matchCandidate struct {
|
||||
// and growing these afresh each time was ~1.9 of the ~2.2 GB the matcher
|
||||
// allocated in 80 s of polls (#2278). A sync.Pool kept nothing: a poll
|
||||
// collects several times a second, and each collection empties it. One is
|
||||
// kept until ReleaseScratch, which the window calls once its reads rest.
|
||||
// kept until ReleaseScratch, which the window calls once its reads rest,
|
||||
// or until no pairing has used it for scratchLinger.
|
||||
type matchScratch struct {
|
||||
reserved map[rowRef]bool
|
||||
byID map[string]int32
|
||||
@@ -1469,6 +1470,34 @@ func ReleaseScratch() {
|
||||
releasePriced()
|
||||
}
|
||||
|
||||
// scratchLinger is how long the matcher's scratch is kept once the last
|
||||
// pairing put it back. While an agent works every poll pairs again and
|
||||
// finds it kept; with nothing new to pair, a poll is answered from the
|
||||
// pairings kept (pairMemo) and pairs nothing, and the scratch of one pairing
|
||||
// of the whole history sat unused while the window polled: ~120 MB of the
|
||||
// ~270 MB held over 60 s of polls on a 65 GB history (#2278).
|
||||
var scratchLinger = 30 * time.Second
|
||||
|
||||
var scratchSweep struct {
|
||||
sync.Mutex
|
||||
t *time.Timer
|
||||
}
|
||||
|
||||
// lingerScratch has the kept scratch let go scratchLinger after the last
|
||||
// pairing put it back.
|
||||
func lingerScratch() {
|
||||
scratchSweep.Lock()
|
||||
defer scratchSweep.Unlock()
|
||||
if scratchSweep.t == nil {
|
||||
scratchSweep.t = time.AfterFunc(scratchLinger, func() {
|
||||
keptMatch.Store(nil)
|
||||
keptPicks.Store(nil)
|
||||
})
|
||||
return
|
||||
}
|
||||
scratchSweep.t.Reset(scratchLinger)
|
||||
}
|
||||
|
||||
// sized is s[:n], zeroed, in s's array when it holds n
|
||||
func sized[T any](s []T, n int) []T {
|
||||
if cap(s) < n {
|
||||
@@ -1495,6 +1524,7 @@ func (s *matchScratch) release() {
|
||||
s.calls, s.found, s.refs, s.groups, s.ks = s.calls[:0], s.found[:0], s.refs[:0], s.groups[:0], s.ks[:0]
|
||||
s.ends, s.counts, s.unique, s.candidates = s.ends[:0], s.counts[:0], s.unique[:0], s.candidates[:0]
|
||||
keptMatch.Store(s)
|
||||
lingerScratch()
|
||||
}
|
||||
|
||||
// agyMatchSession stands for the session of an Antigravity CLI (agy) call
|
||||
|
||||
Reference in New Issue
Block a user