Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

usage: the request file's first write makes each entry on the last one's bytes (#2278)

yetone8e1b1b26d0b6
diff --git a/internal/usage/request_disk.go b/internal/usage/request_disk.go
index f99c99132..b6dc2e596 100644
--- a/internal/usage/request_disk.go
+++ b/internal/usage/request_disk.go
@@ -99,10 +99,14 @@ func appendDiskString(b []byte, s string) []byte {
return append(b, s...)
}
-// diskEntry is c as an entry, or nil when it can't be kept: a chunk of
-// another computer's day, or one whose times are in a zone that can't be
-// named again.
-func diskEntry(c *rowChunk) []byte {
+// diskEntry is c as an entry, appended to b[:0], or nil when it can't be
+// kept: a chunk of another computer's day, or one whose times are in a
+// zone that can't be named again. A write hands each entry the last one's
+// b: grown from nil an entry at a time, a first write of 15.7k sessions
+// allocated ~300 MB in the seconds after a cold start's first page, and
+// the heap grew to 764 MB before a collection, the footprint's peak
+// (#2278).
+func diskEntry(b []byte, c *rowChunk) []byte {
if c.Computer != "" {
return nil
}
@@ -110,7 +114,7 @@ func diskEntry(c *rowChunk) []byte {
if len(archive) == 0 {
archive, locations = c.encode(func([]byte) bool { return true })
}
- var b []byte
+ b = b[:0]
put := func(v uint64) { b = binary.AppendUvarint(b, v) }
s := c.Source
for _, t := range []string{s.Path, s.Key, s.Agent} {
@@ -377,12 +381,14 @@ func toDisk(root, meta string, chunks map[string]*rowChunk, on map[string]bool)
defer os.Remove(out.Name())
w := bufio.NewWriterSize(out, 1<<20)
err = writeDiskEntry(w, diskHeader(binary, root, meta))
+ var buf []byte
for _, c := range chunks {
if err != nil {
break
}
- if b := diskEntry(c); b != nil {
+ if b := diskEntry(buf, c); b != nil {
err = writeDiskEntry(w, b)
+ buf = b
}
}
if err == nil {
@@ -391,7 +397,7 @@ func toDisk(root, meta string, chunks map[string]*rowChunk, on map[string]bool)
if !ok {
return false
}
- if on[p] && chunks[p] == nil {
+ if on[string(p)] && chunks[string(p)] == nil {
err = writeDiskEntry(w, raw)
}
return err == nil
@@ -425,27 +431,85 @@ func onDisk(root, meta string, chunks map[string]*rowChunk) map[string]*rowChunk
if !ok {
return false
}
- c := chunks[path]
+ c := chunks[string(path)]
if c == nil || c.serial == 0 || c.pagesRank() == 2 || len(c.Archive) == 0 {
return true
}
- e, err := readDiskEntry(raw)
- if err != nil || e.Source.Size != c.Source.Size || !e.Source.Modified.Equal(c.Source.Modified) || !bytes.Equal(e.Archive, c.Archive) {
+ size, modified, archive, ok := diskEntryArchive(raw)
+ if !ok || size != c.Source.Size || modified != c.Source.Modified.UnixNano() || !bytes.Equal(archive, c.Archive) {
return true
}
next := *c
- next.Archive, next.mapped = e.Archive, m
- kept[path] = &next
+ next.Archive, next.mapped = archive, m
+ kept[string(path)] = &next
return true
})
return kept
}
-// diskEntryPath is the path of the source an entry is of.
-func diskEntryPath(b []byte) (string, bool) {
+// diskEntryPath is the path of the source an entry is of, of b's bytes.
+func diskEntryPath(b []byte) ([]byte, bool) {
n, k := binary.Uvarint(b)
if k <= 0 || n > uint64(len(b)-k) {
- return "", false
+ return nil, false
+ }
+ return b[k : k+int(n)], true
+}
+
+// diskEntryArchive is the size and mtime of the source an entry is of, and
+// its archive, of b's bytes: what onDisk compares, with nothing else of
+// the entry copied out (readDiskEntry's strings were ~95 MB of a first
+// write's ~440 MB, #2278).
+func diskEntryArchive(b []byte) (size, modified int64, archive []byte, ok bool) {
+ take := func() uint64 {
+ n, k := binary.Uvarint(b)
+ if k <= 0 {
+ b = nil
+ return 0
+ }
+ b = b[k:]
+ return n
+ }
+ skip := func() bool {
+ n := take()
+ if b == nil || n > uint64(len(b)) {
+ return false
+ }
+ b = b[n:]
+ return true
+ }
+ for range 3 { // path, key, agent
+ if !skip() {
+ return 0, 0, nil, false
+ }
+ }
+ size, modified = int64(take()), int64(take())
+ take() // count
+ take() // latest
+ n := take()
+ if b == nil || n > uint64(len(b)) {
+ return 0, 0, nil, false
+ }
+ for range n { // a zone's name and offset
+ if !skip() {
+ return 0, 0, nil, false
+ }
+ if take(); b == nil {
+ return 0, 0, nil, false
+ }
+ }
+ n = take()
+ if b == nil || n > uint64(len(b)) {
+ return 0, 0, nil, false
+ }
+ for range n { // actors
+ if !skip() {
+ return 0, 0, nil, false
+ }
+ }
+ n = take()
+ if b == nil || n != uint64(len(b)) || n == 0 {
+ return 0, 0, nil, false
}
- return string(b[k : k+int(n)]), true
+ return size, modified, b, true
}
diff --git a/internal/usage/request_disk_entry_test.go b/internal/usage/request_disk_entry_test.go
new file mode 100644
index 000000000..fc6d42bc1
--- /dev/null
+++ b/internal/usage/request_disk_entry_test.go
@@ -0,0 +1,79 @@
+package usage
+
+import (
+ "bytes"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/sessions"
+)
+
+// What a write compares of an entry it wrote is the size, mtime and
+// archive readDiskEntry reads of it, past any zones and actors, and an
+// entry cut short anywhere is none. Each entry is made on the last one's
+// bytes, as a write makes them.
+func TestDiskEntryArchive(t *testing.T) {
+ shanghai, err := time.LoadLocation("Asia/Shanghai")
+ if err != nil {
+ t.Skip(err)
+ }
+ var buf []byte
+ for i, c := range []struct {
+ zones []*time.Location
+ actors []string
+ }{
+ {nil, nil},
+ {[]*time.Location{time.UTC, shanghai, time.FixedZone("X", 3600)}, []string{"cli", "exec", "a long actor name"}},
+ {[]*time.Location{time.Local}, nil},
+ } {
+ chunk := &rowChunk{
+ Source: sessions.CallSource{Path: "/s/rollout-" + string(rune('a'+i)) + ".jsonl", Key: "k", Agent: "codex",
+ Size: int64(1000 + i), Modified: time.Unix(1790000000+int64(i), 123456789)},
+ Count: 3, Latest: time.Unix(1790000100, 0), Locations: c.zones,
+ Archive: bytes.Repeat([]byte{byte(i + 1)}, 100+i),
+ }
+ for _, a := range c.actors {
+ chunk.codexActors.add(a)
+ }
+ b := diskEntry(buf, chunk)
+ if b == nil {
+ t.Fatalf("%d: no entry", i)
+ }
+ want, err := readDiskEntry(bytes.Clone(b))
+ if err != nil {
+ t.Fatalf("%d: %v", i, err)
+ }
+ size, modified, archive, ok := diskEntryArchive(b)
+ if !ok || size != want.Source.Size || modified != want.Source.Modified.UnixNano() || !bytes.Equal(archive, want.Archive) {
+ t.Fatalf("%d: %d %d %d bytes %v, want %d %d %d bytes", i, size, modified, len(archive), ok, want.Source.Size, want.Source.Modified.UnixNano(), len(want.Archive))
+ }
+ if !bytes.Equal(archive, chunk.Archive) {
+ t.Fatalf("%d: archive is not the chunk's", i)
+ }
+ for n := range len(b) {
+ if _, _, _, ok := diskEntryArchive(b[:n]); ok {
+ t.Fatalf("%d: cut to %d of %d bytes still read", i, n, len(b))
+ }
+ }
+ buf = b
+ }
+}
+
+// An entry made on bytes big enough allocates nothing, nor does reading
+// back what a write compares: each grown from nil was ~300 MB of a cold
+// start's first write (#2278).
+func TestDiskEntryAllocatesOnce(t *testing.T) {
+ chunk := &rowChunk{
+ Source: sessions.CallSource{Path: "/s/rollout.jsonl", Key: "k", Agent: "codex", Size: 1, Modified: time.Unix(1790000000, 0)},
+ Count: 1, Latest: time.Unix(1790000000, 0), Locations: []*time.Location{time.UTC},
+ Archive: bytes.Repeat([]byte{7}, 64<<10),
+ }
+ chunk.codexActors.add("cli")
+ buf := diskEntry(nil, chunk)
+ if n := testing.AllocsPerRun(20, func() { buf = diskEntry(buf, chunk) }); n != 0 {
+ t.Errorf("an entry on the last one's bytes allocated %v times", n)
+ }
+ if n := testing.AllocsPerRun(20, func() { diskEntryArchive(buf) }); n != 0 {
+ t.Errorf("reading an entry's archive allocated %v times", n)
+ }
+}