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.
This commit is contained in:
yetone
2026-10-29 07:25:58 +08:00
parent 905166d786
commit 2f55483718
+14 -3
View File
@@ -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