The Sessions and Usage index (sessions.json, ~18k files on a long
history) was JSON. Every start decoded all of it through reflection,
and every save, which happens at most once a minute while a session is
live, marshalled it whole again. It is now a compact binary form
(cache_binary.go):
- A magic and a version header.
- Then length-prefixed records, varints for the numbers.
- Interned names, as the JSON path interned them.
- The rare nested parts (Claude Code's window, a Codex identity,
StateMore) as JSON blobs.
Records are decoded in parallel.
A JSON index from an older magpie is still read. loadCache saves it
again at once, so it turns binary on the first start even when no
session changed; before, a save came only after a file changed.
A binary file is refused whole, never half read: when it is cut
anywhere, has a byte added, or is of another zone or version. That
means one full read again, as a refused JSON file meant before.
TestCacheBinaryKnowsEveryField fails when state, day or Tokens gains a
field that the binary form doesn't carry.
On the real acctp history, on mote (18,346 files):
- The file is 14.8 MB, down from 23.3 MB.
- Decoding takes 19 ms, down from 69 ms, and allocates 28 MB, down
from 58 MB.
- Encoding takes 10 ms, down from 98 ms, and allocates 3.1 MB, down
from 32.5 MB.
- The heap kept after decoding is the same: 24 MB.
- /api/sessions from a w94 build on the binary index and from a w92
build (before) on the JSON index answer the same bytes (5000
sessions). Neither rewrote the index.
- The page-open footprint peak was 152 to 214 MB on both builds at
load 60 to 80. It is too noisy to claim a change.
Run:
- mica-run go test -count=1 -timeout 45m ./internal/sessions
./internal/usage ./internal/gui: all ok.
- GOOS=linux and GOOS=windows go vet ./internal/sessions
./internal/usage: ok. go vet ./internal/sessions: ok.
- gofmt -l: clean.
- TestCacheBinaryRefusesBadFiles cuts the file at every length, adds a
byte, changes the zone and the version, and flips every byte; it
checks that a bad file is never accepted and never panics.
Broken by hand:
- The decoder skipped Provider: TestCacheBinaryIsJSON went red.
- loadCache stopped re-saving a JSON index:
TestCacheJSONRewrittenOnStart went red, the file still JSON.
At the pushed head (rebased on 500f39886): go test ./internal/sessions
./internal/testenv ok.
Not tried: Windows and Linux runs of the package; the mapping read is
shared code and unchanged there.
48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package sessions
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Writing the whole index to disk takes a second or more for a long
|
|
// history; a read of a few changed files neither waits on it nor counts it
|
|
// as indexing, or the page shows its indexing show for every catch-up read
|
|
// (Image #15: the show came back again and again at 3 of 3 files read).
|
|
func TestSaveBehindTheRead(t *testing.T) {
|
|
setup(t)
|
|
release := make(chan struct{})
|
|
writing := make(chan struct{}, 8)
|
|
var once sync.Once
|
|
let := func() { once.Do(func() { close(release) }) }
|
|
saveHook = func() { writing <- struct{}{}; <-release }
|
|
t.Cleanup(func() { let(); saveHook = nil })
|
|
|
|
done := make(chan []Session)
|
|
go func() { done <- List(0) }()
|
|
select {
|
|
case <-writing:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("no save started")
|
|
}
|
|
var list []Session
|
|
select {
|
|
case list = <-done:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("the read waited on the save")
|
|
}
|
|
if len(list) == 0 {
|
|
t.Fatal("no sessions read")
|
|
}
|
|
if Indexing().Indexing {
|
|
t.Fatal("still indexing while the index is saved")
|
|
}
|
|
// the save lands once let go, and a new load waits for it
|
|
let()
|
|
Saved()
|
|
if kept := savedIndex(t); len(kept.Files) == 0 {
|
|
t.Fatal("no files kept")
|
|
}
|
|
}
|