Skip to content

perf: build mixed Canvas and SVG layers in linear time - #276

Open
tannerlinsley wants to merge 1 commit into
mainfrom
codex/canvas-layer-composition-275
Open

tannerlinsley wants to merge 1 commit into
mainfrom
codex/canvas-layer-composition-275

Conversation

@tannerlinsley

@tannerlinsley tannerlinsley commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Mixed Canvas/SVG charts can spend quadratic time building their layers. This appends each node once while preserving renderer order and nested group metadata. Thanks @le-marais for the report and reproduction.

Fixes #275.

AI review notes:
The layer builder owns its arrays, so appending a node does not mutate the input scene. The helper now takes one node directly, avoiding both repeated array copies and temporary layer objects.
Independently verified: 137 Canvas/renderer tests, type checking, the explicit bundle policy gate, and the complete packed-package gate including Native/Expo, seven framework adapters and the unified artifact. No dependencies, exported APIs, workflows, budgets or fingerprint locks changed.
A deterministic traversal regression fails on current main with 265,214 node visits for 512 points and passes with this change. Frozen-input, nested-group and renderer-boundary checks also pass.
In a Node 24/jsdom fixture using the public layered prerender entry point with paint methods stubbed, seven-sample medians at 32,000 points were 879.83 ms before and 0.30 ms after. These are composition timings, not browser frames. The baseline fixture completed its samples but exceeded Vitest's five-second test timeout.
All four affected measured Canvas consumers became smaller. Required CI, current-head review and any source fingerprint gate remain to be completed before merge. The issue stays open until delivery is verified.

Summary by CodeRabbit

  • Performance
    • Improved composition of mixed Canvas and SVG layers, avoiding repeated copying of collected nodes.
  • Tests
    • Added coverage for layer ordering, custom-renderer routing, nested groups, and preservation of input data.

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

splitRenderLayers now appends nodes to mutable arrays instead of repeatedly merging node arrays. Tests cover node visits, renderer ordering, and nested groups. Renderer selection and custom-renderer tracking remain unchanged.

Changes: Canvas layer composition

Layer / File(s) Summary
Compose and validate render layers
packages/charts-core/src/canvas.ts, packages/charts-core/src/canvas-layers.test.ts, .changeset/fast-canvas-layer-composition.md
splitRenderLayers appends nodes to matching renderer layers. Tests check node visits, SVG and canvas ordering, and nested group behavior. The changeset declares a patch release for @tanstack/charts.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor · Severity of issue fixed: Medium

Merge Risk: Minimal · up to 47de3

This change makes mixed Canvas and SVG layer composition linear while preserving renderer order and group metadata. No actionable merge-blocking risk was found. Confirm that CI passes before merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title states the main change: mixed Canvas and SVG layer construction is changed from quadratic to linear time, which matches the linked issue #275 and the canvas.ts change that appends nodes to t…
Linked Issues check Passed Directly linked issue #275 asks that splitRenderLayers in packages/charts-core/src/canvas.ts compose layers in linear time without re-copying the node array per node, while preserving renderer groupin…
Out of Scope Changes check Passed The PR touches three files: a patch changeset for @tanstack/charts describing linear-time Canvas/SVG layer composition, packages/charts-core/src/canvas-layers.test.ts (regression and behavior tests fo…
Full details: Title check

Explanation

The title states the main change: mixed Canvas and SVG layer construction is changed from quadratic to linear time, which matches the linked issue #275 and the canvas.ts change that appends nodes to the current layer instead of copying arrays. The 'perf:' prefix is a conventional type label and does not reduce clarity. The title is specific and does not mention unrelated work.

Full details: Linked Issues check

Explanation

Directly linked issue #275 asks that splitRenderLayers in packages/charts-core/src/canvas.ts compose layers in linear time without re-copying the node array per node, while preserving renderer grouping, order, and recursion into groups. The PR summary and the AI-generated change summary state that splitRenderLayers now builds mutable layer node arrays and appends each node to the latest layer when the renderer matches, keeping custom-renderer resolution, group splitting, and the hasCustomRenderer flag. The new canvas-layers.test.ts adds a deterministic bounded-iterator-visit test for 512 consecutive nodes and a focus group, plus SVG ordering, custom-renderer routing, and nested-group preservation tests. Limitation: the diff read for canvas.ts was unavailable and the file view was truncated before splitRenderLayers, so the code-level behavior rests on the summaries and the reported test evidence, not on a direct read of the changed lines. The reported timing data (879.83 ms to 0.30 ms at 32,000 points) is composition evidence supplied by the author, not verified here.

Full details: Out of Scope Changes check

Explanation

The PR touches three files: a patch changeset for @tanstack/charts describing linear-time Canvas/SVG layer composition, packages/charts-core/src/canvas-layers.test.ts (regression and behavior tests for the same function), and packages/charts-core/src/canvas.ts (the splitRenderLayers change). All changes connect to the #275 objective. The summary reports no changes to exported or public declarations, dependencies, workflows, or budgets. Limitation: the canvas.ts diff could not be read directly, so the absence of unrelated edits inside that file rests on the change summary.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
Fix failing CI checks

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3cd763a3-b72e-4237-b15b-3437b46f6ffe
Commits

Reviewing files that changed from the base of the PR and between b236dae and 47de34c.

Files selected for processing: 3

  • .changeset/fast-canvas-layer-composition.md
  • packages/charts-core/src/canvas-layers.test.ts
  • packages/charts-core/src/canvas.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@nx-cloud

nx-cloud Bot commented Nov 20, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 47de34c

Command Status Duration Result
nx run charts-workspace:benchmark-check ❌ Failed 53s View ↗

💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-20 16:18:54 UTC

This branch has not been deployed

No deployments
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.

[Perf] Canvas layer composition is quadratic: splitRenderLayers re-copies the node array per node

1 participant