routing: a flowing wire is dashed only near what is in sight (#3164)

A wire to a key hundreds of rows down is ~18,000px long, and WebKit
stroked every dash of it again on each frame it flowed (.live's
dashoffset animation), keeping Routing under 50 fps with both groups
open. Each live wire now gets a stroke-dasharray that is the usual
"3 5" only from a screen above to a screen below what is in sight,
started on a multiple of 8px so it stays in step with the whole
wire's, and a gap elsewhere. The edges move in steps of 200px, so
scrolling redashes a wire only now and then. layout() records where
the stage and the view were (it reads them anyway) and the view's
scroll events track the scroll, so flowNear forces no layout of its
own.

Where a wire crosses those edges is measured from its own d (magpie's
M, L, Q and C, each bend walked in 16 steps, edges moved out 64px).
A first version asked WebKit's getPointAtLength, which cost ~3.8ms a
call, 780ms per 15s with 800 keys: a wire's d changes with most
updates, so a cache missed 147 of 179 times. Measured from d it is
13ms per 15s.

Run:
- Sandbox magpie app, sandbox HOME, fake upstream under load, Routing
  with both groups open (~900 rows), first version, WebKit paired runs
  (old and new at once, 20s, 5 pairs): frames 913/914/936/912/893 ->
  1012/1018/1029/1004/979 (about +10%).
- Page-route mock (routing-many-keys' 500 + 300 keys, a request every
  300ms, both folds open), this version against v0.1.1558's client.js,
  WebKit, 15s, 3 pairs at once: frames 840/843/844 -> 870/876/875,
  on time (<18ms) 507/538/568 -> 660/670/668, jank over 16.7ms
  1799/1693/1638 -> 1221/1187/1189ms. Chromium 901 -> 901 (60 fps).
- Coverage in the mock, WebKit and Chromium, wheeled to scrollTop 0,
  4000, 9000, 15000, 22000, 30000, 2000, 0, read 40ms and 400ms after:
  every point of every live wire inside the view (2824 sampled) lies
  inside its dashed window. The first try of measuring from d missed
  long straight L steps (a lane 5000px down got a window of 64px) and
  this check caught it; crossings are now interpolated inside a step.
- GUI: all 108 routing*.test.cjs files at this change, both engines:
  1341 pass, 47 fail, in 7 files that are red the same on c0985122b
  run beside it (intent-entry 4/4 vs 4/4 on a second pair, live-patch
  6/6, manual 2/2, pause 14/14, rule-effort 14/14, rule-remove 6/7,
  steady 1/1), each filed: #3408, #3637, #3409, #3410, #3411, #3096,
  #3412. routing-many-keys passes.
- node --check on client.js.

Not fixed: WebKit is still below 60 fps with both groups open. Hiding
rows past 40 gains more again, so the rest is the number of rows laid
out (rebuild alone is ~690ms per 15s); drawing only the rows in sight
is the next step on #3164.
This commit is contained in:
yetone
2026-10-26 09:15:53 +08:00
parent 7499ebb5ef
commit 19253f67bc
@@ -1491,6 +1491,71 @@ function routingPage(part) {
return s + ` C${b[0]} ${b[1]} ${c[0]} ${c[1]} ${x3} ${y3}`;
}
// a flowing wire is dashed only near what is in sight (#3164): one to a
// key hundreds of rows down is tens of thousands of pixels long, and
// WebKit stroked every dash of it again on each frame it flowed, under
// 50 fps with two groups open. Its dashes from a screen above to a screen
// below what is in sight are kept, from a multiple of their 8px so they
// stay in step with the whole wire's; the rest of it is a gap, out of
// sight (dashFor). Nothing is laid out for it: where the
// stage and what is in sight were is kept by layout, which reads them
// anyway (sight), and the view's scroll since by its scroll events.
// Reading them here laid the stage out again after each update
const flowing = new Map();
let flowAsked = 0, sight = null, scrolled = 0;
const flowSoon = () => { if (!flowAsked) flowAsked = requestAnimationFrame(() => { flowAsked = 0; flowNear(); }); };
function flowNear() {
const live = new Set([...wires.querySelectorAll("path.live[d]")].filter((p) => !p.classList.contains("rest")));
for (const p of flowing.keys()) if (!live.has(p)) { p.style.removeProperty("stroke-dasharray"); flowing.delete(p); }
if (!live.size || !sight) return;
// in the stage's own terms, a screen more each way, in steps of 200px
// so a scroll redashes a wire only now and then
const at = sight.at - (scrolled - sight.scroll), m = Math.max(sight.hi - sight.lo, 200);
const top = Math.floor((sight.lo - m - at) / 200) * 200, bottom = Math.ceil((sight.hi + m - at) / 200) * 200;
for (const p of live) {
// the same wire with the same in sight is dashed as it was: this is
// asked after every update
const d = p.getAttribute("d"), key = `${d}|${top}|${bottom}`;
if (flowing.get(p)?.key === key) continue;
const dash = dashFor(d, top, bottom);
const was = flowing.get(p)?.dash;
flowing.set(p, { key, dash });
if (was === dash) continue;
if (dash) p.style.setProperty("stroke-dasharray", dash);
else p.style.removeProperty("stroke-dasharray");
}
}
// dashFor is a wire's dashes from top to bottom, measured from its d,
// which is magpie's own M, L, Q and C: WebKit's getPointAtLength took
// milliseconds a wire, and a wire's d changes with most updates. Each
// bend is walked in 16 straight steps, a little shorter than the bend,
// so its ends are moved out by 64px each way, more than that is off
const dashFor = (d, top, bottom) => {
const n = (d.match(/-?[\d.]+(?:e-?\d+)?/g) || []).map(Number), cmd = d.match(/[MLQC]/g) || [];
let x = n[0], y = n[1], k = 2, len = 0, s0 = Infinity, s1 = -Infinity;
const mark = (l) => { s0 = Math.min(s0, l); s1 = Math.max(s1, l); };
if (y >= top && y <= bottom) mark(0);
for (const c of cmd.slice(1)) {
const q = c === "L" ? 2 : c === "Q" ? 4 : 6, v = n.slice(k, k += q), steps = c === "L" ? 1 : 16;
for (let i = 1; i <= steps; i++) {
const t = i / steps, u = 1 - t;
const [px, py] = c === "L" ? [v[0], v[1]]
: c === "Q" ? [u * u * x + 2 * u * t * v[0] + t * t * v[2], u * u * y + 2 * u * t * v[1] + t * t * v[3]]
: [u * u * u * x + 3 * u * u * t * v[0] + 3 * u * t * t * v[2] + t * t * t * v[4], u * u * u * y + 3 * u * u * t * v[1] + 3 * u * t * t * v[3] + t * t * t * v[5]];
// where a step crosses an edge of the band, a straight one
// thousands of pixels long too
const l = Math.hypot(px - x, py - y);
for (const e of [top, bottom]) if (py !== y && (y - e) * (py - e) <= 0) mark(len + l * (e - y) / (py - y));
len += l; x = px; y = py;
if (y >= top && y <= bottom) mark(len);
}
}
if (s0 === Infinity) return `0 ${Math.ceil(len) + 64}`;
const a = Math.max(0, Math.floor(s0 / 8) * 8 - 64), z = Math.ceil(s1 / 8) * 8 + 64;
return a <= 0 && z >= len ? "" : `0 ${a} ${"3 5 ".repeat((z - a) / 8)}0 ${Math.ceil(len) + 64}`;
};
$("#view-routing").addEventListener("scroll", (e) => { scrolled = e.currentTarget.scrollTop; flowSoon(); }, { passive: true });
function layout() {
const r = stage.getBoundingClientRect();
if (!r.width) return;
@@ -1547,12 +1612,17 @@ function routingPage(part) {
// set after this frame: the keys not drawn changing size resize the
// stage, which in its own ResizeObserver WebKit reports as a loop
if (rowH && list.style.getPropertyValue("--rt-row") !== rowH + "px") requestAnimationFrame(() => list.style.setProperty("--rt-row", rowH + "px"));
// read before the wires are set, which lays the stage out again
const view = $("#view-routing"), v = view.getBoundingClientRect();
scrolled = view.scrollTop;
sight = { at: r.top, scroll: scrolled, lo: Math.max(0, v.top), hi: Math.min(innerHeight, v.bottom) };
wires.setAttribute("viewBox", `0 0 ${r.width} ${r.height}`);
sky.setAttribute("viewBox", `0 0 ${r.width} ${r.height}`);
for (const [p, d] of sets) {
if (d === null) { if (p.hasAttribute("d")) p.removeAttribute("d"); }
else if (p.getAttribute("d") !== d) p.setAttribute("d", d);
}
flowSoon();
holdAll();
}
@@ -2191,6 +2261,7 @@ function routingPage(part) {
if (lit) s.wire.style.setProperty("--agent", lit.wire.style.getPropertyValue("--agent"));
}
for (const [id, a] of agents) a.wire.classList.toggle("live", busy.has(id));
flowSoon();
}
// ---------- the log: how one request was routed ----------