A cold Requests build (every upgrade) read the call shards eight at a time, but turned every call into a row on the one loop that takes them: logRecord, the account resolver, pricing, interning and packing, ~1 s of serial CPU over a real history, with the readers waiting on the window behind it. prefetchSources becomes prefetchEach: each reader has a make of its own from newMake, and the loop has its own for a source not wanted. Request rows are made there. Each make has its own Pricer and Swapped memo, so neither needs a lock; the session resolver is made once (sync.OnceValue) and only read after. The serial number is still given in the loop, in source order. The window now holds packed chunks, smaller than the calls it held. Measured on a copy of real data (15,656 shards, 3,451 Claude Code subagent meta files), GUI warm-up step 2 (QueryPage(All)), cold (requests-v1.bin written for the other binary), alternating runs: - before (432393217): 3.24 s / 448 MB maxrss, 2.18 s / 433 MB - after: 1.85 s / 417 MB, 1.84 s / 410 MB The readers are now mostly in syscalls (shard and meta.json reads). Run at this head (432393217+ this): - go test -count=1 -timeout 45m ./internal/usage: ok; with -race: ok - go test -count=1 -timeout 45m ./internal/gui: one red, TestRestartHandsTheGatewayOver (18 of 19 requests answered, EOF), the open #3451; alone -count=5 it passes. Load was ~45. - TestPrefetchEachMakeIsAReadersOwn: 9 makes (8 readers, the loop's own) and no race. Broken by hand: one make shared by every reader -> -race reports DATA RACE, red. - gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui ./internal/usage ok.
105 lines
3.0 KiB
Go
105 lines
3.0 KiB
Go
package usage
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
)
|
|
|
|
// The sources a query parses are read several at a time, each handed back
|
|
// as its own, in any order asked; never more than a window of them waits to
|
|
// be taken, and one not wanted is read when it is asked for.
|
|
func TestPrefetchSources(t *testing.T) {
|
|
var sources []sessions.CallSource
|
|
for i := range 200 {
|
|
sources = append(sources, sessions.CallSource{Path: fmt.Sprint(i)})
|
|
}
|
|
var mu sync.Mutex
|
|
reads := map[string]int{}
|
|
var busy, most atomic.Int32
|
|
readSource := func(s sessions.CallSource) []sessions.Call {
|
|
n := busy.Add(1)
|
|
for {
|
|
m := most.Load()
|
|
if n <= m || most.CompareAndSwap(m, n) {
|
|
break
|
|
}
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
busy.Add(-1)
|
|
mu.Lock()
|
|
reads[s.Path]++
|
|
mu.Unlock()
|
|
return []sessions.Call{{Model: s.Path}}
|
|
}
|
|
want := func(s sessions.CallSource) bool { return s.Path != "7" }
|
|
read := prefetchEach(sources, want, func() func(sessions.CallSource) []sessions.Call { return readSource })
|
|
for i := range sources {
|
|
if i == 50 {
|
|
// nothing is taken for a while: the readers stop at the window
|
|
time.Sleep(50 * time.Millisecond)
|
|
mu.Lock()
|
|
ahead := len(reads)
|
|
mu.Unlock()
|
|
if ahead > 50+32+8 {
|
|
t.Fatalf("%d read with 50 taken", ahead)
|
|
}
|
|
}
|
|
cs := read(i)
|
|
if len(cs) != 1 || cs[0].Model != sources[i].Path {
|
|
t.Fatalf("source %d: %+v", i, cs)
|
|
}
|
|
}
|
|
if most.Load() < 2 {
|
|
t.Fatalf("at most %d read at once", most.Load())
|
|
}
|
|
for _, s := range sources {
|
|
if reads[s.Path] != 1 {
|
|
t.Fatalf("%s read %d times", s.Path, reads[s.Path])
|
|
}
|
|
}
|
|
|
|
// one or none wanted: read as asked, nothing ahead
|
|
reads = map[string]int{}
|
|
read = prefetchEach(sources[:3], func(s sessions.CallSource) bool { return s.Path == "1" }, func() func(sessions.CallSource) []sessions.Call { return readSource })
|
|
if len(reads) != 0 {
|
|
t.Fatalf("read ahead: %v", reads)
|
|
}
|
|
if cs := read(2); cs[0].Model != "2" {
|
|
t.Fatalf("%+v", cs)
|
|
}
|
|
}
|
|
|
|
// Each reader makes what it makes of a source with a make of its own, and
|
|
// so does the loop for a source not wanted: what a make keeps between
|
|
// sources (a pricer's memos) is never written from two at once. -race
|
|
// fails when one is shared.
|
|
func TestPrefetchEachMakeIsAReadersOwn(t *testing.T) {
|
|
var sources []sessions.CallSource
|
|
for i := range 100 {
|
|
sources = append(sources, sessions.CallSource{Path: fmt.Sprint(i)})
|
|
}
|
|
var makes atomic.Int32
|
|
read := prefetchEach(sources, func(s sessions.CallSource) bool { return s.Path != "3" }, func() func(sessions.CallSource) string {
|
|
makes.Add(1)
|
|
seen := map[string]bool{} // unlocked, as a reader's memo is
|
|
return func(s sessions.CallSource) string {
|
|
seen[s.Path] = true
|
|
time.Sleep(100 * time.Microsecond)
|
|
return s.Path + fmt.Sprint(len(seen) > 0)
|
|
}
|
|
})
|
|
for i, s := range sources {
|
|
if got := read(i); got != s.Path+"true" {
|
|
t.Fatalf("source %d: %q", i, got)
|
|
}
|
|
}
|
|
if n := makes.Load(); n != 9 { // 8 readers and the loop's own
|
|
t.Fatalf("%d makes, want 9", n)
|
|
}
|
|
}
|