From 9dd5917a413fbb2156a34b55d8cb041562139749 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 12 Oct 2026 23:26:59 +0800 Subject: [PATCH] gui: the session-stats memo lets go of the range asked least recently, counted by asks rather than the clock (review of CaramelizedCUDA's cap, #1827) 9f0c9593 caps the memo at 8 ranges and stamps each with time.Now() when asked. Its test only checked the count and that the newest range stays, which passes with the eviction reversed (k.used.After) or with a hit that doesn't restamp. TestStatsMemoKeepsTheRangeAskedAgain fills the memo, asks range 1 again, adds one more and checks 1 is kept and 2 let go. The stamp is now the memo's ask count: Windows' clock can read the same for two asks in a row, so with time stamps the range asked again could tie with one asked once and either could go. Run (macOS, go1.26.8, sandbox HOME, private caches): - reversed comparison (> for <): the new test fails, "the range asked again was let go before one asked once"; a hit that doesn't restamp: the same failure. Both pass with the change. - go test -tags nogui -count=1 ./internal/gui ./internal/plugin: ok. - gofmt -l clean; go vet on both packages for darwin, linux (nogui) and windows/amd64; go build for darwin, linux nogui and windows. Not run: the whole suite (machine loaded), the new test on Windows. --- internal/gui/sessions.go | 16 ++++++++++------ internal/gui/sessions_stats_cap_test.go | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index 910238a23..d801adc83 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -345,14 +345,17 @@ func wslName(name string, s sessions.Session) string { // than a few seconds old it is read again behind, for the next ask. var statsMemo struct { sync.Mutex - m map[int]*statsKept + m map[int]*statsKept + asks uint64 } type statsKept struct { at time.Time st sessions.Stats busy bool - used time.Time + // used is the memo's ask count when this range was last asked: a count, + // not a clock, since Windows' clock can give two asks the same time. + used uint64 } // statsMemoMost is how many distinct ?days values the memo keeps: each one is @@ -364,7 +367,8 @@ func statsFor(days int) sessions.Stats { days = max(0, days) statsMemo.Lock() if k := statsMemo.m[days]; k != nil { - k.used = time.Now() + statsMemo.asks++ + k.used = statsMemo.asks if !k.busy && time.Since(k.at) > 10*time.Second { k.busy = true go func() { @@ -388,14 +392,14 @@ func statsFor(days int) sessions.Stats { if len(statsMemo.m) >= statsMemoMost { oldest := -1 for d, k := range statsMemo.m { - if oldest == -1 || k.used.Before(statsMemo.m[oldest].used) { + if oldest == -1 || k.used < statsMemo.m[oldest].used { oldest = d } } delete(statsMemo.m, oldest) } - now := time.Now() - statsMemo.m[days] = &statsKept{at: now, st: st, used: now} + statsMemo.asks++ + statsMemo.m[days] = &statsKept{at: time.Now(), st: st, used: statsMemo.asks} } statsMemo.Unlock() return st diff --git a/internal/gui/sessions_stats_cap_test.go b/internal/gui/sessions_stats_cap_test.go index d240f41d3..b678cbe5a 100644 --- a/internal/gui/sessions_stats_cap_test.go +++ b/internal/gui/sessions_stats_cap_test.go @@ -21,3 +21,25 @@ func TestStatsMemoKeepsAFewDays(t *testing.T) { t.Fatal("the value asked last was let go") } } + +// The range the page keeps asking for (the one it shows) is the one kept: a +// range asked again is newer than one asked once before it, so a run of new +// numbers lets go of the others first. +func TestStatsMemoKeepsTheRangeAskedAgain(t *testing.T) { + forgetStats() + t.Cleanup(forgetStats) + for d := 1; d <= statsMemoMost; d++ { + statsFor(d) + } + statsFor(1) + statsFor(100) + statsMemo.Lock() + kept1, kept2 := statsMemo.m[1] != nil, statsMemo.m[2] != nil + statsMemo.Unlock() + if !kept1 { + t.Fatal("the range asked again was let go before one asked once") + } + if kept2 { + t.Fatal("the least recently asked range is still kept past the cap") + } +}