Skip to content

perf(xaml): Generate a fast path for TemplateBinding - #26584

Open
MartinZikmund wants to merge 2 commits into
masterfrom
dev/mazi/perf2026-templatebinding
Open

MartinZikmund wants to merge 2 commits into
masterfrom
dev/mazi/perf2026-templatebinding

Conversation

@MartinZikmund

@MartinZikmund MartinZikmund commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

GitHub Issue: Part of #24093

PR Type:

🔄 Refactoring

What changed? 🚀

Split out of #24094 (originally by @morning4coffe-dev), which now only carries the opt-in XamlOptionalChanges part. This piece is always on, so it gets its own review.

The XAML generator now compiles a simple one-way {TemplateBinding} into direct source/target DependencyProperty references (BindingHelper.SetTemplateBinding). That skips allocating a Binding, RelativeSource, BindingPath, its path nodes and the reflection-based target setter. Complex paths/options keep the general binding fallback.

  • Handles templated-parent replacement, suspension/resume, attached DPs (as source and target), hot-reload cloning and lazy ParentBinding reconstruction.
  • DependencyObject.Store detects template bindings through BindingExpression.IsTemplateBinding instead of reading ParentBinding.RelativeSource, so the lazy ParentBinding isn't materialized.
  • 1,391 of the 1,399 checked-in {TemplateBinding} uses in Uno.UI qualify.

Upstream counterpart: WinUI removed the per-TemplateBinding handler allocation in bbb46616.

Validation

  • Compile: Uno.UI.UnitTests (net11.0) and Uno.UI.SourceGenerators.Tests.
  • Runtime: BindingTests.Given_Binding unit tests 13/13. All source-generator tests 639 passed, 0 failed, 7 skipped (including the new fast-path snapshots).

Performance

Headless Skia app with Fluent resources (x64, net10.0, DOTNET_TieredCompilation=0), one process per scenario, 4 interleaved A/B rounds against master. Allocations are UI-thread bytes and exact: a master-vs-master null run drifted 0.00%. A time change is bold only when it is larger than that scenario's null drift and the A/B ranges don't overlap; anything else is marked noise. Background load on the machine made some timings noisy.

Scenario Allocated per iteration Δ alloc Per unit Time per iteration (median) Δ time Null drift
300 Fluent Buttons, built + laid out 34,689,532 → 26,927,412 B −22.38% −25.3 KB 91.00 → 72.47 ms −20.4% (noise) ±6.8%
300 Buttons with local values overriding the style 35,196,331 → 27,498,128 B −21.87% −25.1 KB 89.36 → 79.45 ms −11.1% (noise) ±1.2%
Mixed page: 40 × 7 Fluent controls 90,441,326 → 82,265,926 B −9.04% −28.5 KB 289.68 → 280.29 ms −3.2% (noise) ±3.1%
100 Button/CheckBox × 4 visual-state changes 745,659 → 745,659 B 0 — 2.14 → 1.99 ms −7.0% (noise) ±7.7%
2,000 Border + child (no templates) 28,781,390 → 28,781,390 B 0 — 8.52 → 8.43 ms −1.0% (noise) ±11.5%
200k Tag set/get 0 → 0 B 0 — 19.85 → 18.95 ms −4.5% (noise) ±6.1%

The generated fast path removes about 25 KB per templated Fluent Button (−22% of everything a Button allocates to build and lay out), and about 28 KB per control on a mixed page. Untemplated elements and visual-state changes are unaffected, as expected. This is the largest single win of the Perf2026 port.

PR Checklist ✅

🤖 Generated with Claude Code

https://claude.ai/code/session_01TXMNu1LMuUpziGZWHVDmpX

Copilot AI balanced review requested due to automatic review settings November 18, 2026 10:55
@github-actions github-actions Bot added area/code-generation Categorizes an issue or PR as relevant to code generation area/automation Categorizes an issue or PR as relevant to project automation labels Nov 18, 2026

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 insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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


Review of #26584

Verdict: fix-first (minor). The design is sound. The fast path is isolated behind IsTemplateBinding, and the lazy ParentBinding keeps the fallback compatible. The PR follows AGENTS.md conventions (Allman braces, tabs, no event Action, Skia-only). I reviewed by inspection only and did not build or run tests.

Findings (3 inline, all medium/low):

  • Medium: SetTemplatedParent now re-applies every template binding, including the general fallback ones. Possible double evaluation.
  • Low: replaced BindingExpressions stay in _bindings after ClearBinding().
  • Low: GetCurrentControlTemplateTargetType falls back to the style's TargetType. This is untested for templates without TargetType.

Cross-cutting:

@mergify

mergify Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request


// No validation hook: a TemplateBinding never resolves a source path, so it can't be a validation source.
details.SetBinding(bindingExpression);
_bindings = _bindings.Add(bindingExpression);

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.

details.ClearBinding() disposes the previous expression but never removes it from _bindings. Re-applying a template binding to the same target (template re-applied or pooled, hot reload) leaves disposed expressions in the list. ApplyTemplateBindingParents, the suspend/resume loops and the theme-resource loops then iterate over them. ApplyTemplateBindingParent returns early when _disposed, so this is not a correctness bug, but the list grows. Check how SetBinding (line ~174) handles replacement and remove the old expression the same way.

}

_templatedParentWeakRef = parentReference;
_properties?.ApplyTemplateBindingParents();

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.

ApplyTemplateBindingParents() is new behavior for all template bindings, including the general Binding { RelativeSource = TemplatedParent } fallback. Those were previously re-resolved through the existing paths (ApplyTemplateBindingParent calls elsewhere, DataContext propagation). Re-applying them here may double-evaluate them. It also fires on every templated-parent change, including the common set-once case. Please add a runtime test that counts evaluations or setter invocations for a general-path template binding. Alternatively, restrict this call to fast-path expressions (TemplateBindingSourceProperty is not null).

{
if (scope.Object.Type.Name == "ControlTemplate"
&& FindMember(scope.Object, "TargetType")?.Value is { } targetType)
{

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.

The fallback to CurrentStyleTargetType assumes the enclosing Style's TargetType equals the templated parent type. For a ControlTemplate with no TargetType inside a Setter of Style TargetType="Control", this binds to Control.XProperty even when the template's real target is a derived type. This is the right owner in most cases, but it differs from the old reflection path, which resolved the name at runtime against the actual templated parent. The generator-level snapshot tests only cover templates that declare TargetType. Please add a case for a template without TargetType, or return null here so those uses keep the general fallback.

@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 247318 found UI Test snapshots differences: skia-linux-screenshots: 225, skia-windows-screenshots: 263

Details
  • skia-linux-screenshots: 225 changed over 2500

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Border_CornerRadius_Binding.png-dark
    • Border_CornerRadius_Binding.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • Accessibility_Showcase.png-dark
    • Accessibility_Showcase.png
    • Basics.png-dark
    • Basics.png
    • Buttons.png
    • ComboBox_MaxDropdownHeight.png-dark
    • ComboBox_MaxDropdownHeight.png
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • BreadcrumbBarPage.png-dark
    • BreadcrumbBarPage.png
    • AppWindowTitleBar Properties.png-dark
    • CompositionNineGridBrush_Source_Changes.png-dark
    • CompositionNineGridBrush_Source_Changes.png
    • BasicAcrylicBrushTest.png-dark
    • BasicAcrylicBrushTest.png
  • skia-windows-screenshots: 263 changed over 2500

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedBackVisualSourceSamplePage.png
    • AutomationProperties_StructureAndProps.png-dark
    • AutomationProperties_StructureAndProps.png
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • ClipboardTests.png-dark
    • AutomationProperties_AutomationId.png
    • Border_CornerRadius_Binding.png-dark
    • Border_CornerRadius_Binding.png
    • Border_CornerRadius_BorderThickness.png-dark
    • Border_CornerRadius_BorderThickness.png
    • Buttons.png-dark
    • AutomationProperties_AutomationId.png-dark
    • Basics.png-dark
    • Basics.png
    • BasicAcrylicBrushTest.png-dark
    • BasicAcrylicBrushTest.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • CalendarView_Theming.png-dark

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

Labels

area/automation Categorizes an issue or PR as relevant to project automation area/code-generation Categorizes an issue or PR as relevant to code generation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants