Skip to content

Reduce boolean boxes across the framework - #35596

Merged
kubaflo merged 11 commits into
dotnet:inflight/currentfrom
pictos:pj/avoid-bool-boxing
Jun 14, 2026
Merged

Reduce boolean boxes across the framework#35596
kubaflo merged 11 commits into
dotnet:inflight/currentfrom
pictos:pj/avoid-bool-boxing

Conversation

@pictos

@pictos pictos commented May 23, 2026

Copy link
Copy Markdown
Contributor

Description of Change

This is a follow up from #35303, that implements the BooleanBoxes class and use inside the Control project

Use the same solution that the WPF team uses, cache the boxed true and false values, and use it instead of boxing a new boolean on every operation.

Issues Fixed

Relates with #35302

@github-actions

github-actions Bot commented May 23, 2026

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 35596

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 35596"
@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label May 23, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @@pictos! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@github-actions github-actions Bot added area-controls-general General issues that span multiple controls, or common base classes such as View or Element platform/android platform/ios platform/tizen Samsung Tizen Devices (TV) labels May 23, 2026
@kubaflo

kubaflo commented May 24, 2026

Copy link
Copy Markdown
Collaborator

/review -b feature/refactor-copilot-yml

@MauiBot MauiBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Expert Review — 1 findings

See inline comments for details.

global using Microsoft.Maui;
global using Microsoft.Maui.Handlers;
global using Microsoft.Maui.Platform;
global using Microsoft.Maui.Controls.Internals;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[critical] Platform-Specific Code Scoping / Build - Adding Microsoft.Maui.Controls.Internals as a global using imports every internal Controls type into every file in this project. On iOS files that also use Foundation and unqualified [Preserve], this makes PreserveAttribute ambiguous between Foundation.PreserveAttribute and Microsoft.Maui.Controls.Internals.PreserveAttribute, causing the iOS build to fail. Please keep BooleanBoxes references explicit, use a targeted alias/static using, or add local usings only where needed instead of globally importing the whole Internals namespace.

@MauiBot MauiBot added s/agent-review-incomplete s/agent-fix-win AI found a better alternative fix than the PR s/agent-reviewed PR was reviewed by AI agent workflow (full 4-phase review) labels May 24, 2026
@kubaflo

kubaflo commented May 24, 2026

Copy link
Copy Markdown
Collaborator

/review -b feature/refactor-copilot-yml

@MauiBot MauiBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Expert Review — 1 findings

See inline comments for details.

Comment thread src/Controls/src/Core/GlobalNamespaces.cs Outdated
@kubaflo

kubaflo commented May 25, 2026

Copy link
Copy Markdown
Collaborator

/review -b feature/refactor-copilot-yml

MauiBot

This comment was marked as outdated.

@pictos

pictos commented May 30, 2026

Copy link
Copy Markdown
Contributor Author

I don't think the bot suggestion makes sense. I believe to have one API is better

pictos and others added 8 commits June 13, 2026 23:05
…ols.Core

Replace raw true/false literals with BooleanBoxes.TrueBox/FalseBox in
SetValue and SetValueFromRenderer calls for bool-typed BindableProperties.
Added using Microsoft.Maui.Controls.Internals to ShellPageRendererTracker.cs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ties in Controls.Core

Replace raw true/false/default(bool) literals with BooleanBoxes.TrueBox/FalseBox
in BindableProperty.CreateAttached and CreateAttachedReadOnly calls for
bool-typed properties. Added using Microsoft.Maui.Controls.Internals to
6 files that did not already have it.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… Controls.Core

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…cator

The global using Microsoft.Maui.Controls.Internals caused CS0104 ambiguous
reference errors for PreserveAttribute in iOS files that also import Foundation.
Remove the global using and add an explicit using to the one file that was
missing it (ActivityIndicator.cs).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…boxing

All public bool property setters that previously called SetValue(Prop, value)
now call SetValue(Prop, BooleanBoxes.Box(value)) to eliminate per-assignment
heap allocations. This completes the boxing optimization across:
- VisualElement (IsVisible, IsEnabled, InputTransparent, IsFocused)
- CheckBox, Switch, RadioButton (IsChecked, IsToggled)
- ActivityIndicator, RefreshView, ListView, CarouselView, and others
- SetterSpecificity overloads (FromHandler) also updated

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Add BooleanBoxesProductionTests class with Assert.Same checks on:
  * DefaultValue for representative BindableProperties
  * GetValue() after setter calls (verifies cached box is stored)
- Fix CoerceIsEnabledProperty and CoerceInputTransparentProperty in
  VisualElement to return BooleanBoxes.Box(...) / BooleanBoxes.FalseBox
  instead of raw bool literals (which would allocate a new box)
- Fix CoerceIsEnabledProperty in MenuItem similarly

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@kubaflo

kubaflo commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).
@kubaflo
kubaflo merged commit 22c02a2 into dotnet:inflight/current Jun 14, 2026
4 of 21 checks passed
@github-actions github-actions Bot added this to the .NET 10 SR9 milestone Jun 14, 2026
@pictos
pictos deleted the pj/avoid-bool-boxing branch June 15, 2026 11:18
PureWeen pushed a commit that referenced this pull request Jun 22, 2026
<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Description of Change

<!-- Enter description of the fix in this section -->

This is a follow up from #35303, that implements the `BooleanBoxes`
class and use inside the `Control` project

[Use the same solution that the WPF team
uses](https://github.com/dotnet/wpf/blob/b325a186ebcead7fddfde43c118f6a807397de35/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/KnownBoxes.cs#L10),
cache the boxed `true` and `false` values, and use it instead of boxing
a new boolean on every operation.

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Relates with #35302

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Pedro Jesus <pedrojesus@Pedros-MacBook-Pro.local>
kubaflo pushed a commit that referenced this pull request Jun 25, 2026
<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Description of Change

<!-- Enter description of the fix in this section -->

This is a follow up from #35303, that implements the `BooleanBoxes`
class and use inside the `Control` project

[Use the same solution that the WPF team
uses](https://github.com/dotnet/wpf/blob/b325a186ebcead7fddfde43c118f6a807397de35/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/KnownBoxes.cs#L10),
cache the boxed `true` and `false` values, and use it instead of boxing
a new boolean on every operation.

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Relates with #35302

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Pedro Jesus <pedrojesus@Pedros-MacBook-Pro.local>
kubaflo pushed a commit that referenced this pull request Jul 3, 2026
<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Description of Change

<!-- Enter description of the fix in this section -->

This is a follow up from #35303, that implements the `BooleanBoxes`
class and use inside the `Control` project

[Use the same solution that the WPF team
uses](https://github.com/dotnet/wpf/blob/b325a186ebcead7fddfde43c118f6a807397de35/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/KnownBoxes.cs#L10),
cache the boxed `true` and `false` values, and use it instead of boxing
a new boolean on every operation.

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Relates with #35302

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Pedro Jesus <pedrojesus@Pedros-MacBook-Pro.local>
@kubaflo kubaflo mentioned this pull request Jul 6, 2026
kubaflo pushed a commit that referenced this pull request Jul 6, 2026
<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

<!-- Enter description of the fix in this section -->

This is a follow up from #35303, that implements the `BooleanBoxes`
class and use inside the `Control` project

[Use the same solution that the WPF team
uses](https://github.com/dotnet/wpf/blob/b325a186ebcead7fddfde43c118f6a807397de35/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/KnownBoxes.cs#L10),
cache the boxed `true` and `false` values, and use it instead of boxing
a new boolean on every operation.

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Relates with #35302

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Pedro Jesus <pedrojesus@Pedros-MacBook-Pro.local>
devanathan-vaithiyanathan pushed a commit to devanathan-vaithiyanathan/maui that referenced this pull request Jul 7, 2026
<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

<!-- Enter description of the fix in this section -->

This is a follow up from dotnet#35303, that implements the `BooleanBoxes`
class and use inside the `Control` project

[Use the same solution that the WPF team
uses](https://github.com/dotnet/wpf/blob/b325a186ebcead7fddfde43c118f6a807397de35/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/KnownBoxes.cs#L10),
cache the boxed `true` and `false` values, and use it instead of boxing
a new boolean on every operation.

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Relates with dotnet#35302

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Pedro Jesus <pedrojesus@Pedros-MacBook-Pro.local>
PureWeen pushed a commit that referenced this pull request Jul 7, 2026
<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

<!-- Enter description of the fix in this section -->

This is a follow up from #35303, that implements the `BooleanBoxes`
class and use inside the `Control` project

[Use the same solution that the WPF team
uses](https://github.com/dotnet/wpf/blob/b325a186ebcead7fddfde43c118f6a807397de35/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/KnownBoxes.cs#L10),
cache the boxed `true` and `false` values, and use it instead of boxing
a new boolean on every operation.

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Relates with #35302

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Pedro Jesus <pedrojesus@Pedros-MacBook-Pro.local>
PureWeen pushed a commit that referenced this pull request Jul 7, 2026
<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

<!-- Enter description of the fix in this section -->

This is a follow up from #35303, that implements the `BooleanBoxes`
class and use inside the `Control` project

[Use the same solution that the WPF team
uses](https://github.com/dotnet/wpf/blob/b325a186ebcead7fddfde43c118f6a807397de35/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/KnownBoxes.cs#L10),
cache the boxed `true` and `false` values, and use it instead of boxing
a new boolean on every operation.

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Relates with #35302

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Pedro Jesus <pedrojesus@Pedros-MacBook-Pro.local>
devanathan-vaithiyanathan pushed a commit to devanathan-vaithiyanathan/maui that referenced this pull request Jul 8, 2026
<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

<!-- Enter description of the fix in this section -->

This is a follow up from dotnet#35303, that implements the `BooleanBoxes`
class and use inside the `Control` project

[Use the same solution that the WPF team
uses](https://github.com/dotnet/wpf/blob/b325a186ebcead7fddfde43c118f6a807397de35/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/KnownBoxes.cs#L10),
cache the boxed `true` and `false` values, and use it instead of boxing
a new boolean on every operation.

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Relates with dotnet#35302

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Pedro Jesus <pedrojesus@Pedros-MacBook-Pro.local>
kubaflo pushed a commit that referenced this pull request Jul 10, 2026
<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

<!-- Enter description of the fix in this section -->

This is a follow up from #35303, that implements the `BooleanBoxes`
class and use inside the `Control` project

[Use the same solution that the WPF team
uses](https://github.com/dotnet/wpf/blob/b325a186ebcead7fddfde43c118f6a807397de35/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/KnownBoxes.cs#L10),
cache the boxed `true` and `false` values, and use it instead of boxing
a new boolean on every operation.

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Relates with #35302

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Pedro Jesus <pedrojesus@Pedros-MacBook-Pro.local>
kubaflo pushed a commit that referenced this pull request Jul 15, 2026
<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

<!-- Enter description of the fix in this section -->

This is a follow up from #35303, that implements the `BooleanBoxes`
class and use inside the `Control` project

[Use the same solution that the WPF team
uses](https://github.com/dotnet/wpf/blob/b325a186ebcead7fddfde43c118f6a807397de35/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/KnownBoxes.cs#L10),
cache the boxed `true` and `false` values, and use it instead of boxing
a new boolean on every operation.

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Relates with #35302

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Pedro Jesus <pedrojesus@Pedros-MacBook-Pro.local>
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-controls-general General issues that span multiple controls, or common base classes such as View or Element community ✨ Community Contribution platform/android platform/ios platform/tizen Samsung Tizen Devices (TV) s/agent-fix-win AI found a better alternative fix than the PR s/agent-reviewed PR was reviewed by AI agent workflow (full 4-phase review)

3 participants