Skip to content

Avoid boxing allocations in dependency properties [7.1+] #12410

Description

@Youssef1313

image

We have some boxing allocations like these. I suspect this happens on the call SetValue(SomeProperty, value) where value is a value type.

We can have DependencyObjectGenerator to generate a bool overload for SetValue, and its internal implementation would look like this:

static class Boxes
{
    static object BoxedTrue = true;
    static object BoxedFalse = false;
}

// ...
void SetValue(DependencyProperty dp, bool value)
{
    SetValue(dp, value ? Boxes.BoxedTrue : Boxes.BoxedFalse);
}

We could do the same for integers, but we could handle minor common values (e.g, -1, 0, 1, 2) and fallback to boxing for any other values.

In addition, we should use the Boxes static class to provide default values for FrameworkPropertyMetadata.

In addition to that, we can create boxed values for some enums

Activity

  1. added
    kind/enhancement
    triage/untriaged
    difficulty/tbd
    on May 24, 2023
  2. Youssef1313 commented on May 24, 2023

    @Youssef1313
    MemberAuthor

    @jeromelaban Thoughts on this?

  3. added
    area/performance 📈
    on May 24, 2023
  4. jeromelaban commented on May 24, 2023

    @jeromelaban
    Member

    It's an optimization that's used in WPF, so it must make sense to use :)

  5. self-assigned this
    on May 26, 2023
  6. added
    difficulty/medium 🤔
    and removed
    triage/untriaged
    difficulty/tbd
    on Oct 25, 2023
  7. github-actions commented on Aug 9, 2025

    @github-actions
    Contributor

    Hey there — just a heads-up that this issue has been quiet for over a year. If there’s no update or comment in the next 10 days, it’ll be closed automatically. Feel free to remove the stale label or drop a note to keep it open.

  8. added
    stale
    on Aug 9, 2025
  9. removed
    stale
    on Aug 11, 2025
  10. MartinZikmund commented on Aug 11, 2025

    @MartinZikmund
    Member

    Still relevant

  11. changed the title [-]Avoid boxing allocations in dependency properties[/-] [+]Avoid boxing allocations in dependency properties [7.1+ ][/+] on Aug 9, 2026
  12. changed the title [-]Avoid boxing allocations in dependency properties [7.1+ ][/-] [+]Avoid boxing allocations in dependency properties [7.1+][/+] on Aug 9, 2026
  13. MartinZikmund commented on Sep 2, 2026

    @MartinZikmund
    Member

    Audit result: PARTIALLY DONE in feature/breakingchanges (as of 0950717a0d7).

    The Uno.UI.Helpers.Boxes cache the issue asks for exists and is hand-wired into a handful of call sites, but that infrastructure predates this branch (it is already present, byte-identical, on origin/master) and covers only bool, two enums, and double?. The issue's actual ask — teach DependencyPropertyGenerator/DependencyPropertyMixinGenerator to emit boxed bool sets automatically, so every [GeneratedDependencyProperty] bool setter stops allocating — is an open, unmerged PR, not landed. Integer small-value caching is not implemented at all.

    Landed

    • Uno.UI.Helpers.Boxes cache exists — src/Uno.UI/Helpers/Boxes.cs:9-58 defines BooleanBoxes (:11-15), VerticalAlignmentBoxes (:17-23), NullableDoubleBoxes (:25-29), StretchBoxes (:31-37), plus Box(bool) (:39), Box(VerticalAlignment) (:41-48) and Box(Stretch) (:50-57) helpers. This is the file the issue proposes, matching the static object BoxedTrue = true; pattern from the issue body almost verbatim.
    • Hand-wired call sites — src/Uno.UI/UI/Xaml/DependencyProperty.cs:554,558,561 boxes the default value of IsTabStopProperty; src/Uno.UI/UI/Xaml/Controls/Popup/Popup.cs:115 and src/Uno.UI/UI/Xaml/UIElement.Properties.cs:46 return Boxes.BooleanBoxes.BoxedFalse / Boxes.Box(element._isHitTestVisible) from manually-written bool DP getters; src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.Properties.cs:35,56 box CanUndo/CanRedo. Only 5 files in src/Uno.UI reference Boxes. at all (grep -rl "Boxes\." src/Uno.UI src/SourceGenerators → 5 hits).
    • This is pre-existing, not 7.0 work — git show origin/master:src/Uno.UI/Helpers/Boxes.cs returns the identical file, so none of the above landed as part of the breaking-changes branch; it was already on the pre-7.0 baseline.

    Remaining

    • Generator-emitted boxing for [GeneratedDependencyProperty] bool setters — the issue's core ask — is not on this branch. src/SourceGenerators/Uno.UI.SourceGenerators.Internal/DependencyObject/DependencyPropertyGenerator.cs:493,616 still emit the plain form: instance.SetValue({propertyOwnerTypeName}.{propertyName}Property, value) and SetValue({propertyName}Property, value), with no Boxes.Box(...) wrapping for bool-typed properties, and git grep -n "Boxes.Box\|Helpers.Boxes" src/SourceGenerators returns nothing. The exact fix (SetValue(XProperty, global::Uno.UI.Helpers.Boxes.Box(value)) for bool) is proposed but unmerged: perf(uno-ui): Use the cached bool boxes in generated DP setters #24242 (perf(uno-ui): Use the cached bool boxes in generated DP setters, closes Generated bool DependencyProperty setters box on every set #24241) is state: OPEN, mergedAt: null — git merge-base --is-ancestor f17201fbee2 HEAD (its head commit) → not an ancestor. DependencyPropertyMixinGenerator.cs:57 likewise still emits plain this.SetValue({dp.Name}Property, value);.
    • Integer small-value boxing (-1, 0, 1, 2) — not implemented. grep -rn "IntBoxes\|IntegerBoxes" across src/Uno.UI and src/SourceGenerators returns no matches; Boxes.cs has no integer box table.
    • Enum boxing is 2 enums, not "some enums." Only VerticalAlignmentBoxes and StretchBoxes exist; no HorizontalAlignmentBoxes, VisibilityBoxes, OrientationBoxes, etc.
    • FrameworkPropertyMetadata default-value boxing is not systematic. Only the one hand-coded IsTabStopProperty special case in DependencyProperty.cs:550-562 uses Boxes; there is no general mechanism routing [GeneratedDependencyProperty(DefaultValue = ...)] value-type defaults through the box cache.
    • Original issue-linked PR never merged. gh pr view 12677 — closes #12410, base master, opened years ago, still state: OPEN, unmerged — superseded in practice by the newer, narrower perf(uno-ui): Use the cached bool boxes in generated DP setters #24242/Generated bool DependencyProperty setters box on every set #24241 pair, which is itself still open.

    Caveats (not blocking)

    • The issue title carries [7.1+], i.e. explicitly deferred past 7.0 by design — its incompleteness here is expected, not a regression, and should not drive any state change.
    • no-repro is one of the issue's current labels, which reads oddly against a live perf-allocation report; that label choice is outside the scope of this audit.
    • perf(uno-ui): Use the cached bool boxes in generated DP setters #24242's own PR description cites the measured allocation win (72 B → 48 B per SetValue on a bool DP) and reports golden/runtime-test results, so the fix is implementation-ready — it simply hasn't merged.

    Audit is code inspection against the branch worktree; no build or runtime validation was performed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/performance 📈difficulty/medium 🤔kind/enhancementno-repro

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions