Repository navigation
feat: Add CreateWithCapacity factory method to ResourceDictionary to reduce SpecializedResourceDictionary resizes - #23235
Conversation
…e XAML generator Agent-Logs-Url: https://github.com/unoplatform/uno/sessions/8ef5685a-15ef-430b-b3ba-f16ce7472e27 Co-authored-by: MartinZikmund <[email protected]>
|
|
|
@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 |
…tory method Agent-Logs-Url: https://github.com/unoplatform/uno/sessions/a5ee4d79-1126-4dec-8926-629332684e23 Co-authored-by: MartinZikmund <[email protected]>
… to locals Agent-Logs-Url: https://github.com/unoplatform/uno/sessions/a5ee4d79-1126-4dec-8926-629332684e23 Co-authored-by: MartinZikmund <[email protected]>
Done in 15e77a6. Added a |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
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-hiddenCreateWithCapacity(int)factory for generator use. - Updated
XamlFileGeneratorto emit statement-based initialization for top-level singleton dictionaries with resources, usingCreateWithCapacity(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.
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23235/wasm-skia-net9/index.html |
|
The build 211781 found UI Test snapshots differences: Details
|
- 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]>
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23235/wasm-skia-net9/index.html |
|
|
ResourceDictionaryalways initialized its backingSpecializedResourceDictionarywith 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:Resizeappears 644,648 times, 1.4% inclusive).PR Type:
✨ Feature
What changed? 🚀
ResourceDictionary.cs: Addedinternal ResourceDictionary(int initialCapacity)constructor that pre-sizes the backingSpecializedResourceDictionary. 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. WhenresourceCount > 0, emits a statement-based initialization usingCreateWithCapacityinstead of an object-initializer, pre-sizing the backing store. Also extractsMergedDictionariesandThemeDictionariesmember lookups to local variables to reduce duplication. Inline (non-singleton) dictionaries continue to use plainnew ResourceDictionary().Generated code before:
Generated code after:
new ResourceDictionaryfor inline dictionaries.PR Checklist ✅
Screenshots Compare Test Runresults.