gui: the tray keeps the words it asked for, not a language's whole table
The tray tooltip says each usage window's name and unit the way the Usage page does, from i18n.js's table. pageWords kept the language's whole parsed table for good: ~2 MB of an idle tray magpie's live heap (pprof inuse_space, real-account tray run on mote), for the dozen words a card asks for. Now pageWord keeps only the words asked for (capped at 512). A new word parses the table once, and the table is kept a minute for the other new words of the same card, then let go on the next ask. A word asked before is answered without parsing again.
This commit is contained in:
@@ -97,3 +97,50 @@ func TestParsePageWordsInOneLanguage(t *testing.T) {
|
||||
t.Errorf("ja then zh: %q %q", pageWords("ja")["Credits"], pageWords("zh")["Credits"])
|
||||
}
|
||||
}
|
||||
|
||||
// The tray keeps the words it asked for, not the language's whole table:
|
||||
// a word asked again is not looked up again, and the table a lookup
|
||||
// parsed is let go a minute later.
|
||||
func TestPageWordKeepsOnlyAskedWords(t *testing.T) {
|
||||
wordsMu.Lock()
|
||||
wordsLang, wordsAsked, wordsTable = "", nil, nil
|
||||
wordsMu.Unlock()
|
||||
t.Cleanup(func() {
|
||||
wordsMu.Lock()
|
||||
wordsLang, wordsAsked, wordsTable = "", nil, nil
|
||||
wordsMu.Unlock()
|
||||
})
|
||||
parsed := func() int {
|
||||
wordsMu.Lock()
|
||||
defer wordsMu.Unlock()
|
||||
return wordsParsed
|
||||
}
|
||||
now := time.Now()
|
||||
start := parsed()
|
||||
if askWord("zh", "Credits", now) == "" || askWord("zh", "5 hours", now) == "" {
|
||||
t.Fatal("zh has Credits and 5 hours")
|
||||
}
|
||||
if askWord("zh", "no such words", now) != "" {
|
||||
t.Fatal("a word the page doesn't have")
|
||||
}
|
||||
if n := parsed() - start; n != 1 {
|
||||
t.Fatalf("one card's words parsed the table %d times, want 1", n)
|
||||
}
|
||||
later := now.Add(wordsTableFor)
|
||||
for range 3 {
|
||||
askWord("zh", "Credits", later)
|
||||
askWord("zh", "no such words", later)
|
||||
}
|
||||
wordsMu.Lock()
|
||||
held := wordsTable != nil
|
||||
wordsMu.Unlock()
|
||||
if held {
|
||||
t.Fatal("the table is still held a minute after the lookup")
|
||||
}
|
||||
if n := parsed() - start; n != 1 {
|
||||
t.Fatalf("words asked before were looked up again: %d parses", n)
|
||||
}
|
||||
if askWord("ja", "Credits", later) == askWord("zh", "Credits", later) {
|
||||
t.Fatal("ja answered with zh's word")
|
||||
}
|
||||
}
|
||||
|
||||
+52
-21
@@ -4,40 +4,47 @@ import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/lazyre"
|
||||
)
|
||||
|
||||
// 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.
|
||||
// one language, parsed from the source each time they are asked for: 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%").
|
||||
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
|
||||
}
|
||||
pageWordsLang, pageWordsHeld = lang, parsePageWordsIn(string(b), lang)[lang]
|
||||
if pageWordsHeld == nil {
|
||||
pageWordsHeld = map[string]string{}
|
||||
if m := parsePageWordsIn(string(b), lang)[lang]; m != nil {
|
||||
return m
|
||||
}
|
||||
return pageWordsHeld
|
||||
return map[string]string{}
|
||||
}
|
||||
|
||||
// The tray asks pageWord for a dozen words or so, again on every usage
|
||||
// read. One language's whole table held ~2 MB of an idle tray magpie's
|
||||
// heap, so only the words asked for are kept (wordsAsked); the table a
|
||||
// new word was looked up in is kept a minute (wordsTable), for the other
|
||||
// new words of the same card, and let go on the first ask after that.
|
||||
var (
|
||||
pageWordsMu sync.Mutex
|
||||
pageWordsLang string
|
||||
pageWordsHeld map[string]string
|
||||
wordsMu sync.Mutex
|
||||
wordsLang string
|
||||
wordsAsked map[string]string
|
||||
wordsTable map[string]string
|
||||
wordsTableAt time.Time
|
||||
wordsParsed int // how many times a table was parsed, for tests
|
||||
)
|
||||
|
||||
const (
|
||||
wordsTableFor = time.Minute
|
||||
wordsAskedMax = 512 // a vendor's names are few; past this, asked again
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -88,12 +95,36 @@ func parsePageWordsIn(src, lang 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 := askWord(lang, s, time.Now()); v != "" {
|
||||
return v
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func askWord(lang, s string, now time.Time) string {
|
||||
wordsMu.Lock()
|
||||
defer wordsMu.Unlock()
|
||||
if wordsLang != lang {
|
||||
wordsLang, wordsAsked, wordsTable = lang, nil, nil
|
||||
}
|
||||
if wordsTable != nil && now.Sub(wordsTableAt) >= wordsTableFor {
|
||||
wordsTable = nil
|
||||
}
|
||||
if v, ok := wordsAsked[s]; ok {
|
||||
return v
|
||||
}
|
||||
if wordsTable == nil {
|
||||
wordsTable, wordsTableAt = pageWords(lang), now
|
||||
wordsParsed++
|
||||
}
|
||||
v := wordsTable[s]
|
||||
if wordsAsked == nil || len(wordsAsked) >= wordsAskedMax {
|
||||
wordsAsked = map[string]string{}
|
||||
}
|
||||
wordsAsked[s] = v
|
||||
return v
|
||||
}
|
||||
|
||||
// trayWindowName is a window's name as its card says it in lang: the
|
||||
// name whole when the page has it, else each " · " part the page has
|
||||
// ("Gemini · 5 hours", a pool's window named with its pool by trayShared,
|
||||
|
||||
Reference in New Issue
Block a user