sessions: a day keeps only the hours it has time in
A day's hours were a [24]int64 (192 bytes) whether or not it had any. The owner's real cache has 18,331 files and 19,191 days; 13,406 days had hours, but only 19,107 hours across them had any time, so ~2.6 MB of zeros sat in the idle heap for good. hours is now a sorted []uint64 of hour<<58|ms, one entry per hour with time. Its JSON is still the 24-number array, so sessions.json reads and writes as before; an all-zero array read back keeps one zero entry so the day still counts as having hours. Run on mote at this head: - go test ./internal/sessions: ok - GOOS=darwin/linux/windows go vet ./internal/sessions: ok - gofmt -l: clean - Broken by hand: without hours.MarshalJSON, TestHoursKeepTheCacheShape is red (the packed entries went out as raw numbers). - Real-account copy (~/r2278/acct): idle heap at 180s, t1 (v0.1.3398's tree) vs this: inuse 112.94 MB -> 110.65 MB (-2.3 MB; the 2.6 MB estimate less sampling noise).
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package sessions
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"slices"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// hours is a day's Active by the local hour it was put on, kept as only
|
||||
// the hours that have some: of the owner's 19,191 days, 13,406 had hours,
|
||||
// and 19,107 hours had any, so 24 int64s a day (192 bytes) were ~2.6 MB of
|
||||
// zeros held for good. Each entry is hour<<hourShift | ms, in hour order.
|
||||
// In the cache it is the 24 numbers it always was. A day that counted a
|
||||
// gap of no whole millisecond keeps its hour's entry at 0, so it is still
|
||||
// a day with hours, as its 24 zeros were.
|
||||
type hours []uint64
|
||||
|
||||
const hourShift = 58
|
||||
|
||||
func hourOf(e uint64) int { return int(e >> hourShift) }
|
||||
func msOf(e uint64) int64 { return int64(e & (1<<hourShift - 1)) }
|
||||
func hourEntry(h int, ms int64) uint64 { return uint64(h)<<hourShift | uint64(ms) }
|
||||
|
||||
// add puts ms (never below 0) on hour h, 0 to 23.
|
||||
func (hs *hours) add(h int, ms int64) {
|
||||
for i, e := range *hs {
|
||||
switch eh := hourOf(e); {
|
||||
case eh == h:
|
||||
(*hs)[i] = hourEntry(h, msOf(e)+ms)
|
||||
return
|
||||
case eh > h:
|
||||
*hs = slices.Insert(*hs, i, hourEntry(h, ms))
|
||||
return
|
||||
}
|
||||
}
|
||||
*hs = append(*hs, hourEntry(h, ms))
|
||||
}
|
||||
|
||||
func (hs hours) MarshalJSON() ([]byte, error) {
|
||||
var all [24]int64
|
||||
for _, e := range hs {
|
||||
all[hourOf(e)] = msOf(e)
|
||||
}
|
||||
b := make([]byte, 0, 2+24*2)
|
||||
b = append(b, '[')
|
||||
for i, ms := range all {
|
||||
if i > 0 {
|
||||
b = append(b, ',')
|
||||
}
|
||||
b = strconv.AppendInt(b, ms, 10)
|
||||
}
|
||||
return append(b, ']'), nil
|
||||
}
|
||||
|
||||
func (hs *hours) UnmarshalJSON(b []byte) error {
|
||||
var all []int64
|
||||
if err := json.Unmarshal(b, &all); err != nil {
|
||||
return err
|
||||
}
|
||||
if all == nil {
|
||||
*hs = nil
|
||||
return nil
|
||||
}
|
||||
n := 0
|
||||
for _, ms := range all[:min(len(all), 24)] {
|
||||
if ms > 0 {
|
||||
n++
|
||||
}
|
||||
}
|
||||
out := make(hours, 0, max(n, 1))
|
||||
for h, ms := range all[:min(len(all), 24)] {
|
||||
if ms > 0 {
|
||||
out = append(out, hourEntry(h, ms))
|
||||
}
|
||||
}
|
||||
if len(out) == 0 {
|
||||
out = append(out, hourEntry(0, 0))
|
||||
}
|
||||
*hs = out
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package sessions
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestHoursKeepTheCacheShape: a day's hours read from the cache's 24
|
||||
// numbers are written back as the same 24, an hour of 0ms included, and
|
||||
// hold only the hours that have some.
|
||||
func TestHoursKeepTheCacheShape(t *testing.T) {
|
||||
for _, src := range []string{
|
||||
`{"h":[0,0,0,0,0,0,0,0,0,1500,0,0,0,0,0,0,0,0,0,0,0,0,0,42]}`,
|
||||
`{"h":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}`,
|
||||
`{"a":3}`,
|
||||
} {
|
||||
var d day
|
||||
if err := json.Unmarshal([]byte(src), &d); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(d.Hours) > 2 || cap(d.Hours) > 2 {
|
||||
t.Fatalf("%s: %d entries (cap %d)", src, len(d.Hours), cap(d.Hours))
|
||||
}
|
||||
b, _ := json.Marshal(&d)
|
||||
if string(b) != src {
|
||||
t.Fatalf("%s came back as %s", src, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestHoursAddInOrder: a day's hours add up per hour, in hour order,
|
||||
// as the 24 numbers did.
|
||||
func TestHoursAddInOrder(t *testing.T) {
|
||||
var hs hours
|
||||
var want [24]int64
|
||||
for i, h := range []int{9, 3, 23, 9, 0, 3, 12} {
|
||||
ms := int64(i*1000 + 7)
|
||||
hs.add(h, ms)
|
||||
want[h] += ms
|
||||
}
|
||||
hs.add(5, 0)
|
||||
var got [24]int64
|
||||
last := -1
|
||||
for _, e := range hs {
|
||||
if hourOf(e) <= last {
|
||||
t.Fatalf("hours out of order: %v", hs)
|
||||
}
|
||||
last = hourOf(e)
|
||||
got[hourOf(e)] = msOf(e)
|
||||
}
|
||||
if got != want || len(hs) != 6 {
|
||||
t.Fatalf("got %v (%d entries), want %v", got, len(hs), want)
|
||||
}
|
||||
}
|
||||
@@ -363,8 +363,8 @@ type day struct {
|
||||
// it runs while its session's own file goes on.
|
||||
Active int64 `json:"a,omitempty"`
|
||||
// Hours is Active again, by the local hour of the day it was put on
|
||||
// (24 of them, or none)
|
||||
Hours []int64 `json:"h,omitempty"`
|
||||
// (only the hours that have some, or none; 24 numbers in the cache)
|
||||
Hours hours `json:"h,omitempty"`
|
||||
// Prompts are the messages typed in the session's own file, Replies
|
||||
// the agent's messages back (Claude Code's, Codex's, Pi's and omp's)
|
||||
Prompts int `json:"u,omitempty"`
|
||||
@@ -469,10 +469,7 @@ func (s *state) saw(t time.Time, main bool) {
|
||||
if gap := t.Sub(s.Last); main && !s.Last.IsZero() && gap < idleGap {
|
||||
d := s.day(dateOf(t))
|
||||
d.Active += gap.Milliseconds()
|
||||
if d.Hours == nil {
|
||||
d.Hours = make([]int64, 24)
|
||||
}
|
||||
d.Hours[t.In(time.Local).Hour()] += gap.Milliseconds()
|
||||
d.Hours.add(t.In(time.Local).Hour(), gap.Milliseconds())
|
||||
}
|
||||
s.Last = t
|
||||
}
|
||||
@@ -505,9 +502,7 @@ func (s *state) clone() *state {
|
||||
for i, e := range c.Days {
|
||||
v := e.v
|
||||
d := &day{Active: v.Active, Models: slices.Clone(v.Models), Prompts: v.Prompts, Replies: v.Replies}
|
||||
if v.Hours != nil {
|
||||
d.Hours = append([]int64(nil), v.Hours...)
|
||||
}
|
||||
d.Hours = slices.Clone(v.Hours)
|
||||
d.Tools, d.Skills = slices.Clone(v.Tools), slices.Clone(v.Skills)
|
||||
c.Days[i].v = d
|
||||
}
|
||||
|
||||
@@ -422,12 +422,12 @@ func statsOfRange(days int, today time.Time, files []file, copies map[string]str
|
||||
act[k] = a
|
||||
}
|
||||
a.ms += d.Active
|
||||
if len(d.Hours) == 24 {
|
||||
if d.Hours != nil {
|
||||
if a.hours == nil {
|
||||
a.hours = make([]int64, 24)
|
||||
}
|
||||
for h, ms := range d.Hours {
|
||||
a.hours[h] += ms
|
||||
for _, e := range d.Hours {
|
||||
a.hours[hourOf(e)] += msOf(e)
|
||||
}
|
||||
}
|
||||
dates[date] = true
|
||||
|
||||
Reference in New Issue
Block a user