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.
This commit is contained in:
yetone
2026-10-12 23:33:56 +08:00
parent d85ef1908a
commit 9dd5917a41
2 changed files with 32 additions and 6 deletions
+10 -6
View File
@@ -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
+22
View File
@@ -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")
}
}