Repository navigation
Conversation
|
Thanks for sharing. It seems to me though that this is optimizing the wrong thing, and not sure it's worth the added complexity. The most wasteful thing in our SVG path generator is the build-up of string after string; I believe we could be faster by filling in a single buffer and converting it to a string at the last moment. But again, is it worth the added complexity? |
|
I will leave the PR open so we can continue exploring alternatives. The PR does two changes and it's not clear how much each brings. |
|
Fair point, thanks for looking. I tried the buffer idea before replying, two ways: a parts array joined once in For the split, I put each change on its own branch in my fork so it's easy to check: template-only, rounding-only, plus the two buffers: join-buffer, byte-buffer. All produce the same strings as main. So the rounding table is most of the win on its own, and the two changes stack. If you'd rather keep it smaller I can drop the template part and keep only the rounding table (rounding-only, the change is all inside NumbersM1 Pro, node 26, main
bench harnessSave as import {pathRound} from "./src/index.js";
let seed = 1, y = 250;
const random = () => (seed = seed * 16807 % 2147483647) / 2147483647;
const walk = n => Array.from({length: n}, (_, i) => [i * 960 / n, y += random() * 4 - 2]);
const data1k = walk(1e3), data10k = walk(1e4), data50k = walk(5e4), data200k = walk(2e5);
function lines(points) {
const path = pathRound(3);
path.moveTo(...points[0]);
for (let i = 1; i < points.length; ++i) path.lineTo(...points[i]);
return path + "";
}
function curves(points) {
const path = pathRound(3);
path.moveTo(...points[0]);
for (let i = 1; i < points.length; ++i) {
const [x0, y0] = points[i - 1], [x1, y1] = points[i], dx = (x1 - x0) / 3;
path.bezierCurveTo(x0 + dx, y0 + random() - 0.5, x1 - dx, y1 + random() - 0.5, x1, y1);
}
return path + "";
}
function slices(count) {
let total = 0;
for (let i = 0; i < count; ++i) {
const a0 = i / count * 2 * Math.PI, a1 = (i + 1) / count * 2 * Math.PI;
const path = pathRound(3);
path.arc(0, 0, 200, a0 - Math.PI / 2, a1 - Math.PI / 2);
path.arc(0, 0, 50, a1 - Math.PI / 2, a0 - Math.PI / 2, true);
path.closePath();
total += (path + "").length;
}
return total;
}
function bench(name, f, n) {
const run = () => { for (let i = 0; i < n; ++i) f().length; };
for (let i = 0; i < 3; ++i) run();
const times = [];
for (let i = 0; i < 15; ++i) {
const t0 = performance.now();
run();
times.push((performance.now() - t0) / n);
}
times.sort((a, b) => a - b);
console.log(`${name}\t${times[7].toFixed(3)} ms`);
}
bench("100 pie slices", () => [slices(100)], 100);
bench("bezierCurveTo, 1k points", () => curves(data1k), 100);
bench("bezierCurveTo, 10k points", () => curves(data10k), 10);
bench("lineTo, 10k points", () => lines(data10k), 10);
bench("lineTo, 50k points", () => lines(data50k), 4);
bench("lineTo, 200k points", () => lines(data200k), 2); |
I was profiling an SVG line chart with a few thousand points per series (
node --cpu-profond3.line), and most of the time went toappendRoundin d3-path, so building the string, not the curve math. The same curve drawn to a canvas context is like 25x cheaper.Every number goes through
Math.round(x * k) / kand then double-to-string, which is the expensive part since it's the shortest round-trip formatting. On top of that the tagged template append does a+=per number througharguments.With digits <= 4 (default is 3) the rounded value is just the integer
n = Math.round(x * k), so this formatsn / kas the integer part plus a fraction suffix from a small lookup table (k entries, built once per digits). That's exactly the stringString(Math.round(x * k) / k)gives: while |n| < 2^52 the decimal n/10^d is the only d-digit decimal in that double's rounding interval, and that range never hits exponent notation. Everything else (|n| >= 2^52, NaN, Infinity, digits > 4) falls back to the old expression. The methods now use plain template literals instead of the tagged append.Output is byte-identical to main on ~20k random paths and 5M numbers (digits null to 20, -0, NaN, ±Infinity, 2^52, 1e21), and the d3-path, d3-shape and d3-chord tests pass against this branch.
Numbers
M1 Pro, node 26, main
b9ab345,pathRound(3), median of 5 runs of the harness below:Through d3-shape's line and area the gain is a bit smaller since the curve math is in there too, and a bit bigger in Firefox.
harness
Save as
bench.jsin the repo root and runnode bench.json main and on this branch.