[Perf] Canvas layer composition is quadratic: splitRenderLayers re-copies the node array per node
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- typescript
- Domain
- data-visualization, performance
Research direction
Read packages/charts-core/src/canvas.ts around splitRenderLayers (lines 639–646) and inspect how it groups consecutive nodes by renderer. The issue provides a reproduction using a canvas dot mark and benchmark results; use that to check the cost before and after the change. Done means layer composition avoids repeated array copying and the reproduction no longer scales quadratically.
Written by the indexing model from the issue text.
Description
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)
}
}
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.
- Dominant language
- TypeScript
- Stars
- 870
- Forks
- 57
- Avg merge
- 11h 58m
- Merged PRs (30d)
- 18
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from TanStack/charts
-
[Bug] Focus guides: `url(#id)` paints ignore `idPrefix`Possibly taken @getdownandcode claimed this 37 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
TanStack/charts#211 · 1 comment ·
Maintainers usually reply within 1 day
-
Add reverse options to waffleY and waffleXPossibly taken @tannerlinsley claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 25/100
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
Maintainers usually reply within 1 day
Similar issues
-
inference/go type/feature
Difficulty 1/5 1-3 hours Newbie friendliness 72/100
anomalyco/opencode#65148 · 1 comment ·
Maintainers usually reply within 1 day
-
truapi-release
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
paritytech/product-sdk#529 ·
Maintainers usually reply within 1 day
-
Add: Mansudae TVOpenapproved check:passed streams:add
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
devlikeapro/waha#2369 ·
Maintainers usually reply within 5 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
bigbluebutton/bigbluebutton#26638 · 1 comment ·
Maintainers usually reply within 1 day