plugin: a plugin's market icon parses the cached list once per copy

Before the market is fetched, or with no network, plugin.Icon read the
cached plugin-market.json and parsed it whole for every look. The
Providers page looks once for each plugin's row on each poll: 13 MB of
what twenty polls allocated with real accounts.

The copy is now read through filememo.Read, which parses it again only
when the file changes. The built-in list is parsed once (sync.OnceValue).
Both are shared and only read.

Test: TestMarketIconParsesTheCopyOnce. It caches the built-in list as the
copy and allows at most 20 allocations a look. With the old ReadFile and
parseMarket put back by hand it fails at 260 allocations a look. It also
checks that a rewritten copy is read again and that with no copy the
built-in list answers.

Run on mote at this tree:
- gofmt, go vet, and GOOS=windows and GOOS=linux vet of internal/plugin
  and internal/provider: ok.
- TestMarketIcon* and TestMarketSources alone, 3 times: ok.
- go test -timeout 45m ./internal/plugin ./internal/provider: ok.
- ./internal/gui: TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners (#3949) fail, both already filed. Nothing else
  failed.
This commit is contained in:
yetone
2026-11-01 05:51:13 +08:00
parent 3a3bdb9f23
commit b19d085935
2 changed files with 60 additions and 4 deletions
+12 -4
View File
@@ -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)
+48
View File
@@ -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)
}
}