Skip to content

perf(composition): Drop the CompositionObject finalizer - #25046

Open
MartinZikmund wants to merge 3 commits into
dev/mazi/fix-detached-subtree-animationsfrom
dev/mazi/perf-comp-finalizer
Open

MartinZikmund wants to merge 3 commits into
dev/mazi/fix-detached-subtree-animationsfrom
dev/mazi/perf-comp-finalizer

Conversation

@MartinZikmund

@MartinZikmund MartinZikmund commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

GitHub Issue: relates to #25042

Important

Stacked on #25055. Dropping the finalizer is only safe once a running animation can no longer outlive its tree, which #25055 fixes. Until both land, #25187 (closes #25042) fixes the retention by suppressing the finalizer while an object has no animations. Once #25187 merges, this PR rebases to remove the finalizer and that suppression logic altogether.

PR Type:

🐞 Bugfix

What changed? 🚀

Every CompositionObject was finalizable, and its finalizer (always off the UI thread) enqueued StopAllAnimations onto the UI dispatcher even when there were no animations. Every dead visual was promoted, went through the finalization queue, and stayed alive with its subgraph until the UI thread drained a no-op callback.

  • DisposeInternal returns early when there are no animations, so nothing is queued.
  • The ~CompositionObject() finalizer is removed. A running or paused animation always keeps its target reachable (compositor, scoped batch, animation target list), so an unreachable visual never has an animation to stop. Every DisposeInternal override only frees SkiaSharp/drawing objects whose backends have their own finalizers; CompositionImageSurface keeps its own finalizer.

Measured (Release, net10.0-desktop, 100k Border { Child = TextBlock } created and dropped while the UI thread is blocked, 3 rounds):

Retained Gen1 GCs Create + collect
Before 253-257 MB 34-37 1.65-2.4 s
After 0 MB 13-14 0.45-0.83 s

Tests: Given_CompositionObject_Lifetime (2 runtime tests) fail on master and pass with the fix. They use long weak references and collect on a worker thread while the UI thread sleeps, since waiting on the STA UI thread pumps messages and would drain the queue. All 115 Windows_UI_Composition runtime tests pass.

Risk: a composition object dropped without Dispose now frees its Skia handles through SkiaSharp's own finalizers rather than DisposeInternal: same result, later. No WinUI parity impact (WinUI composition objects are COM objects).

PR Checklist ✅

🤖 Generated with Claude Code

https://claude.ai/code/session_01NL1zD4nmcDjrcPhceNXFNm

Copilot AI balanced review requested due to automatic review settings October 6, 2026 11:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @MartinZikmund's task in 18s —— View job


Review of #25046: drop the CompositionObject finalizer

Verdict: ship. I found no blocking issues and posted no inline comments.

Issues by severity: 0 blocking, 0 major, 2 minor notes (below).

What I checked (code review only; I did not build or run tests):

  • The fix is a root-cause fix. The finalizer was what queued a no-op onto the UI dispatcher, and removing it ends the retention.
  • The early return in DisposeInternal also stops an explicit off-thread Dispose() from queuing no-op work.
  • Every DisposeInternal override I found under src/Uno.UI.Composition/Composition calls base.DisposeInternal(). The base early return doesn't skip any subclass cleanup, because the base call runs after the subclass frees its own resources.
  • The tests follow the repo conventions. They are gated with #if __SKIA__, use [RunsOnUIThread], and use long weak references. Collection runs on a worker thread, so the UI thread never pumps messages. There are no ignored or skipped tests.
  • The code style is fine: tabs, braces, and a short comment that explains the why.

Minor notes (non-blocking):

  • DisposeInternal now reads _animations.Count on whatever thread calls Dispose(), before the dispatcher hop. An off-thread Dispose() could race with a UI-thread animation start, and an animation started just after that read would not be stopped. The previous code had the same race in a different form, since it always hopped. This is only worth a follow-up if off-thread Dispose() is a supported pattern.
  • The PR says an unreachable visual never has a live animation, because the compositor, scoped batch, or animation target list keeps the target reachable. I took that on trust. It would be worth confirming for ExpressionAnimation and AnimationController, where a controller could hold the target without the compositor doing so.

The PR title and commits follow Conventional Commits.

@unodevops

Copy link
Copy Markdown
Contributor

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 237636 found UI Test snapshots differences: skia-linux-screenshots: 130, skia-windows-screenshots: 178

Details
  • skia-linux-screenshots: 130 changed over 2458

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • CalendarView_Theming.png-dark
    • ColorPickerSample.png-dark
    • ColorPickerSample.png
    • CompositionEffectBrush.png-dark
    • CompositionEffectBrush.png
    • CompositionNineGridBrush_Source_Changes.png-dark
    • CompositionNineGridBrush_Source_Changes.png
    • DisplayInformation.png-dark
    • ClipboardTests.png-dark
    • DynamicFont.png-dark
    • DynamicFont.png
    • AutomationProperties_AutomationId.png-dark
    • AutomationProperties_AutomationId.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • Buttons.png-dark
    • Buttons.png
    • CompositionPath.png-dark
    • CompositionPath.png
    • DoubleImageBrushInList.png-dark
  • skia-windows-screenshots: 178 changed over 2458

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • Buttons.png-dark
    • Buttons.png
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • ClipboardTests.png-dark
    • ClipboardTests.png
    • Examples.png
    • Grid_left_column_Auto.png-dark
    • Grid_left_column_Auto.png
    • Grid_Margin_And_Padding.png-dark
    • Grid_Margin_And_Padding.png
    • Grid_middle_col_auto__bottom_row_auto.png-dark
    • Grid_middle_col_auto__bottom_row_auto.png
    • Grid_MinWidth_MaxWidth.png-dark
    • Grid_MinWidth_MaxWidth.png
    • Focus_FocusVisual_Properties.png-dark
    • ImagesInlineInFlipView.png-dark
    • ImagesInlineInFlipView.png

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 237636 has failed on Uno.UI - CI.

MartinZikmund and others added 3 commits October 10, 2026 17:56
Disposing a composition object off the UI thread queued StopAllAnimations
on the dispatcher even when it had no animations, keeping the object and
its subgraph alive until the UI thread drained the queue.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NL1zD4nmcDjrcPhceNXFNm
Its only job was stopping animations, but a running or paused animation
references its target (AnimationFrame handler, Compositor registration,
scoped batch, started-objects list), so an unreachable object never has
live animations. Native payloads released by DisposeInternal overrides are
SkiaSharp objects or backend resources with their own finalizers, and
CompositionImageSurface keeps its own. Every visual was finalizable and
survived an extra GC for nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NL1zD4nmcDjrcPhceNXFNm
@MartinZikmund
MartinZikmund force-pushed the dev/mazi/perf-comp-finalizer branch from f399ebc to ce1c25d Compare October 10, 2026 15:56
Copilot AI balanced review requested due to automatic review settings October 10, 2026 15:56
@MartinZikmund
MartinZikmund changed the base branch from master to dev/mazi/fix-detached-subtree-animations October 10, 2026 15:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @MartinZikmund's task in 15s —— View job


Re-review of #25046 after the latest push

Verdict: ship. No new findings and no inline comments. This was a code-review pass only. I did not build or run tests.

The diff against the base (CompositionObject.cs plus the new lifetime test) is the same as in the earlier review. The finalizer removal and the early return in DisposeInternal still hold up. The base early return doesn't skip subclass cleanup, because overrides free their own resources before calling base.DisposeInternal().

CI: build 237636 failed with 130 Linux and 178 Windows screenshot differences. They are spread across unrelated samples such as Buttons, CalendarView_Theming and the Grid_* pages. This PR has no rendering change, so I suspect baseline drift or inherited changes from the base branch (#25055), but I haven't confirmed it. Compare against the base branch's last green screenshot run before merging.

Carried over from the earlier review: the thread-safety note on reading _animations.Count in an off-thread Dispose() is still open. It is only worth a follow-up if that pattern is supported.

@mergify

mergify Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@morning4coffe-dev morning4coffe-dev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified at head ce1c25d: read the diff and the surrounding code. All 13 DisposeInternal overrides under src/Uno.UI.Composition call base.DisposeInternal(), directly or after their own cleanup, so the new early return cannot skip subclass cleanup. CompositionImageSurface keeps its own finalizer (CompositionImageSurface.skia.cs, ~CompositionImageSurface), as the PR says. AnimationController holds its target (CompositionObject.cs, TryGetAnimationController returns new(this, ...)), which answers the reachability question raised by the bot review. No public API change; no WinUI parity surface.

Validation: code review only. I did not build, and I did not run the two new runtime tests.

Findings, most severe first:

  1. Merge order and the reachability wording (medium). The fix is only safe with #25055 in place, which the PR body states. The invariant in the body is off, though: after #25055, a parked infinite animation holds its host weakly, so "a running or paused animation always keeps its target reachable" is not accurate. The correct reasoning is that an animation whose host is unreachable can no longer be observed or ticked, so skipping Stop() is harmless. Please reword, and keep the #25055 dependency explicit in the description until it lands.

  2. Test cleanup (low). In Given_CompositionObject_Lifetime.cs, DisposeOffThread calls GC.SuppressFinalize(visual). With the finalizer gone this is a no-op, and its comment ("Only the explicit Dispose path is under test here") is misleading. Remove both. The comment on CreateDroppedElementVisuals about short weak references clearing "before the finalizer runs" is also stale.

  3. Off-thread read (low). DisposeInternal reads _animations.Count before the dispatcher hop, so a Dispose() from a non-UI thread races with a UI-thread StartAnimation. The PR does not say whether off-thread Dispose() is supported. If it is not, add a comment or an assertion. If it is, the check should move inside the dispatched work.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Every CompositionObject is finalizable and its finalizer queues no-op work on the UI thread

5 participants