Skip to content

Generated bool DependencyProperty setters box on every setΒ #24241

Open

Description

@MartinZikmund

What would you like to be added? πŸ’‘

DependencyObject.SetValue takes an object, so a bool is boxed at the call site β€” 24 bytes on every set.

Uno.UI already keeps the two boxed instances for exactly this reason (Uno.UI.Helpers.Boxes.BooleanBoxes, with a Boxes.Box(bool) helper) and uses them by hand in a handful of places. But both DependencyProperty generators emit the plain form:

private void SetIsEnabledValue(bool value) => SetValue(IsEnabledProperty, value);
//                                                                       ^^^^^ boxed, every time

so every generated bool setter allocates.

Both generators should emit SetValue(XProperty, global::Uno.UI.Helpers.Boxes.Box(value)) when the property type is bool, passing value through unchanged for every other type β€” which also makes it automatic for any bool DP added later.

  • src/SourceGenerators/Uno.UI.SourceGenerators.Internal/DependencyObject/DependencyPropertyGenerator.cs
  • src/SourceGenerators/Uno.UI.SourceGenerators.Internal/Mixins/DependencyPropertyMixinGenerator.cs (its data model declares 13 bool properties)

Why is this needed? πŸ€”

bool dependency properties are set constantly β€” visual-state changes, IsEnabled propagation down a subtree, focus and tab-stop flags β€” and each set allocates a box that is immediately discarded.

Measured impact πŸ“Š

1,000 Γ— toggling Control.IsEnabled (a [GeneratedDependencyProperty] bool) on a Button, 3 runs, median:

Metric Before After Ξ”
alloc / set 72.0 B 48.0 B βˆ’33.3 %

Byte-identical across every run in both configurations β€” exactly 24 B per set, one box. The remaining 48 B is the change-notification path and is unrelated.

Why it cannot change behaviour βœ…

DependencyObject.AreDifferent (DependencyObject.Store.cs:2313) takes the newValue is ValueType branch for a boxed bool and compares with object.Equals β€” a value comparison, not a reference comparison:

if (newValue is ValueType || newValue is string)
{
    return !object.Equals(previousValue, newValue);
}

So change detection never depended on box identity, and sharing the two boxed instances is invisible to it.

Renderer 🎨

  • Skia

For which platforms πŸ“±πŸ’»πŸ–₯️

All platforms 🌍

Anything else we need to know? πŸ’¬

Follow-up, deliberately not included: 151 hand-written bool DP setters in Uno.UI still call SetValue(XProperty, value) directly and pay the same 24 B, including hot ones on the base types β€” UIElement.IsHitTestVisible (72 B/set measured) and UIElement.IsTabStop (24 B/set measured) are both hand-written, not generated. That is a 151-site mechanical sweep and is worth doing separately.

Found by allocation profiling and measured before/after with a headless harness that project-references the real framework. Full methodology and raw runs: specs/059-performance-improvements/evidence.md.

Activity

  1. added
    triage/untriaged
    on Aug 27, 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

    triage/untriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions