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.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user