Skip to content

feat: upload Float32Array data of scattergl traces without a copy - #9

Draft
katosh wants to merge 6 commits into
mainfrom
pr/scattergl-typed
Draft

katosh wants to merge 6 commits into
mainfrom
pr/scattergl-typed

Conversation

@katosh

@katosh katosh commented Dec 5, 2026 •

Copy link
Copy Markdown

Description

A markers-only scattergl trace with Float32Array x and y now goes to the GPU without a copy. With 20 million points and hovermode: false, a recolor takes 111 ms instead of 3,778 ms. With hover on, a recolor takes 3,265 ms instead of 3,844 ms. The positions take 8 bytes of GPU memory for each point instead of 16. The change is not opt-in, and the pixels stay the same.

Diagram of the copies of one point in regl-scatter2d
The copies of one point inside regl-scatter2d, in 3.4.0 and on the pinned branch.

calc and regl-scatter2d copy each point four times and upload 16 bytes for each point. Each restyle or moveTraces repeats the copies and the upload.

Bar charts of time, upload and memory at 20 million points
main against this branch: 20 million points in ten traces, hovermode: false, strict bundles, Apple M3 Max, headless Chromium. The chart names commit a019f22 (source identical to 2441332).

Now calc passes the arrays to regl-scatter2d, which uploads 8 bytes for each point one time. A recolor or a reorder uploads no positions.

AnnZarro draws millions of single cells with plotly.js and needs this. Overview: #11.

Changes

  • src/traces/scattergl/calc.js: pass {x, y, count, rev}, cache the extremes, build ids and tree on demand
  • package.json: pin regl-scatter2d to a fork commit with settylab/regl-scatter2d#1
  • test/jasmine/tests/scattergl_test.js: 11 new specs, 1 updated spec

Testing

  • scattergl: 59 specs pass, and main has 48
  • scattergl_select, gl2d_plot_interact, scatterpolargl and splom pass
  • 61 scenes render the same pixels as main

Notes

  • Behavior change. If you refill a typed array in place, set a new layout.datarevision. A new view of the same buffer range is not new data.
  • On main, a trace of 100,000 points or more gets a hover tree at every calc. Without hover, this branch builds the tree at the first read.
  • The pin is temporary. A regl-scatter2d release replaces the pin.
  • plotly.min.js grows by 5,794 bytes

I can split, reshape or drop any part. Thank you for your time.

Checklist

  • The linked issue has the ready-for-pr label, and it has no assignee or lists me as an assignee
  • I opened the pull request from a branch other than main in my fork
  • I selected "Allow edits from maintainers"
  • I did not edit files in dist/
  • I committed package-lock.json only for a dependency change
  • I added a file to draftlogs/ as draftlogs/README.md describes, or I will add one after the pull request opens

The next commits pass Float32Array data to regl-scatter2d without a copy. The pinned commit holds the changes that regl-scatter2d needs for that, and a built bundle.js. regl-scatter2d builds bundle.js at publish time, so a plain git commit holds no entry file. A regl-scatter2d release replaces this pin.
calc wrote x and y of each trace to a plain Array of doubles and built an Array of point ids. regl-scatter2d then copied the positions and uploaded 16 bytes for each point. Each restyle or moveTraces repeated the copies and the upload.

For a markers-only trace on linear axes with Float32Array data, calc now passes {x, y, count, rev} to regl-scatter2d. regl-scatter2d uploads 8 bytes for each point one time. The same buffer range with the same layout.datarevision is the same data. calc reads the axis extremes from a cache and builds the point ids at the first read.

If you refill a typed array in place, set a new layout.datarevision.
calc built a point-cluster tree for each trace of 100,000 points or more, at every calc. Only hoverPoints reads the tree.

If layout.hovermode is false, or the trace has hoverinfo 'skip', the first read now builds the tree. With hover, calc builds the tree as before.
npm prepares a git dependency that has a build script. It installs the devDependencies of regl-scatter2d, which include the native gl package, and a plain npm ci fails where gl has no binary. The new pinned commit holds the same bundle.js and no build script, so npm ci needs no build.
With virtual-webgl, regl gets a virtual context. That context calls functions that virtual-webgl copied from WebGLRenderingContext.prototype at load, so a later spy on that prototype records nothing, and four upload specs failed in the virtual-webgl job. The specs now spy on the prototype of the context itself. Without virtual-webgl, that prototype is WebGLRenderingContext.prototype as before.
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.

1 participant