From 84a6a2c4202186eb3b78b5730ed6a1cc1c73a13d Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 28 Oct 2026 20:53:36 +0800 Subject: [PATCH] sessions: a range's session list is made to its size (#2278) statsRead grew out.Sessions by append, so 34k sessions' list (Summary is ~480 bytes) held up to a quarter more than its rows, 19.4 MB in the heap after a cold start at #2278's scale, and copied itself through every size on the way. Each range statsFor keeps holds its list. It is now made with room for the range's sessions (shares holds only those with a day in range), and Keys with room for the list. TestStatsHoursAndSessions checks the list and keys hold their rows and no more. Broken by hand (the make removed) it failed with "3 sessions held in 4". Run at this change (mote): go vet -tags nogui of internal/usage and internal/sessions for linux, windows and darwin ok; go test -tags nogui ./internal/usage ./internal/sessions ok. Rebased onto 60561129f and run again at the pushed tree: the same three packages (usage, sessions, codexcat) ok. --- internal/sessions/stats.go | 4 ++++ internal/sessions/stats_test.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/internal/sessions/stats.go b/internal/sessions/stats.go index 96205da2c..8cb9ed5d1 100644 --- a/internal/sessions/stats.go +++ b/internal/sessions/stats.go @@ -382,6 +382,9 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats { groups := byKey(files, func(f *file) bool { return shares[f.key] != nil }) first, _ := time.ParseInLocation(time.DateOnly, out.From, time.Local) + // made to size: grown by append, 34k sessions' list held a quarter + // more than its rows, and every copy it was grown through (#2278) + out.Sessions = make([]Summary, 0, len(shares)) for key, sh := range shares { if len(sh.dates) == 0 { continue @@ -441,6 +444,7 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats { return a.Key < b.Key }) // keys after the sort, so they name the sessions in the order they are in + out.Keys = make([]string, 0, len(out.Sessions)) for _, s := range out.Sessions { out.Keys = append(out.Keys, s.Key) } diff --git a/internal/sessions/stats_test.go b/internal/sessions/stats_test.go index f7ea04149..44b8ade13 100644 --- a/internal/sessions/stats_test.go +++ b/internal/sessions/stats_test.go @@ -237,6 +237,10 @@ func TestStatsHoursAndSessions(t *testing.T) { if !sortedByLast(s.Sessions) || len(s.Sessions) < 3 { t.Fatalf("sessions %+v", s.Sessions) } + // each range keeps its list, so it holds its rows and no more (#2278) + if cap(s.Sessions) != len(s.Sessions) || cap(s.Keys) != len(s.Keys) { + t.Fatalf("%d sessions held in %d, %d keys in %d", len(s.Sessions), cap(s.Sessions), len(s.Keys), cap(s.Keys)) + } // a range counts only its own days day := statsAt(1, time.Date(2026, 9, 21, 12, 0, 0, 0, time.UTC)) if x := find(day); x == nil || x.Tokens != (Tokens{400, 40, 10, 0, 0}) || x.Active != 240 || len(x.Days) != 1 || x.Days[0] != 0 {