usage: a trim that lets most chunks go gives back the map's table
The usage page's index of session-file chunks is let go when no page has asked for it in requestIdle + requestForget (trim(0)). A Go map never shrinks, so the emptied map kept its table: 853 KB of a real Codex history's index (pprof inuse_space at request_page.go:1461, the map assignment in keep) stayed for the life of the process, 8 minutes after the page was last open. trim now copies what is left into a map of its size when it dropped more than it kept. TestRequestTrimGivesBackTheMap fills an index with 100k chunks, holds the keys, and checks trim(0) gives back the table. Broken by hand (the copy skipped): red, heap 9,054,088 -> 9,051,552. Fixed: 3 of 3 green. Real Codex account sandbox (mote, acctp HOME, page open 30s then 8 min idle, memprofilerate=1): liveHeap 18.00 MB -> 17.07 MB; nothing held under requestIndex.keep (was 853 KB). One earlier run read 23.1 MB with a usage build in flight at the capture (sharedBuild 5.2 MB); the rerun had none. mica: gofmt clean; go vet ./internal/usage for darwin, linux, windows; go test ./internal/usage ./internal/gui ok. Not run: the full suite.
This commit is contained in:
@@ -6,7 +6,9 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -550,3 +552,35 @@ func TestRequestChunksForgottenLongIdle(t *testing.T) {
|
||||
wait("forgotten", func() bool { return chunks() == 0 })
|
||||
ask() // read back from the file of them
|
||||
}
|
||||
|
||||
// A trim that lets most chunks go gives back the map's table too: a map
|
||||
// never shrinks, and the forget's trim(0) of a real history's index kept
|
||||
// an empty 853 KB table for the life of the process.
|
||||
func TestRequestTrimGivesBackTheMap(t *testing.T) {
|
||||
const n = 100000
|
||||
idx := &requestIndex{chunks: make(map[string]*rowChunk)}
|
||||
c := &rowChunk{Bytes: 1, used: new(atomic.Uint64)}
|
||||
keys := make([]string, n) // held, so only the table is measured
|
||||
for i := range n {
|
||||
keys[i] = fmt.Sprint("/s/rollout-", i, ".jsonl")
|
||||
idx.chunks[keys[i]] = c
|
||||
}
|
||||
heap := func() uint64 {
|
||||
runtime.GC()
|
||||
var m runtime.MemStats
|
||||
runtime.ReadMemStats(&m)
|
||||
return m.HeapAlloc
|
||||
}
|
||||
before := heap()
|
||||
idx.trim(0)
|
||||
after := heap()
|
||||
if len(idx.chunks) != 0 {
|
||||
t.Fatalf("%d chunks after trim(0)", len(idx.chunks))
|
||||
}
|
||||
// the table of 100k entries is ~3 MB
|
||||
if after+1<<20 > before {
|
||||
t.Fatalf("heap %d → %d after trim(0): the map's table was kept", before, after)
|
||||
}
|
||||
idx.chunks["/s/kept"] = c // the map is usable after
|
||||
runtime.KeepAlive(keys)
|
||||
}
|
||||
|
||||
@@ -1585,12 +1585,21 @@ func (idx *requestIndex) trim(bound int64) {
|
||||
}
|
||||
return strings.Compare(a.path, b.path)
|
||||
})
|
||||
dropped := 0
|
||||
for _, c := range kept {
|
||||
if bytes <= bound {
|
||||
break
|
||||
}
|
||||
delete(idx.chunks, c.path)
|
||||
bytes -= c.row.Bytes
|
||||
dropped++
|
||||
}
|
||||
// a map never gives back its table: the forget's trim(0) emptied the
|
||||
// map of a real history and kept its 853 KB for the life of the process
|
||||
if dropped > len(idx.chunks) {
|
||||
rest := make(map[string]*rowChunk, len(idx.chunks))
|
||||
maps.Copy(rest, idx.chunks)
|
||||
idx.chunks = rest
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user