[charts] Make ordinal scales O(1) to copy and re-range#22691
Merged
Conversation
Deploy previewBundle size
PerformanceTotal duration: 2,218.95 ms +353.66 ms(+19.0%) | Renders: 67 (+0) | Paint: 3,192.27 ms +518.52 ms(+19.4%)
…and 8 more (+13 within noise) — details Check out the code infra dashboard for more information about this PR. |
Band/point axes over large category domains were rebuilt on every pan/zoom frame. Two costs dominated: - `scaleBand.copy()` rebuilt the domain InternMap and materialized the full position array (`O(domain)`), and `selectorChartXScales` copies the scale every frame to apply the zoomed range. For a 1M-category axis this was ~255ms/frame (~4fps); linear-axis charts were unaffected, so only bar charts (and other categorical axes) were slow. - `getNormalizedAxisScale` rebuilt the band scale on every render because the filtered-domains record is recreated per zoom, re-interning the domain. Changes: - Rewrite `scaleBand` so positions are computed arithmetically (no materialized range array) and `copy()` shares the immutable domain index. copy+re-range drops from ~255ms to ~0.004ms per frame for 1M categories, while still returning a fresh scale identity (memoized consumers such as the grid keep updating on zoom). - Cache the normalized ordinal scale, keyed by domain reference and scale variant (type + padding). The variant key matters because one `data` array can back several axes (e.g. a `band` and a `point` axis sharing it), which must not share a scale instance. General improvement for any chart with a band/point axis; no API change.
JCQuintas
force-pushed
the
perf/ordinal-scale-copy
branch
from
June 5, 2026 09:43
0c076ca to
6695b47
Compare
alexfauquette
approved these changes
Jun 5, 2026
Comment on lines
67
to
83
| const rescale = () => { | ||
| const n = domain.length; | ||
| const reverse = r1 < r0; | ||
| const start = reverse ? r1 : r0; | ||
| const stop = reverse ? r0 : r1; | ||
| step = (stop - start) / Math.max(1, n - paddingInner + paddingOuter * 2); | ||
| reverse = r1 < r0; | ||
| const lo = reverse ? r1 : r0; | ||
| const hi = reverse ? r0 : r1; | ||
| step = (hi - lo) / Math.max(1, n - paddingInner + paddingOuter * 2); | ||
| if (isRound) { | ||
| step = Math.floor(step); | ||
| } | ||
| const adjustedStart = start + (stop - start - step * (n - paddingInner)) * align; | ||
| let adjustedStart = lo + (hi - lo - step * (n - paddingInner)) * align; | ||
| bandwidth = step * (1 - paddingInner); | ||
| const finalStart = isRound ? Math.round(adjustedStart) : adjustedStart; | ||
| const finalBandwidth = isRound ? Math.round(bandwidth) : bandwidth; | ||
| bandwidth = finalBandwidth; | ||
| const values = sequence(n).map((i) => finalStart + step * i); | ||
| ordinalRange = reverse ? values.reverse() : values; | ||
| if (isRound) { | ||
| adjustedStart = Math.round(adjustedStart); | ||
| bandwidth = Math.round(bandwidth); | ||
| } | ||
| start = adjustedStart; | ||
| return scale; |
Member
There was a problem hiding this comment.
This rewriting is a bit suspect, but I did not catch any difference
Member
Author
There was a problem hiding this comment.
AI did it to remove sequence(n).map and values.reverse() from this calculation.
It moves reverse into scale fn above, making it kind of "lazy" instead of eager in the sense that it doesn't recompute unnecessarily.
mbrookes
pushed a commit
to mbrookes/mui-x
that referenced
this pull request
Jun 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Charts with a band/point axis over a large category domain were slow to pan and zoom. Every pan/zoom frame the axis scale is copied to apply the zoomed range, and for an ordinal scale that copy was
O(domain). With ~1M categories this was ~255ms/frame (~4fps). Charts on linear axes were unaffected, so this mostly hit bar charts (and any categorical line/scatter).Two costs dominated:
scaleBand.copy()rebuilt the domainInternMapand materialized the full position array on everyrescale(), andselectorChartXScalescopies the scale every frame.getNormalizedAxisScalerebuilt the band scale on every render because the filtered-domains record is recreated per zoom, re-interning the domain.Changes
scaleBandso band positions are computed arithmetically inscale()(no materialized range array), andcopy()shares the immutable domain index instead of rebuilding it.copy()+ re-range drops from ~255ms to ~0.004ms per frame for a 1M-category domain, while still returning a fresh scale identity (so memoized consumers such as the grid keep updating on zoom).scalePointis built onscaleBand, so it benefits too.No public API change. Behavior is unchanged (existing
scaleBand/scalePoint, zoom, grid, and bar tests pass); only the cost of building/copying ordinal scales changes.