routing: wire lengths and ends are read from their d, not getTotalLength (#3813)

linull24 (#3813): the Routing page used too much over mobile remote.
Reproduced it with a 390x844 phone viewport at 4x CPU throttle, three
live and three done requests coming every 400ms: the main thread was
busy 0.88s of each second. A 10s profile had getTotalLength at 1.66s,
the largest single item: while rows slide (the 520ms after each
rebuild, when layout() runs every frame) every wire's d is new each
frame, and measure() asked the browser for each one's length just to
find its ends, then pose() asked again for each leg of every flight.

- measure() no longer asks for a length up front.
- A wire's ends and the way it heads there are read off its d
  (wireEnds: magpie's own absolute M/L/Q/C), the browser only for
  any other d.
- A path's length is its points' (flat(), which pointAt already
  used), so pointAt reads s along them directly; the browser's
  getTotalLength is kept for a d flat() can't read.

Measured (same probe, same load, main's client.js swapped in for the
"before"): layouts 590/s -> 235/s, script 0.187 -> 0.144 s/s, main
thread 0.89 -> 0.84 s/s at load ~100; 0.71-0.74 s/s at load ~10.
getTotalLength is gone from the profile. A per-frame check that each
row's wire ends at the row's own centre while rows slide: worst 0px
over 1546 frames, as on main.

Tried and dropped: drawing a slide's frames from the boxes of the
last layout, each moved by its row's eased slide. Rows also move
during a slide for reasons that aren't the transform (a row above
changing height), and the wires were off by up to 153px.

Not changed: the rest of a busy page is the browser's own style,
layout and paint of the motion; with reduced motion it is 0.31 s/s.

GUI (both engines): every routing-*, panel-routing* and idle-ticks
file at 6ce0f14b9 with this change. Reds were in 12 files, all rule /
pause / model-rest timeouts; the same 12 files with main's client.js
gave 89 reds against 91 here, the same tests, timeouts landing on
different subtests at load 40-110. Filed already: #3096, #3094,
#2115, #3410, #3408, #3593, #3411. routing-busy-frames, routing-busy,
routing-flood, routing-idle-frame and idle-ticks pass.

On the rebased tree (parent b5582912a, which changed app.js): routing-busy-frames, routing-busy, routing-flood, routing-idle-frame, idle-ticks, routing-steady-garbage, routing-held-dot and routing-js, both engines, 65 of 65 ok; go test -tags nogui -run 'Budget|Script' ./internal/gui ok.
This commit is contained in:
yetone
2026-10-27 16:46:48 +08:00
parent b5582912af
commit 68bbcc01c5
+63 -12
View File
@@ -1386,15 +1386,27 @@ function routingPage(part) {
// three agents sending at once, ~10 s of a 40 s look at the page went to
// getTotalLength and getPointAtLength alone, and the page lagged
// (yg6888 on X: "打开页面很卡").
const measured = new WeakMap(); // path → { d, L, tips, pts, flat }
// Neither is asked of the browser for a d of magpie's own: its length is
// that of its points (flat) and the wire ends a flight meets are read
// from it (wireEnds). While rows slide every wire's d is new each frame,
// and getTotalLength on each was a quarter of a busy page's time
// (#3813).
const measured = new WeakMap(); // path → { d, L, tips, pts, flat, ends }
function measure(p) {
const d = p.isConnected && p.getAttribute("d");
if (!d) return null;
let m = measured.get(p);
if (!m || m.d !== d) measured.set(p, m = { d, L: p.getTotalLength?.() || 0, tips: [], pts: [] });
if (!m || m.d !== d) measured.set(p, m = { d, tips: [], pts: [] });
return m;
}
const pathLength = (p) => measure(p)?.L || 0;
const lengthOf = (p, m) => {
if (m.L === undefined) {
if (m.flat === undefined) m.flat = flat(m.d);
m.L = m.flat ? m.flat.len[m.flat.len.length - 1] : p.getTotalLength?.() || 0;
}
return m.L;
};
const pathLength = (p) => { const m = measure(p); return m ? lengthOf(p, m) : 0; };
// pointAt is the point s along p, read between the points measured every
// ptStep along it, each measured once for its d: every flight asked
// getPointAtLength three times a frame, and the flights on one wire asked
@@ -1441,13 +1453,10 @@ function routingPage(part) {
}
function pointAt(p, s) {
const m = measure(p);
if (!m?.L) return p.getPointAtLength(s);
if (!m || !lengthOf(p, m)) return p.getPointAtLength(s);
s = Math.max(0, Math.min(m.L, s));
if (m.flat === undefined) m.flat = flat(m.d);
if (m.flat) {
// the browser's length and the points' differ by a hair: s is read
// as the same share of the way along
const { xs, ys, len } = m.flat, u = s / m.L * len[len.length - 1];
const { xs, ys, len } = m.flat, u = s;
let lo = 0, hi = len.length - 1;
while (hi - lo > 1) { const mid = (lo + hi) >> 1; if (len[mid] <= u) lo = mid; else hi = mid; }
const f = len[hi] > len[lo] ? (u - len[lo]) / (len[hi] - len[lo]) : 0;
@@ -1464,16 +1473,58 @@ function routingPage(part) {
// tip is where a flight meets a wire's end or start, and which way it
// heads there: along the wire, or back along it
const tip = (p, atEnd, back) => () => {
const m = measure(p), L = m?.L;
if (!L) return null; // not laid out: nowhere to meet it yet
const m = measure(p);
if (!m) return null; // not laid out: nowhere to meet it yet
const key = (atEnd ? 2 : 0) + (back ? 1 : 0);
if (m.tips[key]) return m.tips[key];
const a = p.getPointAtLength(atEnd ? L : 0), b = p.getPointAtLength(atEnd ? Math.max(0, L - 2) : Math.min(L, 2));
let tx = atEnd ? a.x - b.x : b.x - a.x, ty = atEnd ? a.y - b.y : b.y - a.y;
if (m.ends === undefined) m.ends = wireEnds(m.d);
let a, tx, ty;
if (m.ends) {
const e = m.ends[atEnd ? 1 : 0];
a = e; tx = e.tx; ty = e.ty;
} else {
const L = lengthOf(p, m);
if (!L) return null;
a = p.getPointAtLength(atEnd ? L : 0);
const b = p.getPointAtLength(atEnd ? Math.max(0, L - 2) : Math.min(L, 2));
tx = atEnd ? a.x - b.x : b.x - a.x; ty = atEnd ? a.y - b.y : b.y - a.y;
}
if (back) { tx = -tx; ty = -ty; }
const n = Math.hypot(tx, ty) || 1;
return m.tips[key] = { x: a.x, y: a.y, tx: tx / n, ty: ty / n };
};
// wireEnds are a d's start and end, each with the way it heads there (along
// the path at its end, into it at its start), read off its first and
// last pieces: the line or control point next to each end that isn't
// the end itself. Only for magpie's own absolute M, L, Q and C, and
// null for any other d, or one with no way at all, which the browser
// measures instead.
function wireEnds(d) {
const n = d.match(/[A-Za-z]|-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi) || [], k = { M: 2, L: 2, Q: 4, C: 6 };
const pts = []; // [x, y], each point the d names in turn
let c = "";
for (let i = 0; i < n.length;) {
if (/[A-Za-z]/.test(n[i])) { c = n[i++]; continue; }
if (!k[c] || (c === "M" && pts.length)) return null;
const a = n.slice(i, i += k[c]).map(Number);
if (a.length < k[c] || a.some(isNaN)) return null;
for (let j = 0; j < a.length; j += 2) pts.push([a[j], a[j + 1]]);
if (c === "M") c = "L";
}
if (pts.length < 2) return null;
const way = (from, step) => {
const [x, y] = pts[from];
for (let j = from + step; j >= 0 && j < pts.length; j += step) {
const tx = (pts[j][0] - x) * step, ty = (pts[j][1] - y) * step, h = Math.hypot(tx, ty);
if (h > 1e-9) return { x, y, tx: tx / h, ty: ty / h };
}
return null;
};
// at its start, from the start to what comes next; at its end, from
// what came before to the end
const s = way(0, 1), e = way(pts.length - 1, -1);
return s && e ? [s, e] : null;
}
// via is the way through magpie from one wire to the next: a gentle arc
// across it, carrying on the way the flight came in and leaving the way
// the next wire goes — or, out the side it came in, a loop round inside.