Skip to content

faster rounded path strings - #42

Open
hood wants to merge 1 commit into
d3:mainfrom
hood:perf/path-string
Open

hood wants to merge 1 commit into
d3:mainfrom
hood:perf/path-string

Conversation

@hood

@hood hood commented Nov 21, 2026

Copy link
Copy Markdown

I was profiling an SVG line chart with a few thousand points per series (node --cpu-prof on d3.line), and most of the time went to appendRound in 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) / k and 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 through arguments.

With digits <= 4 (default is 3) the rounded value is just the integer n = Math.round(x * k), so this formats n / k as the integer part plus a fraction suffix from a small lookup table (k entries, built once per digits). That's exactly the string String(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:

main this PR
bezierCurveTo, 1k points 0.46 ms 0.26 ms
bezierCurveTo, 10k points 4.75 ms 2.80 ms
lineTo, 50k points 8.13 ms 4.92 ms

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.js in the repo root and run node bench.js on main and on this branch.

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);

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 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("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);

@Fil

Fil commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

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?

@Fil Fil closed this Nov 22, 2026
@Fil

Fil commented Nov 22, 2026

Copy link
Copy Markdown
Member

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.

@Fil Fil reopened this Nov 22, 2026
@hood

hood commented Nov 23, 2026

Copy link
Copy Markdown
Author

Fair point, thanks for looking. I tried the buffer idea before replying, two ways: a parts array joined once in toString, and a single Uint8Array where the rounded digits are written as bytes and decoded once at the end (so no double-to-string at all). Neither helps at chart sizes tbh. Engines already keep += as a rope and flatten it once, so a hand-rolled buffer mostly adds overhead, and it only starts paying off around 200k points. On small paths like pie slices it's clearly slower, since every path pays for its own buffer and decode.

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 appendRound), that's 1.3-1.4x but a tiny bit slower on pie slices. Same ranking in Chrome, Firefox and Safari when I ran it through Playwright.

Numbers

M1 Pro, node 26, main b9ab345, pathRound(3), median of 5 runs of the harness below, speedup vs main:

main template only rounding only this PR join buffer byte buffer
100 pie slices 0.068 ms 1.19x 0.96x 1.15x 0.76x 0.63x
bezierCurveTo, 10k points 4.42 ms 1.15x 1.32x 1.55x 0.86x 0.98x
lineTo, 50k points 7.54 ms 1.18x 1.43x 1.55x 0.89x 1.09x
lineTo, 200k points 44.9 ms 1.23x 1.09x 1.31x 1.13x 1.56x
bench harness

Save as bench.js in the repo root and run node bench.js on main and on each branch.

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants