From 3be1f14ebbb97a593b7e3f41d3574b0ac25575e3 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 24 Oct 2026 22:58:24 +0800 Subject: [PATCH] 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. --- internal/usage/request_page.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index ecc335db9..fae008806 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -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)