Skip to content

perf(composition): Suppress the finalizer without animations - #25187

Merged
MartinZikmund merged 3 commits into
masterfrom
dev/mazi/comp-finalizer-suppress
Oct 10, 2026
Merged

MartinZikmund merged 3 commits into
masterfrom
dev/mazi/comp-finalizer-suppress

Conversation

@MartinZikmund

Copy link
Copy Markdown
Member

GitHub Issue: closes #25042

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.

This is the conservative alternative to #25046, which drops the finalizer entirely. Dropping it is only safe once a running animation can no longer outlive its tree (#25055), so #25046 is now stacked on #25055 and this PR keeps the finalizer as a safety net in the meantime:

  • The finalizer is suppressed while an object has no animations (GC.SuppressFinalize in the constructor and when the last animation stops) and re-registered when the first animation starts (GC.ReRegisterForFinalize). The toggle only fires on a state change, since re-registering an already registered object queues its finalizer twice.
  • DisposeInternal returns early when there are no animations, so an explicit off-thread Dispose queues nothing either.
  • CompositionImageSurface has its own finalizer, which a base-class SuppressFinalize would silently disable, so it opts out (HasOwnFinalizer).

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

Retained Gen1 GCs Create + collect
master 253-257 MB 41-47 4.52-6.11 s
This PR (suppressed finalizer) 0 MB 14 1.26-1.80 s
#25046 (no finalizer) 0 MB 3-14 0.81-1.66 s

The remaining gap to #25046 is the cost of allocating finalizable objects plus the SuppressFinalize call; the retention, which is the real problem, is gone.

Tests: Given_CompositionObject_Lifetime (Skia runtime tests, long weak references, collecting on a worker thread while the UI thread sleeps so nothing drains the queue):

Regression: Windows_UI_Composition, Given_AnimatedVisualPlayer, Given_ProgressRing, Given_Storyboard, Given_AnimatedIcon runtime tests on Skia Desktop: 150/150 pass.

Risk: a composition object without animations dropped without Dispose 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_01KYKir5hPkrgan2z9NajHM7

MartinZikmund and others added 2 commits October 10, 2026 18:41
Every CompositionObject was finalizable, and its finalizer queued a no-op
StopAllAnimations onto the UI thread, retaining every dead visual and its
subgraph until the UI thread drained it.

Suppress finalization while an object has no animations and re-register it
when the first one starts, and skip queuing the stop when there is nothing
to stop. CompositionImageSurface keeps its own finalizer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYKir5hPkrgan2z9NajHM7
Copilot AI balanced review requested due to automatic review settings October 10, 2026 16:41

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 added the area/skia ✏️ Categorizes an issue or PR as relevant to Skia label Oct 10, 2026
@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

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


Review of #25187: suppress the CompositionObject finalizer without animations

Verdict: ship. I found no blocking issues. This is a code review only; I did not build or run anything.

Issues by severity: 0 blocking, 0 major, 2 minor notes (below). I posted no inline comments.

What I checked

  • Toggle logic: SetFinalizable only flips on a state change, so ReRegisterForFinalize can't queue the finalizer twice. The constructors suppress it, StartAnimation re-registers it, and the last StopAnimation or StopAllAnimations suppresses it again.
  • Subclass finalizers: CompositionImageSurface is the only other finalizer under Uno.UI.Composition. Its HasOwnFinalizer opt-out is correct, because a base-class SuppressFinalize would also have disabled its finalizer.
  • DisposeInternal: the early return when there are no animations is safe. The finalizer is suppressed in that state anyway, and an explicit Dispose now queues nothing.
  • Conventions: the change follows the repo style (tabs, Allman braces, short why-comments) and adds no event Action. The runtime tests cover both the fail-on-master and the animated-visual cases.

Minor notes (non-blocking)

  1. _isFinalizable is not synchronised. This is fine as long as animations are only started and stopped on the UI thread, which is the existing assumption. The finalizer cannot race with it, because it only runs once the object is unreachable.
  2. HasOwnFinalizer is a virtual call from the base constructor path, so it must stay a constant-returning property. A short doc remark would protect that, though the current summary comment is adequate.

Cross-cutting: this is explicitly an interim safety net until #25055 lets a running animation no longer outlive its tree. It would help to track removing SetFinalizable and the finalizer once #25046 lands.

@unodevops

Copy link
Copy Markdown
Contributor

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 238801 found UI Test snapshots differences: skia-linux-screenshots: 118, skia-windows-screenshots: 2458

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

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • ClipboardTests.png-dark
    • DisplayInformation.png-dark
    • Buttons.png-dark
    • Buttons.png
    • CompositionEffectBrush.png-dark
    • CompositionEffectBrush.png
    • CompositionNineGridBrush_Source_Changes.png-dark
    • CompositionNineGridBrush_Source_Changes.png
    • DynamicFont.png-dark
    • DynamicFont.png
    • DoubleImageBrushInList.png-dark
    • DoubleImageBrushInList.png
    • DropDownButtonPage.png-dark
    • Focus_FocusVisual_Properties.png-dark
    • Focus_FocusVisual_Properties.png
    • Examples.png
  • skia-windows-screenshots: 2458 changed over 2486

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Accessibility_ScreenReader.png-dark
    • AppBarButtonTest.png
    • AppBar_KeyBoard.png-dark
    • ApplicationViewSpanningRectsPage.png-dark
    • AppWindowFeatures.png-dark
    • AppWindowPresenters.png-dark
    • ArcSegment.png-dark
    • Attributed_text_Simple.png-dark
    • AuthenticationBroker_Cancel.png
    • AuthenticationBroker_Demo.png-dark
    • AutomationPropertiesExtensions_Role.png
    • AutomationProperties_AutomationId.png-dark
    • AutoSizedTopLeft.png-dark
    • AutoSizedTopLeft.png
    • AutoSuggestBox_Reason.png-dark
    • AutoSuggestBox_Reason.png
    • BasicAutoSuggestBox.png-dark
    • BasicAutoSuggestBox.png
    • Basics_Automated.png-dark
    • Basics_Automated.png

@unodevops

Copy link
Copy Markdown
Contributor

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

Browser WASM has no thread to run the Task.Run work while the UI
thread spins, so the run hung on these tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TP1JtY64etkfxxGCms44aN
Copilot AI balanced review requested due to automatic review settings October 10, 2026 20:59

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


Review of #25187: suppress the CompositionObject finalizer without animations

Verdict: ship. I found no blocking issues. This was a code review only. I did not build or run anything.

Issues by severity: 0 blocking, 0 major, 2 minor. I posted no inline comments.

What I checked

  • Toggle logic: SetFinalizable only flips on a state change, so ReRegisterForFinalize can't queue the finalizer twice. The constructors suppress the finalizer. StartAnimation re-registers it. The last StopAnimation and StopAllAnimations suppress it again.
  • Subclass finalizers: CompositionImageSurface opts out through HasOwnFinalizer, so its own finalizer keeps running. The HasOwnFinalizer check sits before the _isFinalizable write, so that flag never changes for that type. That is harmless.
  • DisposeInternal: the early return when there are no animations is safe, because the finalizer is suppressed in that state anyway.
  • Style: tabs, Allman braces and short why-comments. There is no event Action.

Minor notes (non-blocking)

  1. _isFinalizable is unsynchronised. That is fine while animations start and stop only on the UI thread, which is the existing assumption.
  2. HasOwnFinalizer is a virtual call made from the base constructor. Overrides must stay constant-returning and must not read subclass state. A one-line remark on the property would protect that.

CI: build 238801 failed. The screenshot diffs are broad (2458 of 2486 on Windows) and span unrelated samples such as AppBarButtonTest and AuthenticationBroker_Demo. That points to an environment or baseline problem rather than this change, but I didn't inspect the logs. Please check the failed stage before merging.

Cross-cutting: this is an interim safety net until #25055 lands. It would help to track removing SetFinalizable and the finalizer once #25046 is merged.

@MartinZikmund
MartinZikmund merged commit d6cb913 into master Oct 10, 2026
19 of 27 checks passed
@MartinZikmund
MartinZikmund deleted the dev/mazi/comp-finalizer-suppress branch October 10, 2026 21:02
@unodevops

Copy link
Copy Markdown
Contributor

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

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

4 participants