plugins: a row's picture is asked for as it nears the screen (#909, #1956, #2459)

Each SVG an <img> draws is a document of its own in the webview (its
own Page, style engine and DOM, ~0.6 MB), kept while the window is
open. Opening Plugins drew 122 pictures at once, 57 of them SVGs, so
the renderer took 58 documents more, most for rows far below the fold.

icon() takes a lazy flag that sets loading="lazy" on the <img> it makes
for a stored picture or a -color/PNG icon; Plugins' rows and cards pass
it, the plugin's own page header (big) does not. Menus and other pages
keep loading at once, so nothing that opens on screen waits a frame.

Measured in Chromium against a dev backend with a copy of the real
library and its stored icons, every page visited then on Settings
(memory-infra dump of the renderer, then ps RSS):
- documents: 81 -> 37
- malloc 90.3 -> 81.7 MB, blink_gc 41.7 -> 39.7, partition_alloc
  25.4 -> 20.5, image cache 5.3 -> 1.3
- renderer RSS 256-289 -> 243 MiB
Scrolled to the bottom of Discover, all 122 pictures load in Chromium
and WebKit (screenshot checked); before scrolling, 52 and 18.

Run at this head:
- go test ./internal/gui -count=1 -timeout 45m: ok 125s
- GUI: tests/plugin*.test.cjs, models-add-and-icons, mono-icon-redraw,
  routing-log-icons, tray-logos, i18n-keys: 691 of 693 pass; the one
  red is webkit en of plugin-card-update, waiting 5s for the Update
  button under load 10 (#3079, filed on main 75e769a9f), and 3 of 3
  alone pass.
- plugin-own-icon now checks a row's picture is lazy and loads on
  screen, and the page header's is not: 22 of 22.
Broken by hand: logo() without the flag turns plugin-own-icon red.
Not tried: WebView2 and WebKitGTK themselves (both support
loading=lazy; an engine without it loads at once as before).
This commit is contained in:
yetone
2026-10-26 17:36:38 +08:00
parent 6e96903346
commit 689cf41423
3 changed files with 14 additions and 3 deletions
+6 -1
View File
@@ -295,7 +295,10 @@ function maskSpan(src, failed) {
}
const pngIcons = new Set(["crush", "zcode", "alma", "hanako", "cindy", "lody", "typesafe", "atomcode", "snow", "mlx-serve", "mothx", "zoo-code"]);
function icon(name) {
// lazy: a picture in a long list (Plugins' rows) is asked for when it
// nears the screen. Each SVG an <img> draws is a document of its own in
// the webview, ~0.6 MB, kept while the page is open (#909)
function icon(name, lazy) {
const kept = keptIcons?.get(name || "")?.shift();
if (kept) { kept.removeAttribute("title"); return kept; }
const e = el("span", "ic");
@@ -308,6 +311,7 @@ function icon(name) {
if (name?.startsWith("file:")) {
// a picture the user gave their own provider
const img = el("img");
if (lazy) img.loading = "lazy";
img.src = "/api/icons/" + encodeURIComponent(name.slice(5));
img.alt = "";
img.draggable = false;
@@ -318,6 +322,7 @@ function icon(name) {
if (name) {
if (name.endsWith("-color") || pngIcons.has(name)) {
const img = el("img");
if (lazy) img.loading = "lazy";
img.src = `icons/${name}.${pngIcons.has(name) ? "png" : "svg"}`;
img.alt = "";
img.draggable = false;
+1 -1
View File
@@ -139,7 +139,7 @@
}
function logo(ic, big, kind) {
const box = el("span", "pm-logo" + (big ? " big" : ""));
if (ic) box.append(icon(ic));
if (ic) box.append(icon(ic, !big));
else box.append(glyph({ mw: PIPE, ag: TERM, tl: TOOL, pg: PAGE }[kind] || PUZZLE, big ? 22 : 17, 1.4));
return box;
}
+7 -1
View File
@@ -5,7 +5,7 @@
// plugin's picture, not its first provider's; the providers keep theirs in
// the Add sheet. One whose package gives none still wears its first
// provider's, as before. Every language, both engines, wide and narrow;
// the API is faked here.
// the API is faked here. A row's picture loads lazily, the page's at once.
const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const path = require("node:path");
@@ -67,6 +67,11 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
await suite.waitFor();
assert.equal(await suite.locator(".pm-logo img").getAttribute("src"), "/api/icons/suite.png", "the plugin's own, not its first provider's");
assert.equal(await row("plain").locator(".pm-logo img").getAttribute("src"), "/api/icons/pc.png", "none of its own: its first provider's, as before");
// a row's picture is asked for as it nears the screen: each SVG an
// <img> draws is a document of its own in the webview (#909); one
// on screen still loads
assert.equal(await suite.locator(".pm-logo img").getAttribute("loading"), "lazy");
await page.waitForFunction(() => document.querySelector("#view-plugins .pm-row .pm-logo img")?.naturalWidth > 0);
const box = await suite.boundingBox();
assert.ok(box.x >= 0 && box.x + box.width <= width, `the row fits: ${JSON.stringify(box)}`);
@@ -75,6 +80,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
const dlg = page.locator("#modal .pm-detail");
await dlg.waitFor();
assert.equal(await dlg.locator(".pm-dhead .pm-logo img").getAttribute("src"), "/api/icons/suite.png");
assert.equal(await dlg.locator(".pm-dhead .pm-logo img").getAttribute("loading"), null, "the page's own picture loads at once");
assert.deepEqual(errors, []);
await page.context().close();
});