Repository navigation
Closed
Labels
perf/generalThe issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf)
Milestone
Description
Summary
Every BindableObject eagerly allocates a Dictionary<TriggerBase, SetterSpecificity> at construction time, even though the vast majority of objects never use triggers. Lazy-initializing this field would eliminate a per-object heap allocation for all trigger-free elements.
Current behavior
| internal Dictionary<TriggerBase, SetterSpecificity> _triggerSpecificity = new Dictionary<TriggerBase, SetterSpecificity>(); |
The dictionary is only accessed from TriggerBase attach/detach/query:
| bindable._triggerSpecificity[this] = specificity; |
| ((SealedList<TriggerAction>)EnterActions).IsReadOnly = true; |
In a typical app, triggers are used on <5% of visual elements, but every Label, Button, Grid, StackLayout, etc. pays the allocation cost.
Proposed change
Lazy-initialize on first use:
// Before
internal Dictionary<TriggerBase, SetterSpecificity> _triggerSpecificity = new Dictionary<TriggerBase, SetterSpecificity>();
// After
internal Dictionary<TriggerBase, SetterSpecificity>? _triggerSpecificity;The three call sites in TriggerBase.cs need minor updates:
- Attach (line 83):
(bindable._triggerSpecificity ??= new()).Add(this, specificity) - Detach (line 93):
bindable._triggerSpecificity?.Remove(this) - Query (line 109):
bindable._triggerSpecificity?.TryGetValue(this, out var specificity) == true
Estimated impact
An empty Dictionary<TriggerBase, SetterSpecificity> costs ~100 bytes. For a page with 200 elements: ~20 KB saved per page load.
Metadata
Metadata
Assignees
Labels
perf/generalThe issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf)
Activity
Lazy-initialize _triggerSpecificity dictionary on BindableObject
Lazy-initialize _triggerSpecificity dictionary on BindableObject
[Perf] Lazy-initialize _triggerSpecificity dictionary on BindableObje…
PureWeen commentedon Jun 10, 2026
Closed by #34133 (merged to
net11.0). GitHub only auto-closes for PRs merged to the default branch; this issue was fixed by a PR merged to a non-default branch.