Skip to content

perf(uno-ui): Use the cached bool boxes in generated DP setters - #24242

Closed
MartinZikmund wants to merge 1 commit into
masterfrom
dev/mazi/perf-bool-boxing
Closed

MartinZikmund wants to merge 1 commit into
masterfrom
dev/mazi/perf-bool-boxing

Conversation

@MartinZikmund

Copy link
Copy Markdown
Member

GitHub Issue: closes #24241

PR Type:

🔄 Refactoring (no functional changes, no api changes)

What changed? 🚀

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 in Uno.UI.Helpers.Boxes and uses them by hand in a few places, but both DependencyProperty generators emitted the plain SetValue(prop, value), so every generated bool setter allocated.

Both generators now emit SetValue(XProperty, global::Uno.UI.Helpers.Boxes.Box(value)) when the property type is bool, and pass value through unchanged for every other type — so any bool DP added later gets this automatically.

  • DependencyPropertyGenerator — both emission sites (attached and instance setters)
  • DependencyPropertyMixinGenerator — its data model declares 13 bool properties

Uno.UI.Helpers.Boxes is internal to Uno.UI, which is the only assembly using [GeneratedDependencyProperty]; any other consumer would get a compile error here rather than a silent behaviour change.

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 to this change.

Measured with a headless harness that project-references the real framework, rebuilt from the working tree; full methodology and raw runs in specs/059-performance-improvements/evidence.md (PR #24238).

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:

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

Change detection therefore never depended on box identity, so sharing the two boxed instances is invisible to it.

Validation ✅

  • DependencyPropertyGenerator golden tests: 6 run, 6 passed.
  • Runtime tests, Skia Desktop — Given_Control, Given_Button, Given_CheckBox, Given_ToggleSwitch, Given_RadioButton, Given_VisualStateManager, Given_UIElement, Given_FrameworkElement: 684 run, 658 passed, 2 skipped, 26 failed, compared as sets of failing test names against a baseline build — 0 new failures, 0 newly passing.

⚠️ Note for reviewers: the wider Uno.UI.SourceGenerators.Tests suite is not a usable signal locally — it fails 340/482 on the unmodified tree and its pass count varies run to run (110, 128, 129 on identical inputs), because those tests load src/*/bin/** directly rather than through project references. The 6 DependencyProperty golden tests above were therefore checked individually. CI is the real check.

Follow-up (not in this PR)

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 (measured 72 B/set) and UIElement.IsTabStop (24 B/set) are hand-written, not generated. That is a 151-site mechanical sweep and is better done separately.

PR Checklist ✅

  • 🧪 Added Runtime tests, UI tests, or a manual test sample (for bug fixes / features, if applicable) — n/a, allocation-only change with no behaviour difference; covered by the existing generator goldens and control suites above
  • 📚 Docs have been added/updated following the documentation template (for bug fixes / features) — n/a, no API or behaviour change for app authors
  • 🖼️ Validated PR Screenshots Compare Test Run results. — to check once CI has run
  • ❗ Contains NO breaking changes
  • 👀 Reviewed 2 other open pull requests (optional but appreciated!)

Copilot AI lite review requested due to automatic review settings August 27, 2026 12:22
@github-actions github-actions Bot added the area/code-generation Categorizes an issue or PR as relevant to code generation label Aug 27, 2026
@github-actions

github-actions Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Claude encountered an error after 0s —— View job


I'll analyze this and get back to you.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates Uno.UI’s dependency-property source generators to avoid per-set allocations for bool DependencyProperty setters by emitting SetValue(..., global::Uno.UI.Helpers.Boxes.Box(value)) instead of SetValue(..., value) (which boxes).

Changes:

  • Update DependencyPropertyGenerator to box-cache bool values in generated attached and instance setters.
  • Update DependencyPropertyMixinGenerator to box-cache bool values in generated mixin-property setters.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/SourceGenerators/Uno.UI.SourceGenerators.Internal/Mixins/DependencyPropertyMixinGenerator.cs Emit Boxes.Box(value) for generated bool mixin property setters to avoid boxing allocations.
src/SourceGenerators/Uno.UI.SourceGenerators.Internal/DependencyObject/DependencyPropertyGenerator.cs Introduce a shared helper to emit Boxes.Box(value) for generated bool DP setters (attached + instance).

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +731 to +734
private static string GetBoxedValueExpression(string propertyTypeName)
=> propertyTypeName == "bool"
? "global::Uno.UI.Helpers.Boxes.Box(value)"
: "value";
Comment on lines +57 to +61
// SetValue takes an object, so a bool would be boxed on every set. Uno.UI already keeps the
// two boxed instances, and these properties (IsTabStop, focus flags and the like) are set
// often enough for a 24-byte allocation each time to be worth avoiding.
var setterValue = dp.PropertyType == "bool" ? "global::Uno.UI.Helpers.Boxes.Box(value)" : "value";
sb.AppendLine($"\t\t\tset {{ this.SetValue({dp.Name}Property, {setterValue}); }}");
@unodevops

Copy link
Copy Markdown
Contributor

@Youssef1313

Copy link
Copy Markdown
Member

@MartinZikmund FYI, #12677

Feel free to continue with your PR if it's more ready. You can take also a look at mine to see if there are interesting differences and try to take the best of the two worlds.

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 in
Uno.UI.Helpers.Boxes and uses them by hand in a few places, but both
DependencyProperty generators emitted the plain SetValue(prop, value), so
every generated bool setter allocated.

Emit Boxes.Box(value) for bool properties in both generators, and pass
value through unchanged for every other type, so any bool DP added later
gets this automatically.

This cannot change behaviour: AreDifferent takes the `newValue is
ValueType` branch for a boxed bool and compares with object.Equals, a
value comparison, so change detection never depended on box identity.

Measured (specs/059-performance-improvements): toggling Control.IsEnabled,
a generated bool DP, drops from 72 B to 48 B per set -- exactly 24 B, one
box, byte-identical across runs. DependencyPropertyGenerator golden tests
6/6 pass; 684 control and framework runtime tests show identical pass/fail
sets against a baseline build.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_015DSdL5YzLhjx8CeGXZf1Au
@MartinZikmund
MartinZikmund force-pushed the dev/mazi/perf-bool-boxing branch from cf83f33 to f17201f Compare August 29, 2026 10:12
@github-actions

github-actions Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Claude encountered an error after 0s —— View job


I'll analyze this and get back to you.

@unodevops

Copy link
Copy Markdown
Contributor

@MartinZikmund
MartinZikmund changed the base branch from feature/breakingchanges to master September 2, 2026 20:01
@unodevops

Copy link
Copy Markdown
Contributor

@morning4coffe-dev morning4coffe-dev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code review assessment: the emitted setters still call SetValue with the same Boolean value, now using the existing true/false boxes; non-Boolean types retain their previous expression. I checked helper accessibility for the actual consumers, including the friend unit-test assembly. This is a managed allocation optimization, not a change to WinUI Boolean/DP semantics. No blocking source issue found. Add golden assertions for both instance/attached and mixin Boolean setters to protect the new code-generation branch. Any failed automated-review check remains a separate merge gate.

@MartinZikmund MartinZikmund mentioned this pull request Sep 9, 2026
3 of 5 tasks
@MartinZikmund

Copy link
Copy Markdown
Member Author

Superseded by #12677, which has been brought up to current master.

That PR does the same thing generalised: the generated setter boxing now covers int and double as well as bool, the DefaultValue/FrameworkPropertyMetadata defaults are boxed too, and an analyzer (UnoInternal0002) enforces it across the whole of Uno.UI rather than just the generator output.

The DependencyPropertyMixinGenerator hunk from this PR — which #12677 predated — is carried over there.

One correction worth recording: this PR's comment asserted that Uno.UI is the only assembly using [GeneratedDependencyProperty], so a consumer without access to Boxes "would get a compile error rather than a silent behaviour change". That isn't right — Uno.UI.UnitTests uses the attribute too, and a compile error is not an acceptable outcome for an external consumer either way. #12677 instead has the generator check that Uno.UI.Helpers.Boxes is actually accessible from the compilation and emit the plain value when it isn't.

MartinZikmund added a commit to Youssef1313/uno that referenced this pull request Sep 9, 2026
Generated setters and DefaultValue metadata now pass the cached box for bool,
int and double instead of boxing on every set and every registration.

Boxes is internal to Uno.UI, so the generators check that it is actually
accessible from the compilation and fall back to the plain value otherwise -
any other consumer of [GeneratedDependencyProperty] keeps compiling.

Supersedes unoplatform#24242.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01BDvvr5VMr8KxRygFWfc1q4
Co-Authored-By: Youssef Victor <[email protected]>
MartinZikmund added a commit to Youssef1313/uno that referenced this pull request Sep 16, 2026
Generated setters and DefaultValue metadata now pass the cached box for bool,
int and double instead of boxing on every set and every registration.

Boxes is internal to Uno.UI, so the generators check that it is actually
accessible from the compilation and fall back to the plain value otherwise -
any other consumer of [GeneratedDependencyProperty] keeps compiling.

Supersedes unoplatform#24242.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01BDvvr5VMr8KxRygFWfc1q4
Co-Authored-By: Youssef Victor <[email protected]>
MartinZikmund added a commit that referenced this pull request Sep 18, 2026
Generated setters and DefaultValue metadata now pass the cached box for bool,
int and double instead of boxing on every set and every registration.

Boxes is internal to Uno.UI, so the generators check that it is actually
accessible from the compilation and fall back to the plain value otherwise -
any other consumer of [GeneratedDependencyProperty] keeps compiling.

Supersedes #24242.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01BDvvr5VMr8KxRygFWfc1q4
Co-Authored-By: Youssef Victor <[email protected]>
MartinZikmund added a commit that referenced this pull request Sep 24, 2026
Generated setters and DefaultValue metadata now pass the cached box for bool,
int and double instead of boxing on every set and every registration.

Boxes is internal to Uno.UI, so the generators check that it is actually
accessible from the compilation and fall back to the plain value otherwise -
any other consumer of [GeneratedDependencyProperty] keeps compiling.

Supersedes #24242.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01BDvvr5VMr8KxRygFWfc1q4
Co-Authored-By: Youssef Victor <[email protected]>
MartinZikmund added a commit to Youssef1313/uno that referenced this pull request Sep 24, 2026
Generated setters and DefaultValue metadata now pass the cached box for bool,
int and double instead of boxing on every set and every registration.

Boxes is internal to Uno.UI, so the generators check that it is actually
accessible from the compilation and fall back to the plain value otherwise -
any other consumer of [GeneratedDependencyProperty] keeps compiling.

Supersedes unoplatform#24242.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01BDvvr5VMr8KxRygFWfc1q4
Co-Authored-By: Youssef Victor <[email protected]>
MartinZikmund added a commit that referenced this pull request Sep 25, 2026
Generated setters and DefaultValue metadata now pass the cached box for bool,
int and double instead of boxing on every set and every registration.

Boxes is internal to Uno.UI, so the generators check that it is actually
accessible from the compilation and fall back to the plain value otherwise -
any other consumer of [GeneratedDependencyProperty] keeps compiling.

Supersedes #24242.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01BDvvr5VMr8KxRygFWfc1q4
Co-Authored-By: Youssef Victor <[email protected]>
MartinZikmund added a commit to Youssef1313/uno that referenced this pull request Sep 27, 2026
Generated setters and DefaultValue metadata now pass the cached box for bool,
int and double instead of boxing on every set and every registration.

Boxes is internal to Uno.UI, so the generators check that it is actually
accessible from the compilation and fall back to the plain value otherwise -
any other consumer of [GeneratedDependencyProperty] keeps compiling.

Supersedes unoplatform#24242.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01BDvvr5VMr8KxRygFWfc1q4
Co-Authored-By: Youssef Victor <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/code-generation Categorizes an issue or PR as relevant to code generation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated bool DependencyProperty setters box on every set

5 participants