Skip to content

feat: Add CreateWithCapacity factory method to ResourceDictionary to reduce SpecializedResourceDictionary resizes - #23235

Merged
MartinZikmund merged 5 commits into
masterfrom
copilot/add-resource-dictionary-overload
May 11, 2026
Merged

MartinZikmund merged 5 commits into
masterfrom
copilot/add-resource-dictionary-overload

Conversation

Copilot AI commented May 9, 2026 •

Copy link
Copy Markdown
Contributor

ResourceDictionary always initialized its backing SpecializedResourceDictionary with no capacity, causing repeated resize allocations during XAML parsing — even though the number of entries is statically known at codegen time (as shown in the profiler screenshot: Resize appears 644,648 times, 1.4% inclusive).

PR Type:

✨ Feature

What changed? 🚀

  • ResourceDictionary.cs: Added internal ResourceDictionary(int initialCapacity) constructor that pre-sizes the backing SpecializedResourceDictionary. Added [EditorBrowsable(EditorBrowsableState.Never)] public static ResourceDictionary CreateWithCapacity(int initialCapacity) factory method wrapping the internal constructor — public so the XAML source generator can call it, but hidden from IDE completion so external users aren't exposed to it.

  • XamlFileGenerator.cs: For top-level singleton resource dictionaries, counts resource items at codegen time. When resourceCount > 0, emits a statement-based initialization using CreateWithCapacity instead of an object-initializer, pre-sizing the backing store. Also extracts MergedDictionaries and ThemeDictionaries member lookups to local variables to reduce duplication. Inline (non-singleton) dictionaries continue to use plain new ResourceDictionary().

Generated code before:

// top-level singleton
_MyDictionary_ResourceDictionary =
new global::Microsoft.UI.Xaml.ResourceDictionary
{
    IsParsing = true,
    ["myKey"] = ...,
};

Generated code after:

// top-level singleton — capacity pre-allocated
_MyDictionary_ResourceDictionary = global::Microsoft.UI.Xaml.ResourceDictionary.CreateWithCapacity(1);
_MyDictionary_ResourceDictionary.IsParsing = true;
_MyDictionary_ResourceDictionary["myKey"] = ...;
  • Test snapshots updated to reflect the new factory method + statement-based pattern for top-level singleton dictionaries with entries, and reverted to plain new ResourceDictionary for inline dictionaries.

PR Checklist ✅

Copilot AI requested review from Copilot and removed request for Copilot May 9, 2026 08:38
Copilot AI linked an issue May 9, 2026 that may be closed by this pull request
Copilot AI requested review from Copilot and removed request for Copilot May 9, 2026 08:54
@CLAassistant

CLAassistant commented May 9, 2026 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ MartinZikmund
❌ Copilot
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI changed the title [WIP] Add overload for ResourceDictionary constructor with initial capacity feat: Add initialCapacity constructor to ResourceDictionary to reduce SpecializedResourceDictionary resizes May 9, 2026
Copilot AI requested a review from MartinZikmund May 9, 2026 08:56
@MartinZikmund

Copy link
Copy Markdown
Member

@copilot as it is used via source generator, the members need to be public. Instead lets create a public static factory method (something like CreateWithCapacity which will be not editor browsable so that external users don't utilize it) that uses this new internal ctor

Copilot AI requested review from Copilot and removed request for Copilot May 9, 2026 09:52
Copilot AI requested review from Copilot and removed request for Copilot May 9, 2026 09:56

Copilot AI commented May 9, 2026

Copy link
Copy Markdown
Contributor Author

@copilot as it is used via source generator, the members need to be public. Instead lets create a public static factory method (something like CreateWithCapacity which will be not editor browsable so ...

Done in 15e77a6. Added a [EditorBrowsable(EditorBrowsableState.Never)] public static ResourceDictionary CreateWithCapacity(int initialCapacity) factory method that wraps the internal constructor. The generator now uses this factory method in the top-level singleton path when resourceCount > 0, switching from an object-initializer to a statement-based pattern. Inline dictionaries (non-singleton) continue using plain new ResourceDictionary().

Copilot AI changed the title feat: Add initialCapacity constructor to ResourceDictionary to reduce SpecializedResourceDictionary resizes feat: Add CreateWithCapacity factory method to ResourceDictionary to reduce SpecializedResourceDictionary resizes May 9, 2026
@MartinZikmund

Copy link
Copy Markdown
Member

/azp run

@github-actions github-actions Bot added the area/code-generation Categorizes an issue or PR as relevant to code generation label May 9, 2026
@MartinZikmund
MartinZikmund marked this pull request as ready for review May 9, 2026 10:18
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

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.

Pull request overview

This PR introduces an allocation/performance optimization for XAML-generated ResourceDictionary initialization by allowing the source generator to pre-size the backing SpecializedResourceDictionary, reducing resize churn during parsing.

Changes:

  • Added an internal ResourceDictionary(int initialCapacity) constructor plus a public-but-hidden CreateWithCapacity(int) factory for generator use.
  • Updated XamlFileGenerator to emit statement-based initialization for top-level singleton dictionaries with resources, using CreateWithCapacity(resourceCount).
  • Updated source generator snapshot outputs to reflect the new emitted pattern.

Note: The PR description does not currently include a required GitHub issue link (Fixes/Closes/Related to with a full URL).

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Uno.UI/UI/Xaml/ResourceDictionary.cs Adds capacity-aware construction and a generator-facing factory method.
src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs Emits capacity-preallocated singleton dictionary initialization when resource count is known.
src/SourceGenerators/Uno.UI.SourceGenerators.Tests/.../XamlCodeGenerator_MyResourceDictionary_*.cs Updates test snapshot to match the new generated initialization pattern.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@unodevops

Copy link
Copy Markdown
Contributor

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 211781 found UI Test snapshots differences: skia-linux-screenshots: 52, skia-windows-screenshots: 78, wasm: 19

Details
  • skia-linux-screenshots: 52 changed over 2344

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Focus_FocusVisual_Properties.png-dark
    • Focus_FocusVisual_Properties.png
    • DisplayInformation.png-dark
    • Gamepad_CurrentReading.png-dark
    • Gamepad_CurrentReading.png
    • Gamepad_Enumeration.png-dark
    • Buttons.png-dark
    • Buttons.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • Examples.png-dark
    • Examples.png
    • ExpanderColorValidationPage.png-dark
    • ExpanderColorValidationPage.png
    • Image_Stretch_None.png-dark
    • Image_Stretch_None.png
    • ClipboardTests.png-dark
    • ImageIconPage.png-dark
    • ImageIconPage.png
    • CalendarView_Theming.png-dark
  • skia-windows-screenshots: 78 changed over 2344

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • ContentPresenter_NativeEmbedding_ZIndex.png
    • ElementLevelTheme.png-dark
    • ElementLevelTheme.png
    • Examples.png-dark
    • Examples.png
    • EllipsemaskingEllipseGrid.png-dark
    • EllipsemaskingEllipseGrid.png
    • Focus_FocusVisual_Properties.png-dark
    • Focus_FocusVisual_Properties.png
    • InfoBarPage.png-dark
    • InfoBarPage.png
    • CalendarView_Theming.png-dark
    • DropDownButtonPage.png-dark
    • DropDownButtonPage.png
    • Buttons.png-dark
    • Buttons.png
    • InfoBadgePage.png-dark
    • InfoBadgePage.png
    • ClipboardTests.png-dark
    • ClipboardTests.png
  • wasm: 19 changed over 1075

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • initial_state
    • SamplesApp.Wasm.Windows_UI_Xaml_Controls.ListView.ListView_IsSelected
    • MUXControlsTestApp.NavigationViewTopNavPage
    • UITests.Windows_UI_Xaml_Shapes.Offscreen_Shapes
    • UITests.Shared.Windows_UI_Input.GestureRecognizer.PointersEvents
    • UITests.Windows_UI_Xaml_Controls.CalendarView.CalendarView_Theming
    • SamplesApp.Windows_UI_Xaml_Controls.ListView.ListViewLarge
    • UITests.Shared.Microsoft_UI_Xaml_Controls.ExpanderTests.WinUIExpanderPage
    • SamplesApp.Windows_UI_Xaml_Controls.ListView.ListViewSelectedItems
    • UITests.Microsoft_UI_Xaml_Controls.WebView2Tests.WebView2_NavigationProperties
    • UITests.Shared.Windows_UI_Xaml_Controls.ImageTests.Image_Stretch_Algmnt_Inf_Horizontal
    • UITests.Uno_Web.Http.CookieManagerTests
    • UITests.Shared.Windows_UI_Xaml_Controls.MediaPlayerElement.MediaPlayerElement_Minimal
    • UITests.Shared.Windows_UI_Xaml_Controls.MediaPlayerElement.MediaPlayerElement_Ogg_Extension
    • Uno.UI.Samples.Samples.Shared.Content.UITests.ImageBrushInList
    • Uno.UI.Samples.Content.UITests.WebView.WebView_AnchorNavigation
    • Uno.UI.Samples.Content.UITests.TextBoxControl.TextBox_DeleteButton
    • UITests.Windows_UI_Xaml.Performance.Performance_1000ButtonsContinuousRendering
    • Uno.UI.Samples.UITests.ImageBrushTestControl.DoubleImageBrushInList

@MartinZikmund
MartinZikmund disabled auto-merge May 9, 2026 18:03
- Validate negative initialCapacity at the public boundary so callers see
  ParamName == "initialCapacity" instead of the internal sentinel string
  thrown by SpecializedResourceDictionary.
- Guard the source generator fast path with !IsResourceDictionarySubclass
  to keep typed dictionaries on the BuildTypedResourceDictionary route if
  the BuildTopLevelResourceDictionary entry condition is ever broadened.
- Add unit test for the argument validation and a generator snapshot test
  for an x:Class-backed typed ResourceDictionary root with N>0 entries.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@github-actions github-actions Bot added the area/automation Categorizes an issue or PR as relevant to project automation label May 10, 2026
@MartinZikmund

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@unodevops

Copy link
Copy Markdown
Contributor

@unodevops

Copy link
Copy Markdown
Contributor

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

@MartinZikmund
MartinZikmund enabled auto-merge May 11, 2026 05:41
@MartinZikmund
MartinZikmund merged commit 0264ab4 into master May 11, 2026
45 checks passed
@MartinZikmund
MartinZikmund deleted the copilot/add-resource-dictionary-overload branch May 11, 2026 20:23
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.

Reduce resizes in SpecializedResourceDictionary

8 participants