From 2f55483718890d049078f78470413915f65df511 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 29 Oct 2026 07:25:58 +0800 Subject: [PATCH] sessions: a stats share makes its tools and skills maps only when a day has some (#2278) statsOfRange made four maps (tools, skills, skillLast and a day's tools) for every session of every range on each poll, and most sessions' days use no tools or skills. They are made now on the first day that has one. Measured on mote (Usage stats polled every 5s with gateway feed, heap diff 120s to 200s, A/B against 905166d78): statsOfRange allocated 894MB against 938MB per 80s. Peak RSS and CPU in that A/B are not comparable (the main leg ran under load 66.8% CPU). Run at this head (905166d78 + this change), on mote: GOOS=linux/windows/darwin go vet -tags nogui ./internal/sessions/ ok; go test -count=1 ./internal/sessions/ ok. On magpie-desktop (wsl-run): go test ./internal/sessions/ ok. Not run: the full suite. --- internal/sessions/stats.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/internal/sessions/stats.go b/internal/sessions/stats.go index 9f9489a30..a7a6f8936 100644 --- a/internal/sessions/stats.go +++ b/internal/sessions/stats.go @@ -329,13 +329,15 @@ func statsOfRange(days int, today time.Time, files []file, copies map[string]str } sh := shares[f.key] if sh == nil { - sh = &share{models: map[string]Tokens{}, dates: map[string]bool{}, tools: map[string]int{}, - skills: map[string]int{}, skillLast: map[string]string{}, perDate: map[string]*dayShare{}} + // tools and skills are made once a day has some: most + // sessions' days have none, and each poll made them for + // every session of every range (#2278) + sh = &share{models: map[string]Tokens{}, dates: map[string]bool{}, perDate: map[string]*dayShare{}} shares[f.key] = sh } pd := sh.perDate[date] if pd == nil { - pd = &dayShare{tools: map[string]int{}} + pd = &dayShare{} sh.perDate[date] = pd } for model, t := range d.Models.all() { @@ -355,10 +357,19 @@ func statsOfRange(days int, today time.Time, files []file, copies map[string]str } pd.messages += d.Prompts + d.Replies for name, n := range d.Tools.all() { + if sh.tools == nil { + sh.tools = map[string]int{} + } + if pd.tools == nil { + pd.tools = map[string]int{} + } sh.tools[name] += n pd.tools[ToolCategory(name)] += n } for name, n := range d.Skills.all() { + if sh.skills == nil { + sh.skills, sh.skillLast = map[string]int{}, map[string]string{} + } sh.skills[name] += n if date > sh.skillLast[name] { sh.skillLast[name] = date