Skip to content

perf(composition): Cache the rounded-clip paths (backport #24230) - #24698

Closed
mergify[bot] wants to merge 4 commits into
servicing/6.8from
mergify/bp/servicing/6.8/pr-24230
Closed

mergify[bot] wants to merge 4 commits into
servicing/6.8from
mergify/bp/servicing/6.8/pr-24230

Conversation

@mergify

@mergify mergify Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #24221

PR Type:

🔄 Refactoring (no functional changes, no api changes)

What changed? 🚀

Two places handed out a brand-new native SKPath on every frame for every rounded visual, because SKPathBuilder.Detach() transfers ownership into a new path:

  1. RectangleClip.GetClipPath — Visual.Render asks for the post-painting clip several times per visual per frame. The path depends only on the clip's own properties (GetBoundsCore ignores the visual, GetBounds applies the clip's own TransformMatrix), so it is now built once and dropped by OnPropertyChangedCore, making the hot path a single null check. Every mutation reaches that hook: the setters go through SetProperty, the CreateRectangleClip overloads use object initialisers, and RectangleClip does not override SetAnimatableProperty, so animations never touch these fields.
  2. BorderVisual.GetPrePaintingClipping — allocated both an SKPathBuilder and an SKPath per rounded border per frame. Now cached and keyed on the SKRoundRect instance it was built from; CreateBorderPath assigns a brand-new instance whenever the geometry changes, so reference identity is an exact key. The superseded path is disposed, keeping the deterministic native cleanup the old using provided, and the path never escapes the call.

RectangleClip was the odd one out among the clips — InsetClip already caches its path and CompositionGeometricClip reuses a static spare.

Measured impact 📊

Workload Metric Before After Δ
render.full-paint alloc / frame 519,107 B 119,747 B −76.9 %
render.full-paint alloc / element / frame 332.5 B 76.7 B −76.9 %
render.full-paint Gen0 collections / 40 frames 1 0 −100 %
render.full-paint time / frame — — unchanged (noise)

780 rounded Borders + 625 rounded Rectangles (1,561 elements), painted through
RenderTargetBitmap with no layout invalidation, so this is paint cost only. 5 runs each,
median; allocation figures vary by <11 B across runs.

These numbers are for this PR alone, measured by building this branch and the base and running
the same workload on each — not carried over from the combined sequence. (Two related fixes measured
in sequence during development reached −98 % on the same workload, but that figure includes the
separate ShapeVisual LINQ change, which is its own PR.)

Honest scope note: frame time is unchanged — the paint walk is bound by native rasterisation,
which also confirms the same rendering work still happens. This is a GC-pressure win: at 60 fps it
is ~24 MB/s of managed garbage a rounded-border UI no longer produces, plus roughly 1,560 fewer
finaliser-tracked native Skia objects per frame.

Validation ✅

Runtime tests, Skia Desktop, Given_Border + Given_ContainerVisual + Given_ShapeVisual + Given_Visual_Damage + Given_UIElement + Windows_UI_Xaml_Shapes.*: 671 run, 654 passed, 1 skipped, 16 failed — before and after, compared as sets of failing test names against a baseline build: 0 new failures, 0 newly fixed. The 16 include the clip tests that would catch exactly this kind of bug (When_Both_Layouting_Clip_And_Clip_DP, When_TranslateTransform_And_Clip), and they fail identically on the baseline.

PR Checklist ✅

  • 🧪 Added Runtime tests, UI tests, or a manual test sample (for bug fixes / features, if applicable)
  • 📚 Docs have been added/updated following the documentation template (for bug fixes / features) — n/a, no API or behaviour change for app authors
  • 🖼️ Validated PR Screenshots Compare Test Run results. — to check once CI has run
  • ❗ Contains NO breaking changes
  • 👀 Reviewed 2 other open pull requests (optional but appreciated!)

This is an automatic backport of pull request #24230 done by [Mergify](https://mergify.com).

RectangleClip.GetClipPath built a brand new geometry on every call.
Visual.Render asks for the post-painting clip several times per visual per
frame, so every rounded Border produced fresh managed wrappers and native
paths on every frame. InsetClip already caches its geometry.

The path depends only on the clip's own properties -- GetBoundsCore
ignores the visual and GetBounds applies the clip's own TransformMatrix --
so build it once, hand each caller its own reference, and release it from
OnPropertyChangedCore. Every mutation reaches that hook: the setters go
through SetProperty, the CreateRectangleClip overloads use object
initializers, and RectangleClip does not override SetAnimatableProperty, so animations never touch these
fields. The hot path is now a single null check.

Measured on a 1561-element rounded tree (specs/059-performance-improvements):
paint allocations drop from 519 KB to 382 KB per frame (-26%) and Gen0
collections over a 40-frame window from 1 to 0. Wall-clock is unchanged --
this is a GC-pressure win, not a throughput one. Rendering and clipping
runtime tests: identical pass/fail sets before and after, 0 new failures.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_015DSdL5YzLhjx8CeGXZf1Au
(cherry picked from commit 2f505dc)

# Conflicts:
#	src/Uno.UI.Composition/Composition/RectangleClip.skia.cs
GetPrePaintingClipping rebuilt its round-rect geometry on every frame for
every rounded border, allocating a fresh builder and geometry (and their
native path) per border per frame.

Cache the geometry keyed on the RoundRectangle it was built from. It is a
record struct, so value equality is an exact key: changed geometry always
misses. The superseded geometry is released, and every caller gets its own
reference, so IntersectOwned releasing it never frees the cached instance.

Measured on the same 1561-element rounded tree
(specs/059-performance-improvements): paint allocations drop from 272 KB
to 9.75 KB per frame (-96%), and -98% cumulatively with the two preceding
fixes -- 332 B down to 6 B per element per frame. The 262 KB saved is 336 B
per border, an SKPathBuilder plus an SKPath plus their handle
registrations. Frame time is unchanged, confirming the same rasterization
work still happens. Rendering runtime tests: identical pass/fail sets.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_015DSdL5YzLhjx8CeGXZf1Au
(cherry picked from commit 9726017)

# Conflicts:
#	src/Uno.UI.Composition/Composition/BorderVisual.skia.cs
Key the RectangleClip path cache on its round rect, as InsetClip and
BorderVisual do, so it can't go stale whatever path mutates it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit c9e1c91)

# Conflicts:
#	src/Uno.UI.Composition/Composition/RectangleClip.skia.cs
Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 7bed8f0)

# Conflicts:
#	src/Uno.UI.RuntimeTests/Tests/Windows_UI_Composition/Given_CompositionGeometricClip.cs
@mergify

mergify Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor Author

Cherry-pick of 2f505dc has failed:

On branch mergify/bp/servicing/6.8/pr-24230
Your branch is up to date with 'origin/servicing/6.8'.

You are currently cherry-picking commit 2f505dc.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   src/Uno.UI.Composition/Composition/RectangleClip.skia.cs

no changes added to commit (use "git add" and/or "git commit -a")

Cherry-pick of 9726017 has failed:

On branch mergify/bp/servicing/6.8/pr-24230
Your branch is ahead of 'origin/servicing/6.8' by 1 commit.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 9726017.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   src/Uno.UI.Composition/Composition/BorderVisual.skia.cs

no changes added to commit (use "git add" and/or "git commit -a")

Cherry-pick of c9e1c91 has failed:

On branch mergify/bp/servicing/6.8/pr-24230
Your branch is ahead of 'origin/servicing/6.8' by 2 commits.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit c9e1c91.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   src/Uno.UI.Composition/Composition/BorderVisual.skia.cs

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   src/Uno.UI.Composition/Composition/RectangleClip.skia.cs

Cherry-pick of 7bed8f0 has failed:

On branch mergify/bp/servicing/6.8/pr-24230
Your branch is ahead of 'origin/servicing/6.8' by 3 commits.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 7bed8f0.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)
	deleted by us:   src/Uno.UI.RuntimeTests/Tests/Windows_UI_Composition/Given_CompositionGeometricClip.cs

no changes added to commit (use "git add" and/or "git commit -a")

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

@mergify mergify Bot added the conflicts Automated merge hit conflicts label Sep 25, 2026
@github-actions github-actions Bot added the area/skia ✏️ Categorizes an issue or PR as relevant to Skia label Sep 25, 2026
@mergify mergify Bot mentioned this pull request Sep 25, 2026
1 of 5 tasks
@MartinZikmund

Copy link
Copy Markdown
Member

Closing: this backport doesn't apply to this branch. Mergify committed unresolved conflict markers, which is why CI failed.

#24230 fixes per-frame allocations that come from master's IGeometry path layer. This branch still uses raw SKPath, and RectangleClip.GetClipPath / BorderVisual.GetPrePaintingClipping already reuse a static spare path with Rewind(), so there is nothing to cache. The test depends on IGeometry APIs and its file doesn't exist here.

Profiling the #23984 scroll scenario on 6.8 puts clip-path work under 3% of UI-thread time. The remaining 6.5 → 6.8 gap comes from per-visual damage path booleans and Win32 UIA events on every layout move. Both are being fixed in separate PRs.

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

Labels

area/skia ✏️ Categorizes an issue or PR as relevant to Skia conflicts Automated merge hit conflicts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant