Skip to content

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

Closed

Description

@simonrozsival

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.

Activity

  1. added
    perf/general
    on Feb 19, 2026
  2. PureWeen commented on Jun 10, 2026

    @PureWeen
    Member

    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.

  3. locked and limited conversation to collaborators on Jul 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    perf/general

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions