Skip to content
Source
Sign inSign up
TanStack/charts

[Perf] Rolling motion: every frame rebuilds and publishes all presented points, even when nothing reads them #201

Send to my agent
Opentoomingos(GitHub profile, opens in a new tab) opened this issue 2 comments

A rolling-window chart pays a per-frame cost proportional to its row count for points nothing reads.

  • Each rolling transform plan gets a presentation track whose apply re-creates every point of the element, plus the exiting ones, as { ...point, x, y } keyed by pointIdentity(point) (motion.ts:3442-3453).
  • Every tick then calls lifecycle.publish (motion.ts:3999), which copies the whole map into a new array (motion.ts:3598).
  • The renderer's handlePresentationPoints filters that array with viewportInteractionPoints before it checks for a pointer or a focused point, so with neither the result is discarded (renderer.ts:1223-1246).

Use case: a live line that streams back to back 600 ms rolls of about 220 smoothed rows per mark (area, line and tail). With per-track timers, the points track is 77% of each frame's sampling (0.13 ms per call). In a production build the motion tick is about 0.23 ms per frame, about 131 ms of requestAnimationFrame time per 6 s of streaming, with no pointer over the chart. The DOM writes the roll needs are a small part of that.

Repro

Mount any line chart with a rolling window and keep appending data with no pointer and no focus. Profile one roll: apply runs on every frame for every row, and handlePresentationPoints runs viewportInteractionPoints every frame and then returns.

Proposal

Either:

  1. derive presented points lazily from the plan's transform when a subscriber or getPresentationPoints actually needs them; or
  2. skip the per-frame rebuild, publish and filter while there is no pointer, focus or tooltip, and publish once when one appears.

Related: #195 (rolling windows). Checked against 57774e14a1a86081eddb3a23523724a40da05640 (v1.1.0).

  1. Follow-up with a tested fix. Checked against 57774e14a1a86081eddb3a23523724a40da05640 (v1.1.0).

    During a roll, the rolling plan's apply rebuilds every presented point on every frame (motion.ts:3442-3452). publish then copies the map into an array (motion.ts:3598), and handlePresentationPoints runs viewportInteractionPoints on it before it checks for a pointer or focus (renderer.ts:1223-1245). While the chart is idle, nothing reads those points.

    Measured in Chrome on a streaming line (about 220 rows per mark, area, line and tail rolling back to back with 600 ms tweens):

    • The points track is 77% of the frame sampling cost, about 0.13 ms per call.
    • The engine tick as a whole is 0.17 ms per frame, 1.8% of the main thread at rest.

    A lazy version, about 35 lines, behaves the same in our tests:

    1. The rolling plan's apply only stores the latest transform values. A sync function does the per-point presented.set. cancel syncs once first, so an interrupted roll still hands the right previousPresentation to the next render.
    2. When a rolling plan exists, publish hands setPresentationPoints a thunk: sync the rolling plans, then [...presented.values()]. Otherwise it passes the array as today.
    3. The surface resolves the thunk in getPresentationPoints() and where previousPresentation is read. handlePresentationPoints builds visiblePoints only after the pointer branch and the focusedPoint check.

    Hover, focus and the next render see the same points as before. Idle frames allocate nothing.

    One caveat: this removes the engine's per-frame JS, but not the per-frame transform writes on each rolling path or data-ts-motion-progress on the root. On our chart, style and paint from those writes plus CSS animations are most of the idle cost. So the patch is a floor, not the whole fix. We have not benchmarked it end to end on a quiet machine yet. Happy to open a PR if this direction is welcome.

  2. Thanks @toomingos. The unused viewport-filter work is now reduced in 1.4.0 through #256. Idle focus skips that filtering, pointer focus uses its existing lookup, and keyboard focus still gets the visible points it needs.

    AI review notes: The downloaded 1.4.0 core artifact matches npm's SHA-512 integrity and contains the idle guard before viewport filtering. The rolling motion engine still rebuilds and publishes presentation points, so this issue stays open for that remaining work.

Sign in to join this conversation.