Skip to content

fix(filler): avoid quadratic boundary lookups with many line segments - #12381

Open
b84330808 wants to merge 1 commit into
chartjs:masterfrom
b84330808:fix-fill-segment-boundary-lookups
Open

b84330808 wants to merge 1 commit into
chartjs:masterfrom
b84330808:fix-fill-segment-boundary-lookups

Conversation

@b84330808

Copy link
Copy Markdown

Fixes #12380

The boundary line gets two points per line segment, and each fill segment searches all of them three times per draw (once in _segments, twice through interpolate). On the issue's sample (10,430 points, 1,223 segments) that is about 9 million point visits per draw.

The boundary is straight, so a line that only moves forward along it needs just the two ends. Lines that turn back and reversed scales keep their existing points and speed. fill: '-1' and fill: 'stack' follow real lines and aren't sped up here.

Using the data and options from the issue's pen:

Per draw before after
_boundSegment point visits 8,986,026 18,990
chart.draw() JS, ms 82.4 [78.9–83.4] 4.4 [4.3–4.5]
draw + raster, ms 89.7 [86.0–90.5] 11.6 [11.5–11.9]
mousemove with tooltip, ms 94.6 [90.5–97.8] 12.6 [12.4–13.3]
mousemove, fill only, ms 5.3 [5.3–5.4] 5.3 [5.3–5.5]
mousemove, segment only, ms 5.5 [5.4–5.8] 5.5 [5.4–5.6]

Median [min–max] of 7 alternating fresh processes, headless Chrome 157, software canvas, i7-9700, Windows 11. Not measured in Firefox, which is what the issue used.

Canvas pixels hash the same before and after for 39 chart setups at device pixel ratio 1 and 2: origin, start, end and value fills, above/below, gaps, stepped, tension, stacked, indexAxis: 'y', reversed axes, unsorted x, decimation and radar.

The new spec reads the boundary's point count through a spy on interpolate, which is an implementation detail, so drop it if you'd rather not have that.

A boundary line kept two points for every segment of the filled line, and
every fill segment searched all of them three times per draw. With the
`segment` option or null gaps that makes each draw quadratic in the number
of segments.

The boundary is straight, so a line that only moves forward along it needs
just the two ends. Lines that turn back and reversed scales keep the
existing points.
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.

Fill + segment kills performance

1 participant