[Perf] Rolling motion: every frame rebuilds and publishes all presented points, even when nothing reads them #201
Send to my agentA rolling-window chart pays a per-frame cost proportional to its row count for points nothing reads.
- Each rolling
transformplan gets a presentation track whoseapplyre-creates every point of the element, plus the exiting ones, as{ ...point, x, y }keyed bypointIdentity(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
handlePresentationPointsfilters that array withviewportInteractionPointsbefore 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:
- derive presented points lazily from the plan's transform when a subscriber or
getPresentationPointsactually needs them; or - 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).
toomingos(GitHub profile, opens in a new tab) changed the title
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.
Follow-up with a tested fix. Checked against
57774e14a1a86081eddb3a23523724a40da05640(v1.1.0).During a roll, the rolling plan's
applyrebuilds every presented point on every frame (motion.ts:3442-3452).publishthen copies the map into an array (motion.ts:3598), andhandlePresentationPointsrunsviewportInteractionPointson 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):
A lazy version, about 35 lines, behaves the same in our tests:
applyonly stores the latest transform values. Asyncfunction does the per-pointpresented.set.cancelsyncs once first, so an interrupted roll still hands the rightpreviousPresentationto the next render.publishhandssetPresentationPointsa thunk: sync the rolling plans, then[...presented.values()]. Otherwise it passes the array as today.getPresentationPoints()and wherepreviousPresentationis read.handlePresentationPointsbuildsvisiblePointsonly after the pointer branch and thefocusedPointcheck.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
transformwrites on each rolling path ordata-ts-motion-progresson 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.