Files
magpie/internal/provider/hold.go
T
yetone 2a4042a94d perf: magpie opens and shows the Agents page in a fraction of the time it took (Junjie Zhou on Discord: 8 个 agent 30 个 provider … 每次我打开的时候都会很慢,要好几秒。setting 页面秒进后 我切换到 agent 就有问题) — at start, writing the agents' model lists (agent.SyncCatalog) built every provider's catalog thousands of times with nothing held, each build reading migrations.json, settings and groups again: 6000+ file reads and ~8.5s of CPU in the first 8s with the owner's 30-odd providers, all competing with the first /api/state and the Agents page. Now SyncCatalog, magpieModels and main's start-up moves run under provider.Hold, migrations, groups, settings and a provider's fetched models are held for a second like the rest, and filememo.Hold lets a GET /api/* not stat a file it confirmed unchanged within the last second (any magpie write, Changed() or a recent mtime still reads it again). A big ~/.codex/config.toml parsed in quadratic time (the TOML parser's Shape counts newlines from the start for every key): a 3 MB one took 5.4s, now 0.04s (tomlShaper). theInstalled reads through edit.Read's memo. Measured with a copy of the owner's magpie config and agent configs under a sandbox HOME, the real server in WebKit: CPU in the first 8s 8.59s → 0.39s, page up 475 → 75ms, Agents rows drawn 775 → 325ms after start; the agents' written model files are unchanged (pi byte for byte, opencode the same models). TestTOMLShaperMatchesParser, TestTOMLBigFileParsesInLinearTime (fails on the old code), TestHold (filememo), TestHoldReadsMigrationsOnce. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 03:02:40 +08:00

111 lines
2.9 KiB
Go

package provider
import (
"slices"
"sync"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/filememo"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/settings"
)
// The catalog is rebuilt from every provider's files on each Resolve (tens
// of milliseconds with a few hundred models), and a page of the GUI
// resolves a model for each row: /api/state and /api/groups took 4s and
// 7s with 24 providers. A request that only reads holds the catalog it
// built first for all its look-ups.
var held struct {
sync.Mutex
holds int
gen uint64
// what was built, and when, by what it is
built map[string]heldValue
}
type heldValue struct {
v any
at time.Time
}
func init() {
catalog.Forget = Changed
// a plugin's sign-in saved or renewed, a plugin added
plugin.OnChange(Changed)
}
// heldFor is how long a held catalog is used before it is built again,
// while one request after another keeps it held.
const heldFor = time.Second
// Hold keeps the catalog built once until release is called, for a
// request that only reads; a write meanwhile (Changed) drops it.
func Hold() (release func()) {
held.Lock()
held.holds++
held.Unlock()
return sync.OnceFunc(func() {
held.Lock()
if held.holds--; held.holds == 0 {
held.built = nil
}
held.Unlock()
})
}
// Changed drops what is held: a provider, an account, a plugin or a
// setting was written.
func Changed() {
filememo.Forget()
held.Lock()
held.gen++
held.built = nil
held.Unlock()
}
// heldOf is what build gives, built once while a request holds it.
func heldOf[T any](key string, build func() T) T {
held.Lock()
if held.holds == 0 {
held.Unlock()
return build()
}
if h, ok := held.built[key]; ok && time.Since(h.at) < heldFor {
held.Unlock()
return h.v.(T)
}
gen := held.gen
held.Unlock()
v := build()
held.Lock()
if held.holds > 0 && held.gen == gen {
if held.built == nil {
held.built = map[string]heldValue{}
}
held.built[key] = heldValue{v, time.Now()}
}
held.Unlock()
return v
}
func heldEntries(build func() []Entry) []Entry {
// clipped: a caller's append copies
return heldOf("entries", func() []Entry { return slices.Clip(build()) })
}
// heldPlugins is plugin.Cached, which reads the plugins' list and
// sign-ins each time, and PluginOf asks for every model.
func heldPlugins() []plugin.Provider {
return heldOf("plugins", func() []plugin.Provider { return slices.Clip(plugin.Cached()) })
}
// heldSettings is the settings, read once while a request holds the
// catalog: a look at the agents asks them for every model of every agent.
// Only for reading — what it gives is shared by the request's look-ups.
func heldSettings() settings.Settings { return heldOf("settings", settings.Load) }
// HeldSettings is the settings as heldSettings reads them, for a look
// outside the package made many times in one request.
func HeldSettings() settings.Settings { return heldSettings() }