From 797a23cade5aa013a85b52cadc317fcc7f0c1c11 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 13 Nov 2026 01:19:26 +0800 Subject: [PATCH] gui: the tray holds its page words in one language, not all four The tray's tooltip names a usage window as its card does, with the page's own translations from assets/i18n.js. pageWords parsed all four language blocks (zh, zh-TW, ja, de, ~20k entries) once and kept them for the life of the process: 6.5 MB of an idle tray magpie's heap, for the few dozen words one language needs. It now parses only the block of the language asked for and keeps that one; a change of language reads the file again. Run (real account sandbox on mote, `tray`, no page, 150 s): - w35 (before): footprint 71 MB, live heap 16.7 MB; pageWord 6.5 MB cum. - w36 (this): footprint 67 MB, live heap 12.4 MB; pageWord 0.5 MB cum. On mica: go vet ./internal/gui; go test -run 'PageWords|TrayTip|Tray' ./internal/gui ok, with a new TestParsePageWordsInOneLanguage (one block read; ja then zh each give their own word). gofmt clean. Not run: the full suite. --- internal/gui/traytip_words_test.go | 15 ++++++++- internal/gui/traywords.go | 52 ++++++++++++++++++++++-------- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/internal/gui/traytip_words_test.go b/internal/gui/traytip_words_test.go index c6a92f0bd..fc1ff3016 100644 --- a/internal/gui/traytip_words_test.go +++ b/internal/gui/traytip_words_test.go @@ -65,7 +65,7 @@ func TestPluginWindowWordsSaidInEveryLanguage(t *testing.T) { } for _, lang := range []string{"zh", "zh-TW", "ja", "de"} { for _, w := range words { - if _, ok := pageWords()[lang][w]; !ok { + if _, ok := pageWords(lang)[w]; !ok { t.Errorf("i18n.js's %s doesn't say %q", lang, w) } } @@ -84,3 +84,16 @@ func TestParsePageWords(t *testing.T) { t.Errorf("parsed a comment or past the table: %v", got) } } + +// parsePageWordsIn reads only the language asked for: the tray holds one +// language's words, not the page's four. +func TestParsePageWordsInOneLanguage(t *testing.T) { + src := "const I18N = {\n zh: {\n \"Credits\": \"积分\",\n },\n \"zh-TW\": {\n \"Credits\": \"積分\",\n },\n};\n" + got := parsePageWordsIn(src, "zh-TW") + if len(got) != 1 || got["zh-TW"]["Credits"] != "積分" { + t.Errorf("parsed %v", got) + } + if pageWords("ja")["Credits"] == "" || pageWords("zh")["Credits"] == pageWords("ja")["Credits"] { + t.Errorf("ja then zh: %q %q", pageWords("ja")["Credits"], pageWords("zh")["Credits"]) + } +} diff --git a/internal/gui/traywords.go b/internal/gui/traywords.go index 6084e1c6c..3adf4e948 100644 --- a/internal/gui/traywords.go +++ b/internal/gui/traywords.go @@ -8,20 +8,37 @@ import ( "github.com/yetone/magpie/internal/lazyre" ) -// pageWords are the page's own translations (assets/i18n.js's I18N), by -// language, read once: the words a usage card's window is named and -// counted in ("Credits", "credits", "5 hours") come from the vendor or a -// plugin in English, and the Usage page says them with t(). The tray's -// tooltip says them with the same table (pageWord), so a window reads in -// the tray as on its card, in every language the page has (yi_time on -// Discord: "Credits 0 / 500 credits · 已用 0%"). -var pageWords = sync.OnceValue(func() map[string]map[string]string { +// pageWords are the page's own translations (assets/i18n.js's I18N) in +// one language, read when first asked for it: the words a usage card's +// window is named and counted in ("Credits", "credits", "5 hours") come +// from the vendor or a plugin in English, and the Usage page says them +// with t(). The tray's tooltip says them with the same table (pageWord), +// so a window reads in the tray as on its card, in every language the +// page has (yi_time on Discord: "Credits 0 / 500 credits · 已用 0%"). +// Only the language last asked for is kept: all four blocks held ~4.5 MB +// of an idle tray magpie's heap, for the few words one language needs. +func pageWords(lang string) map[string]string { + pageWordsMu.Lock() + defer pageWordsMu.Unlock() + if pageWordsLang == lang && pageWordsHeld != nil { + return pageWordsHeld + } b, err := assets.ReadFile("assets/i18n.js") if err != nil { return nil } - return parsePageWords(string(b)) -}) + pageWordsLang, pageWordsHeld = lang, parsePageWordsIn(string(b), lang)[lang] + if pageWordsHeld == nil { + pageWordsHeld = map[string]string{} + } + return pageWordsHeld +} + +var ( + pageWordsMu sync.Mutex + pageWordsLang string + pageWordsHeld map[string]string +) var ( pageLangHead = lazyre.New(`^ "?([\w-]+)"?: \{`) @@ -34,6 +51,12 @@ var ( // JSON can't read (a JS-only escape) is left out: that word reads in // English, as the page's own fallback does. func parsePageWords(src string) map[string]map[string]string { + return parsePageWordsIn(src, "") +} + +// parsePageWordsIn is parsePageWords with only lang's block read ("" for +// every block). +func parsePageWordsIn(src, lang string) map[string]map[string]string { if i := strings.Index(src, "\n};"); i >= 0 { src = src[:i] } @@ -41,8 +64,11 @@ func parsePageWords(src string) map[string]map[string]string { var cur map[string]string for _, line := range strings.Split(src, "\n") { if m := pageLangHead.FindStringSubmatch(line); m != nil { - cur = map[string]string{} - out[m[1]] = cur + cur = nil + if lang == "" || m[1] == lang { + cur = map[string]string{} + out[m[1]] = cur + } continue } if cur == nil || !strings.HasPrefix(line, " \"") { @@ -62,7 +88,7 @@ func parsePageWords(src string) map[string]map[string]string { // pageWord is s as the page says it in lang (its t()): s itself in // English, or where the page has no word for it. func pageWord(lang, s string) string { - if v, ok := pageWords()[lang][s]; ok && v != "" { + if v, ok := pageWords(lang)[s]; ok && v != "" { return v } return s