usage: a re-read source asks Swapped once per model pair (#3493)

Swapped's regexps ran for every call of every file read again: a few
hundred sent/served pairs over ~289k calls on the owner's real history,
about a third of the re-read's pricing cost in a CPU profile. queryPage
now keeps each pair's answer for the read.

Measured at internal/usage with the owner's real ~/.codex and ~/.claude
in a sandbox HOME: a heatmap read whose key changed, 3.0s -> 1.4s
together with the heatmap's own page.

Run at this tree: go test -count=1 -timeout 45m ./internal/usage
./internal/usageapi ok; GOOS=linux and windows go vet -tags nogui
./internal/usage ok.
This commit is contained in:
yetone
2026-10-24 22:58:26 +08:00
parent cf8b1e9338
commit 3be1f14ebb
+13 -1
View File
@@ -570,12 +570,24 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.C
chunks: maps.Clone(idx.chunks)}
shared.Unlock()
price := Pricer()
// Swapped's names are a few hundred pairs over thousands of files' calls,
// and its regexps were a third of reading them again (#3493)
swapped := map[[2]string]bool{}
priceRow := func(r Record, source string) Row {
sent := r.Model
if source == "" {
sent = provider.SentNameIn(cfg.ModelWires, r.Provider, r.Model, r.Effort)
}
row := Row{Record: r, Source: source, Swapped: r.Served != "" && Swapped(sent, r.Served), Routed: GroupRouted(sent, r.Served)}
row := Row{Record: r, Source: source, Routed: GroupRouted(sent, r.Served)}
if r.Served != "" {
k := [2]string{sent, r.Served}
s, ok := swapped[k]
if !ok {
s = Swapped(sent, r.Served)
swapped[k] = s
}
row.Swapped = s
}
if pr := price(r); pr != nil && r.Input+r.Output > 0 {
row.Priced = true
row.Cost = r.CostAt(*pr)