[Perf] Lazy-initialize _triggerSpecificity dictionary on BindableObject#34133
Open
simonrozsival wants to merge 2 commits intonet11.0from
Open
[Perf] Lazy-initialize _triggerSpecificity dictionary on BindableObject#34133simonrozsival wants to merge 2 commits intonet11.0from
simonrozsival wants to merge 2 commits intonet11.0from
Conversation
Fixes #34131 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Feb 19, 2026
Open
MartyIX
reviewed
Feb 20, 2026
|
|
||
| internal ushort _triggerCount = 0; | ||
| internal Dictionary<TriggerBase, SetterSpecificity> _triggerSpecificity = new Dictionary<TriggerBase, SetterSpecificity>(); | ||
| internal Dictionary<TriggerBase, SetterSpecificity> _triggerSpecificity; |
Contributor
There was a problem hiding this comment.
Would it work to add
#nullable enable
internal Dictionary<TriggerBase, SetterSpecificity>? _triggerSpecificity;
#nullable disable?
Or would it still cascade to more and more changes?
Member
Author
There was a problem hiding this comment.
That should work, but I don't think it has high value right now. We should let copilot annotate the whole class/codebase at some point.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes memory allocation in BindableObject by lazy-initializing the _triggerSpecificity dictionary, which is only used by the small fraction of objects (<5%) that actually have triggers attached. This reduces per-instance allocations for the vast majority of MAUI UI elements.
Changes:
- Removed eager initialization of
_triggerSpecificitydictionary inBindableObject - Added null-safe operations at all three access points in
TriggerBase(attach, detach, condition changed) - Added comprehensive benchmarks to measure allocation improvements for objects without triggers
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Controls/src/Core/BindableObject.cs | Removed eager = new Dictionary<...>() initializer from _triggerSpecificity field |
| src/Controls/src/Core/Interactivity/TriggerBase.cs | Added null-coalescing assignment on attach, conditional access on detach and condition query |
| src/Core/tests/Benchmarks/Benchmarks/BindableObjectAllocBenchmarker.cs | New benchmark file covering multiple performance issues including trigger dictionary allocation (#34131) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Fixes #34131
Description
The
Dictionary<TriggerBase, SetterSpecificity>field_triggerSpecificitywas eagerly allocated on everyBindableObjectinstance, but fewer than 5% of objects ever use triggers. This PR makes it lazy-initialized on first use.Changes
BindableObject.cs— remove eager= new Dictionary<...>()initializerTriggerBase.cs— null-safe access at 3 call sites (??=on attach,?.on detach,?.TryGetValueon condition changed)