gui: a browser tab's font probe yields, and measures the generics once
Opening Settings in a browser tab (magpie web) held the page for 2.5 to
2.9 s on a cold start with the CPU slowed 4x (unscientific9: "冷启动进入设置"
is slow). The profile put the whole long task in webfonts.js drawn() ->
measureText: list() probed every installed family in one task, and each
probe measured monospace and serif again, so a Mac's few hundred families
cost up to four measures each, and a measure of a family not drawn yet
loads it.
Now the two generics' widths are measured once, so each family costs one
or two measures, and list() gives the page back every 8 ms.
Measured on mote (headless Chromium, 1280x820, CPU 4x, sandbox HOME with a
300k-row ledger, magpie web, Settings opened from Usage), two runs each:
- before (a build of 773902a77): ready 3121 / 2756 ms, long tasks
2978 / 2603 ms, longest 2885 / 2494 ms
- after: ready 255 / 257 ms, long tasks 147 / 151 ms, longest 96 / 100 ms
The app window (not a browser tab) doesn't probe; its Settings was ready
in 870 ms at 4x with a 256 ms longest task and /api/fonts at 355 ms on
the server, unchanged here.
Test: fonts.test.cjs "a browser tab's font probe never holds the page"
lists 400 families, makes each measureText take 1 ms, and asserts no task
runs over 300 ms, at most 2N+4 measures, and that an undrawn family isn't
shown. Chromium and WebKit, en and zh.
Run via mote-run: fonts.test.cjs 48 of 48 ok with the routing commit's files (101 ok).
Broken by hand: with the old webfonts.js the new test fails in all four
(chromium/webkit x en/zh); the other 44 fonts tests pass either way.
Not tried: a real Mac's font list in Safari or Chrome; the numbers are
headless Chromium with generated font names.
Reported-by: unscientific9 (on Discord)
This commit is contained in:
@@ -18,11 +18,15 @@ window.webFonts = (() => {
|
||||
let ctx, local = null, asked = false, base = [];
|
||||
const extra = [], seen = new Map();
|
||||
const quoted = window.desktopFonts.quoted;
|
||||
// The two generics' widths are measured once: each family then costs one
|
||||
// or two measures, not two or four.
|
||||
let generic;
|
||||
const width = (f) => (ctx.font = "72px " + f, ctx.measureText("mmmlli WwQ@ 0123 中文").width);
|
||||
function drawn(name) {
|
||||
if (!seen.has(name)) {
|
||||
ctx ||= document.createElement("canvas").getContext("2d");
|
||||
const w = (f) => (ctx.font = "72px " + f, ctx.measureText("mmmlli WwQ@ 0123 中文").width);
|
||||
seen.set(name, !!name && ["monospace", "serif"].some((b) => w(quoted(name) + "," + b) !== w(b)));
|
||||
generic ||= ["monospace", "serif"].map((b) => [b, width(b)]);
|
||||
seen.set(name, !!name && generic.some(([b, w]) => width(quoted(name) + "," + b) !== w));
|
||||
}
|
||||
return seen.get(name);
|
||||
}
|
||||
@@ -64,8 +68,17 @@ window.webFonts = (() => {
|
||||
local = group(await window.queryLocalFonts());
|
||||
}
|
||||
// list: the tab's families, from the server's (/api/fonts)
|
||||
// The probe yields every few milliseconds: a measure of a family not yet
|
||||
// drawn loads it, and probing a Mac's whole list at once held the click
|
||||
// that opened Settings in one task, 2.6 s with the CPU slowed 4x.
|
||||
async function list(server) {
|
||||
base = server.filter((f) => drawn(f.name));
|
||||
const out = [];
|
||||
let start = performance.now();
|
||||
for (const f of server) {
|
||||
if (drawn(f.name)) out.push(f);
|
||||
if (performance.now() - start > 8) await new Promise((r) => setTimeout(r)), start = performance.now();
|
||||
}
|
||||
base = out;
|
||||
if (!asked && window.queryLocalFonts) try {
|
||||
if ((await navigator.permissions.query({ name: "local-fonts" })).state === "granted") await read(), asked = true;
|
||||
} catch {}
|
||||
|
||||
@@ -368,6 +368,40 @@ for (const [engine, runtime] of engines) {
|
||||
} finally { await browser.close(); }
|
||||
});
|
||||
|
||||
// unscientific9: a tab's first open of Settings stuck while the probe
|
||||
// measured every server family in the click's task. Each measure is made
|
||||
// to cost 1 ms here, so 400 families held one task for 1.6 s before; the
|
||||
// probe now yields, and measures the two generics once.
|
||||
for (const lang of ["en", "zh"]) test(`${engine} ${lang}: a browser tab's font probe never holds the page`, async () => {
|
||||
const browser = await runtime.launch();
|
||||
try {
|
||||
const fonts = Array.from({ length: 400 }, (_, i) => ({ name: "Probe Family " + i, styles: [face("Probe Family " + i)] }));
|
||||
fonts.push({ name: "Georgia", styles: [face("Georgia")] });
|
||||
const init = () => {
|
||||
const measure = CanvasRenderingContext2D.prototype.measureText;
|
||||
window.__probe = { calls: 0, longest: 0 };
|
||||
let start = null;
|
||||
CanvasRenderingContext2D.prototype.measureText = function (text) {
|
||||
const now = performance.now();
|
||||
if (start === null) start = now, setTimeout(() => { start = null; });
|
||||
for (const end = now + 1; performance.now() < end;);
|
||||
window.__probe.calls++;
|
||||
window.__probe.longest = Math.max(window.__probe.longest, performance.now() - start);
|
||||
return measure.call(this, text);
|
||||
};
|
||||
};
|
||||
const { page, errors, close } = await open(browser, lang, { web: true, list: fonts, init });
|
||||
const probe = await page.evaluate(() => window.__probe);
|
||||
const shown = await page.evaluate(() => fontList.map(f => f.name));
|
||||
assert(!shown.some(n => n.startsWith("Probe Family")), "a family this browser doesn't draw isn't offered");
|
||||
assert(probe.calls >= fonts.length, "every family was measured: " + probe.calls);
|
||||
assert(probe.calls <= 2 * fonts.length + 4, "the generics are measured once: " + probe.calls + " measures");
|
||||
assert(probe.longest < 300, "the probe held one task for " + probe.longest.toFixed(0) + " ms");
|
||||
assert.deepEqual(errors, []);
|
||||
await close();
|
||||
} finally { await browser.close(); }
|
||||
});
|
||||
|
||||
// Norcleeh (magpie in Docker, its web GUI): the tab listed the server's
|
||||
// fonts, the container's, never the ones on his own computer. Where the
|
||||
// browser has the Local Font Access API, the press that opens a family
|
||||
|
||||
Reference in New Issue
Block a user