Skip to content

Comments

[Perf] Lazy-initialize _triggerSpecificity dictionary on BindableObject#34133

Open
simonrozsival wants to merge 2 commits intonet11.0from
dev/simonrozsival/lazy-trigger-specificity
Open

[Perf] Lazy-initialize _triggerSpecificity dictionary on BindableObject#34133
simonrozsival wants to merge 2 commits intonet11.0from
dev/simonrozsival/lazy-trigger-specificity

Conversation

@simonrozsival
Copy link
Member

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 _triggerSpecificity was eagerly allocated on every BindableObject instance, 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<...>() initializer
  • TriggerBase.cs — null-safe access at 3 call sites (??= on attach, ?. on detach, ?.TryGetValue on condition changed)

Fixes #34131

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@simonrozsival simonrozsival added perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) copilot labels Feb 19, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

internal ushort _triggerCount = 0;
internal Dictionary<TriggerBase, SetterSpecificity> _triggerSpecificity = new Dictionary<TriggerBase, SetterSpecificity>();
internal Dictionary<TriggerBase, SetterSpecificity> _triggerSpecificity;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it work to add

#nullable enable

internal Dictionary<TriggerBase, SetterSpecificity>? _triggerSpecificity;

#nullable disable

?

Or would it still cascade to more and more changes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@simonrozsival simonrozsival marked this pull request as ready for review February 20, 2026 14:38
Copilot AI review requested due to automatic review settings February 20, 2026 14:38
Copy link
Contributor

Copilot AI left a comment

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 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 _triggerSpecificity dictionary in BindableObject
  • 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)

@simonrozsival simonrozsival changed the title [WIP][Perf] Lazy-initialize _triggerSpecificity dictionary on BindableObject [Perf] Lazy-initialize _triggerSpecificity dictionary on BindableObject Feb 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

copilot perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants