Skip to content

Speed up exact integral grid row measurements - #2893

Merged
alixander merged 1 commit into
masterfrom
codex/grid-integral-measurements
Sep 10, 2026
Merged

alixander merged 1 commit into
masterfrom
codex/grid-integral-measurements

Conversation

@alixander

Copy link
Copy Markdown
Collaborator

Human


AI

Large dynamic grids repeatedly rescan row sizes when the bounded measurement cache misses. A native Studio compile profile for 3,001 grid objects spent 87% of sampled CPU time in those row sums.

Use lazy prefix sums when all sizes and the gap are nonnegative integers and their complete total, including the trailing gap used by the existing calculation, fits within float64's exact integer range of 2^53. Every intermediate remains exact, so interval subtraction and gap addition preserve the existing results. Fractional, negative, nonfinite, or oversized inputs retain the existing bounded cache and addition order. Empty and singleton intervals still avoid allocating a cache. Division traversal, search limits, and tie-breaking are unchanged.

Validation:

  • go test ./d2layouts/d2grid -count=1
  • go test ./d2layouts ./d2lib -count=1
  • New randomized tests compare interval results bit-for-bit and grid partitions against the existing reference implementation, including exact-range boundaries and fallback cases.
  • An additional local Studio comparison of v0.9.0 against v0.9.0 plus this patch produced byte-identical D2 layout JSON, SVG, and Studio board JSON for all 14 fixtures: 42 matching artifacts. Cases include variable dimensions, all four directions, rows/columns, asymmetric and zero gaps, nested grids, labels, Markdown/tables/classes, source configuration, Dagre/ELK, and 1,000/3,000-node grids.
  • In one native Apple M4/Go 1.27 profile pair, the 3,001-object Studio compile fell from 6,834 ms to 215.5 ms; JSON serialization remained approximately 13 ms and returned the same 2,288,627 bytes. These are native measurements for this workload, not browser timings or a general speed claim.

@alixander
alixander marked this pull request as ready for review September 10, 2026 15:42
@alixander
alixander merged commit 6d0da42 into master Sep 10, 2026
2 checks passed
@alixander
alixander deleted the codex/grid-integral-measurements branch September 10, 2026 16:13
pancsta pushed a commit to pancsta/d2 that referenced this pull request Nov 7, 2026
…ements

Speed up exact integral grid row measurements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant