diff --git a/internal/plugin/market.go b/internal/plugin/market.go index ea7f60928..0116de6cd 100644 --- a/internal/plugin/market.go +++ b/internal/plugin/market.go @@ -19,6 +19,7 @@ import ( "unicode/utf8" "github.com/yetone/magpie/internal/appdir" + "github.com/yetone/magpie/internal/filememo" "github.com/yetone/magpie/internal/ghtoken" "github.com/yetone/magpie/internal/source" "github.com/yetone/magpie/internal/update" @@ -32,6 +33,12 @@ import ( //go:embed market.json var builtinMarket []byte +// builtinParsed is the built-in copy, parsed once; shared, so only read. +var builtinParsed = sync.OnceValue(func() []Listing { + l, _ := parseMarket(builtinMarket) + return l +}) + // MarketURL is where the list is kept; MAGPIE_PLUGIN_MARKET moves it, and // "off" keeps to the built-in copy. const MarketURL = "https://raw.githubusercontent.com/magpie-community/plugins/main/registry.json" @@ -795,11 +802,12 @@ func Icon(spec, id string) string { l := marketList marketMu.Unlock() if l == nil { - if b, err := os.ReadFile(marketCache()); err == nil { - l, _ = parseMarket(b) - } + // parsed once for each copy, not for each plugin each look: the + // Providers page parsed it for every plugin's row each poll, 13 MB + // of what twenty polls allocated with real accounts + l, _ = filememo.Read("plugin-market", marketCache(), parseMarket) if l == nil { - l, _ = parseMarket(builtinMarket) + l = builtinParsed() } } pkg := Name(spec) diff --git a/internal/plugin/market_icon_once_test.go b/internal/plugin/market_icon_once_test.go new file mode 100644 index 000000000..6b0ac3603 --- /dev/null +++ b/internal/plugin/market_icon_once_test.go @@ -0,0 +1,48 @@ +package plugin + +import ( + "os" + "path/filepath" + "testing" + "time" +) + +// A plugin's icon is looked up for every plugin's row each poll of the +// Providers page, before the market is fetched (or with no network): the +// cached copy was read and parsed whole each time, 13 MB of what twenty +// polls allocated with real accounts. It is parsed once for each copy now, +// and a copy written again is read again. +func TestMarketIconParsesTheCopyOnce(t *testing.T) { + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + marketMu.Lock() + marketList = nil + marketMu.Unlock() + if err := os.MkdirAll(filepath.Dir(marketCache()), 0o755); err != nil { + t.Fatal(err) + } + write := func(b []byte, at time.Time) { + t.Helper() + if err := os.WriteFile(marketCache(), b, 0o644); err != nil { + t.Fatal(err) + } + os.Chtimes(marketCache(), at, at) + } + old := time.Now().Add(-time.Hour) + // the copy as GitHub gives it: the list built in is one + write(builtinMarket, old) + const grok = "@magpie-community/opencode-grok-auth@0.1.0" + if got := Icon(grok, "grok"); got != "xai" { + t.Fatalf("Icon = %q, want the copy's xai", got) + } + if n := testing.AllocsPerRun(50, func() { Icon(grok, "grok") }); n > 20 { + t.Fatalf("an unchanged copy: %.0f allocations a look, parsed again", n) + } + write([]byte(`{"version":1,"plugins":[{"package":"opencode-x-auth","name":"X","icon":"x-two","community":true}]}`), old.Add(time.Minute)) + if got := Icon("opencode-x-auth@1.0.0", "x"); got != "x-two" { + t.Fatalf("after the copy was written again, Icon = %q", got) + } + os.Remove(marketCache()) + if got := Icon(grok, "grok"); got != "xai" { + t.Fatalf("with no copy, the built-in list's: %q", got) + } +}