Repository navigation
Conversation
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
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
drawTrack.addreplays every path op a canvas records soIsPointInPath,IsPointInStroke,ArcTo,EllipseArc, text alignment and the path queries can read the current geometry. ForDrawArc,DrawEllipseandDrawRoundRectit expanded the shape withappendEllipseArc(nil, …)/appendRoundRect(nil, …), a fresh slice per op, and iterated it withfor _, o := range …passing&ointo the recursiveadd, 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.addat 85% cumulative in the allocation profile, and a worst frame of 33.8 ms with a 7.3 ms GC pause.drawTrackkeeps anexpanded []DrawOpbuffer, refilled withexpanded[:0]for each shape op, andaddAlladds 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 survivesResetwith the rest of the track.segWriter(Path.Segments,Length,PointAt,Trim), which had the same pattern, gets the same change.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/DrawAtlasallocation-free.Targets
core,base,ui,tsx, thensgotool)Screenshots
None
Benchmark
BenchmarkTrackedShapesReused: a reusedDrawingrecording 1,002 arcs, ellipses and round rects (one path each, filled or stroked) and tracking them, arm64 Mac, 10 cores,-count 5:Testing
TestTrackedShapesZeroAllocInReusedDrawing(testing.AllocsPerRunover 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, andIsPointInPath/IsPointInStrokeagree on a grid. Passes on main and on this branch.TestPathShapeSegmentsMatchTheirExpansion: the same comparison forPath.Segmentsand forpathSegsunder a transform.Verification
3ce88d38a10710d3go tool nsgo check→ 0go tool nsgo test ./internal/driver/ ./tools/changelog/→ 0go tool nsgo test ./internal/driver/ -- -run 'TestTrackedShapes|TestPathShapeSegments' -bench TrackedShapesReused -benchmem -count 5→ 0passother 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
fix(linux): keep the caret after a paste; a squash merge uses it as the commit subject.changelog.d/fragment, not inCHANGELOG.md.