Skip to content

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

Open

Description

@le-marais

splitRenderLayers in packages/charts-core/src/canvas.ts builds each layer by re-copying its node array once per node, so composing layers is quadratic in the number of consecutive nodes on one renderer:

const append = (layer: ChartRenderLayer) => {
const previous = layers.at(-1)
if (previous?.renderer === layer.renderer) {
previous.nodes = [...previous.nodes, ...layer.nodes]
} else {
layers.push(layer)
}
}

const append = (layer: ChartRenderLayer) => {
  const previous = layers.at(-1)
  if (previous?.renderer === layer.renderer) {
    previous.nodes = [...previous.nodes, ...layer.nodes]
  } else {
    layers.push(layer)
  }
}

It runs whenever a chart mixes renderers, for example one mark with renderer: canvasChartRenderer on the default SVG renderer. The function also recurses into every group to check for a custom renderer, including the default focus layer groups, which hold one node per point. So a canvas dot mark with n points costs about n²/2 element copies per render, on the very marks that were moved to canvas to make them cheap.

Reproduction

@tanstack/charts 1.5.0, Node 24 (vitest, jsdom). Median of 5 calls of the layered renderer's prerender:

import { createChartScene, defineChart, dot } from '@tanstack/charts'
import { canvasChartRenderer, createLayeredChartRenderer } from '@tanstack/charts/canvas'
import { scaleLinear } from '@tanstack/charts/scales/linear'
import { createSvgChartRenderer } from '@tanstack/charts/svg/renderer'

const layered = createLayeredChartRenderer(createSvgChartRenderer())
for (const n of [2000, 4000, 8000, 16000, 32000]) {
  const data = Array.from({ length: n }, (_, i) => ({ x: i, y: (i * 7919) % 1000 }))
  const chart = defineChart({
    marks: [dot(data, { x: (d) => d.x, y: (d) => d.y, renderer: canvasChartRenderer })],
    scales: { x: { scale: scaleLinear().domain([0, n]) }, y: { scale: scaleLinear().domain([0, 1000]) } },
  })
  const scene = createChartScene(chart, { width: 800, height: 400 })
  const t0 = performance.now()
  layered.prerender(scene, { ariaLabel: 'repro' })
  console.log(n, performance.now() - t0)
}
points now with the fix below
2,000 2.5 ms 0.4 ms
4,000 8.2 ms 0.2 ms
8,000 23.6 ms 0.3 ms
16,000 206 ms 0.4 ms
32,000 1,390 ms 1.0 ms

In a real app (a Vue Chart, a scatter chart of about 7,000 points with its points on canvas), wheel zoom spent about 300 ms per render in this function: about 51 million element copies per zoom step. That made the canvas version roughly ten times slower than plain SVG. With the fix, frames are about three times shorter than with SVG.

Suggested fix

Append in place. Every array append receives is a fresh literal built in this function, so mutating it is safe:

type BuildingLayer = Omit<ChartRenderLayer, 'nodes'> & { nodes: SceneNode[] }

const layers: BuildingLayer[] = []
const append = (layer: BuildingLayer) => {
  const previous = layers.at(-1)
  if (previous?.renderer === layer.renderer) {
    for (const node of layer.nodes) previous.nodes.push(node)
  } else {
    layers.push(layer)
  }
}

(A loop rather than push(...layer.nodes), which can exceed the argument limit for very large layers.)


Found and measured with an AI coding agent (Claude Code); the numbers above are from real runs.

Activity

  1. changed the title [-]Canvas layer composition is quadratic: splitRenderLayers re-copies the node array per node[/-] [+][Bug] Canvas layer composition is quadratic: splitRenderLayers re-copies the node array per node[/+] on Nov 20, 2026
  2. changed the title [-][Bug] Canvas layer composition is quadratic: splitRenderLayers re-copies the node array per node[/-] [+][Perf] Canvas layer composition is quadratic: splitRenderLayers re-copies the node array per node[/+] on Nov 20, 2026
  3. tannerlinsley commented on Nov 20, 2026

    @tannerlinsley
    Member

    Thanks for the clear report and reproduction! There's a fix in #276 that builds each layer without repeatedly copying its nodes. This stays open while CI and delivery are still pending.

    AI review notes:
    Independently confirmed the repeated-copy path on current main. The new deterministic regression fails there with 265,214 node visits for 512 points and passes with the fix.
    The candidate passes 137 renderer tests, types, the explicit bundle gate and the full packed-package gate. Frozen scene inputs, nested group metadata and renderer ordering are covered. All four affected measured Canvas consumers get smaller.
    The fix is submitted, not released. The reported browser zoom scenario has not been independently reproduced; local performance measurements isolate composition in Node/jsdom with painting stubbed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions