Skip to content

perf(canvas): track arcs, ellipses and round rects without allocating - #2846

Open
houko wants to merge 1 commit into
mainfrom
perf/drawtrack-allocs
Open

houko wants to merge 1 commit into
mainfrom
perf/drawtrack-allocs

Conversation

@houko

@houko houko commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

drawTrack.add replays every path op a canvas records so IsPointInPath, IsPointInStroke, ArcTo, EllipseArc, text alignment and the path queries can read the current geometry. For DrawArc, DrawEllipse and DrawRoundRect it expanded the shape with appendEllipseArc(nil, …) / appendRoundRect(nil, …), a fresh slice per op, and iterated it with for _, o := range … passing &o into the recursive add, so every expanded sub-op escaped to the heap as well. A game redrawing many circles and rounded rectangles each frame measured about 3,500 allocations per frame, about 11 GCs per second, drawTrack.add at 85% cumulative in the allocation profile, and a worst frame of 33.8 ms with a 7.3 ms GC pause.

  • drawTrack keeps an expanded []DrawOp buffer, refilled with expanded[:0] for each shape op, and addAll adds the ops by index. The expanded ops are only moves, lines, cubics and closes, which never expand again, so the buffer is not re-entered while it is being added. Its size is bounded by one shape's expansion (at most 5 ops for an arc or ellipse, 14 for a round rect) and it survives Reset with the rest of the track.
  • segWriter (Path.Segments, Length, PointAt, Trim), which had the same pattern, gets the same change.
  • The rest of the file already iterates by index (pathSegs) or does not expand. The query functions (flatten, onStroke, pathSegs) still allocate per call, as they did; they run per hit test, not per recorded op.

Geometry is unchanged: the new tests compare each shape op with the plain ops it expands to.

Fixes #2838. Related: #2269 made DrawImage / DrawAtlas allocation-free.

Targets

  • Shared code (core, base, ui, tsx, the nsgo tool)
  • macOS
  • iOS
  • tvOS
  • watchOS
  • Android
  • Windows
  • Linux
  • Web
  • Mini programs
  • Docs or website only

Screenshots

None

Benchmark

BenchmarkTrackedShapesReused: a reused Drawing recording 1,002 arcs, ellipses and round rects (one path each, filled or stroked) and tracking them, arm64 Mac, 10 cores, -count 5:

ns/op B/op allocs/op
before (main a10710d) 1,698,424 – 1,972,746 6,541,092 10,688
after 295,078 – 383,697 0 0

Testing

  • TestTrackedShapesZeroAllocInReusedDrawing (testing.AllocsPerRun over the frame above, after one warm-up frame): written first, failed on main with 10,688 allocations per frame, passes with 0.
  • TestTrackedShapesMatchTheirExpansion: for arcs (clockwise, counter-clockwise, full turn), an ellipse and round rects (plain and with a negative width and clamped radii), with and without a current point and under a translate/rotate/scale, the tracked segments, current point, subpath start and state equal those of the expanded plain ops, and IsPointInPath / IsPointInStroke agree on a grid. Passes on main and on this branch.
  • TestPathShapeSegmentsMatchTheirExpansion: the same comparison for Path.Segments and for pathSegs under a transform.

Verification

  • head: 3ce88d38
  • base: a10710d3
  • commands:
    • go tool nsgo check → 0
    • go tool nsgo test ./internal/driver/ ./tools/changelog/ → 0
    • go tool nsgo test ./internal/driver/ -- -run 'TestTrackedShapes|TestPathShapeSegments' -bench TrackedShapesReused -benchmem -count 5 → 0
  • result: pass
  • not covered: other targets' vetting and tests, the emulator, Windows, Linux and browser jobs; the full repository test suite was not run locally (the change is confined to internal/driver)

Checklist

  • The title is a Conventional Commit, such as fix(linux): keep the caret after a paste; a squash merge uses it as the commit subject.
  • Docs whose facts change are updated, and a capability users can see has a line in a changelog.d/ fragment, not in CHANGELOG.md.
  • New third-party code, assets and dependencies name their source and license.

drawTrack.add and segWriter.add expanded each Arc, Ellipse and RoundRect into a fresh slice and took the address of the range variable, so every expanded op escaped to the heap. Expand into a buffer the track keeps and add the ops by index instead.

Fixes #2838

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core documentation Improvements or additions to documentation local-ci-pass The head commit declares that its local checks passed size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Canvas: replaying Arc, Ellipse and RoundRect in drawTrack.add allocates on every op

1 participant