TestCoreScriptBudget has been red on main since5a0e0378c(06:00): the core's scripts were 1,265,848 bytes against 1,265,544.5a0e0378cadded 344 bytes of code to plugins.js, and its body says the budget passed at7583ed65c, but at7583ed65cthe core had only 40 bytes of room. CI's first full run after it (e9e63c6d7, run 43323956017) went red on ubuntu and macOS with those numbers. The redraw that followed the import, and the threshold, now live in plugingrid.js: pmVirtualGrids(page, card, body, draw) draws Discover's body again from a microtask, once plugins.js holds what it returned, when a list of VMIN cards or more was drawn whole before the import came. It returns min, so plugins.js reads the threshold from it. plugins.js keeps only the import, cardGrid and the three now() calls: 136 bytes out. The budget carries the other 168, with why in its comment. The comment also had ee1553913's "+1,112 for Codex's Open button" twice; the budget was raised by 1,112 once (1,264,432 to 1,265,544), so one copy goes. Run on mote at this tree (origin/main390dd407bplus this), mote-run: - go vet ./internal/gui; go test -run 'TestCoreScriptBudget|TestAppScript' ./internal/gui: ok. gofmt -l: nothing. node --check on both files. - plugin-discover-virtual 22 of 22, plugin-market 6, plugin-kinds-discover 22, plugins-more-discover 6, plugin-progressive 6, i18n-keys 4: all pass. - old-webkit: 2 reds, the known ones (headerpresets.js #6030, :has() #5919), as5a0e0378crecorded. - Recomputed the budget's measure at7583ed65c,5a0e0378cand this tree with a copy of codeBytes: 1,265,504, 1,265,848, 1,265,712. Broken by hand: with the microtask redraw taken out, plugin-discover-virtual still passes 22 of 22, so the "drawn whole before the import" redraw has no test, before this change and after it. Not tried: the real app; a long list drawn before plugingrid.js arrives.
76 lines
3.3 KiB
Go
76 lines
3.3 KiB
Go
package gui
|
||
|
||
import (
|
||
"os"
|
||
"path/filepath"
|
||
"regexp"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// What the window's core may weigh, in bytes of script without its
|
||
// whole-line comments: everything index.html loads that isn't a plugin's
|
||
// (assets/plugins/<id>/) and isn't the words (i18n.js). A page belongs in
|
||
// a plugin. Each page moved out lowers these to what is left, so a page
|
||
// can't quietly grow back into the core; a change that needs more raises
|
||
// them and says why in its commit. Comments aren't counted, so they are
|
||
// never what pays for a change (#5918); the history of each number is in
|
||
// `git log -L` of this file.
|
||
const (
|
||
appScriptBudget = 1_086_267 // app.js: 1,080,614 at 8016205e3, with the 5,653 of room it had
|
||
coreScriptBudget = 1_265_712 // every core script at 8016205e3, the old measure 682 over its cap there (#5918); +367 for the model test on a chosen account (tmr11235), no page left to move it to; +151 for the provider menu's "Only on this device" item and its four translations; +458 for Settings › About's Report a problem row and its translations; +113 for withScheme, the one rule every typed address goes through (eachg0 on X), cut to the shortest form that keeps it whole; +10 for the kept whole's tooltip (Musikrv77852 on X), the past-cycle line reused for it, no page left to move it to; +1,112 for Codex's Open button and its Open Codex with menu (Yink88564391 on X), packed to the shortest form, no page left to move them to; +168 for Discover's long lists drawn in part (the owner, 11-13), after the rest of its hook moved into plugingrid.js, imported when Plugins first draws
|
||
)
|
||
|
||
// codeBytes is how many bytes of b are code: its blank lines, its lines
|
||
// that are only a // comment, and its /* */ blocks that start a line,
|
||
// aren't counted.
|
||
func codeBytes(b []byte) int {
|
||
n, block := 0, false
|
||
for _, l := range strings.SplitAfter(string(b), "\n") {
|
||
t := strings.TrimSpace(l)
|
||
switch {
|
||
case block:
|
||
block = !strings.Contains(t, "*/")
|
||
case t == "", strings.HasPrefix(t, "//"):
|
||
case strings.HasPrefix(t, "/*") && (strings.HasSuffix(t, "*/") || !strings.Contains(t, "*/")):
|
||
block = !strings.HasSuffix(t, "*/")
|
||
default:
|
||
n += len(l)
|
||
}
|
||
}
|
||
return n
|
||
}
|
||
|
||
func TestCoreScriptBudget(t *testing.T) {
|
||
page, err := os.ReadFile("assets/index.html")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
total, n := 0, 0
|
||
for _, m := range regexp.MustCompile(`<script src="([^"?]+)`).FindAllStringSubmatch(string(page), -1) {
|
||
name := m[1]
|
||
if strings.HasPrefix(name, "plugins/") || name == "i18n.js" {
|
||
continue
|
||
}
|
||
b, err := os.ReadFile(filepath.Join("assets", name))
|
||
if os.IsNotExist(err) {
|
||
continue // made by the server as it serves the page (boot.js)
|
||
}
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
code := codeBytes(b)
|
||
if name == "app.js" && code > appScriptBudget {
|
||
t.Errorf("app.js is %d bytes of code, over its budget of %d: put the page's code in its plugin, or raise the budget and say why (comments aren't counted, so cutting them doesn't help)", code, appScriptBudget)
|
||
}
|
||
total += code
|
||
n++
|
||
}
|
||
if n < 5 {
|
||
t.Fatalf("found only %d core scripts in index.html", n)
|
||
}
|
||
if total > coreScriptBudget {
|
||
t.Errorf("the core's scripts are %d bytes of code, over their budget of %d: put the page's code in its plugin, or raise the budget and say why (comments aren't counted, so cutting them doesn't help)", total, coreScriptBudget)
|
||
}
|
||
}
|