From dc928b85956853d28871a95e247e03f2bfd7157b Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 4 May 2022 14:09:09 -0400 Subject: [PATCH 1/2] Reuse bool box objects in UncommonField.SetValue In some scenarios, these UncommonFields end up being set very frequently, and each time the value being passed in gets boxed. We can avoid that by caching and reusing a box for each of the only two possible values (though it looks like the default value is almost always false and so it mainly only boxes true). --- .../System/Windows/UncommonField.cs | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/UncommonField.cs b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/UncommonField.cs index c01e87488ec..952cc302890 100644 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/UncommonField.cs +++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/UncommonField.cs @@ -2,10 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.Collections.Generic; -using System.Diagnostics; - +using System.Runtime.CompilerServices; using MS.Internal.WindowsBase; // for FriendAccessAllowed namespace System.Windows @@ -54,12 +52,24 @@ public void SetValue(DependencyObject instance, T value) // Set the value if it's not the default, otherwise remove the value. if (!EqualityComparer.Default.Equals(value, _defaultValue)) { - instance.SetEffectiveValue(entryIndex, null /* dp */, _globalIndex, null /* metadata */, value, BaseValueSourceInternal.Local); + object valueObject; + + if (typeof(T) == typeof(bool)) + { + // There are only two possible values. Use shared boxed instances rather than creating new objects for each SetValue call. + valueObject = Unsafe.As(ref value) ? (s_true ??= true) : (s_false ??= false); + } + else + { + valueObject = value; + } + + instance.SetEffectiveValue(entryIndex, dp: null, _globalIndex, metadata: null, valueObject, BaseValueSourceInternal.Local); _hasBeenSet = true; } else { - instance.UnsetEffectiveValue(entryIndex, null /* dp */, null /* metadata */); + instance.UnsetEffectiveValue(entryIndex, dp: null, metadata: null); } } else @@ -136,6 +146,11 @@ internal int GlobalIndex private int _globalIndex; private bool _hasBeenSet; + /// A lazily boxed false value. + private static object s_false; + /// A lazily boxed true value. + private static object s_true; + #endregion } } From 70f0b748544c2199b280a1702989e1ef8d8ecc35 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 4 May 2022 14:21:23 -0400 Subject: [PATCH 2/2] Use BooleanBoxes --- .../src/WindowsBase/System/Windows/UncommonField.cs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/UncommonField.cs b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/UncommonField.cs index 952cc302890..fe708394d74 100644 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/UncommonField.cs +++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/UncommonField.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; +using MS.Internal.KnownBoxes; using MS.Internal.WindowsBase; // for FriendAccessAllowed namespace System.Windows @@ -56,8 +57,8 @@ public void SetValue(DependencyObject instance, T value) if (typeof(T) == typeof(bool)) { - // There are only two possible values. Use shared boxed instances rather than creating new objects for each SetValue call. - valueObject = Unsafe.As(ref value) ? (s_true ??= true) : (s_false ??= false); + // Use shared boxed instances rather than creating new objects for each SetValue call. + valueObject = BooleanBoxes.Box(Unsafe.As(ref value)); } else { @@ -146,11 +147,6 @@ internal int GlobalIndex private int _globalIndex; private bool _hasBeenSet; - /// A lazily boxed false value. - private static object s_false; - /// A lazily boxed true value. - private static object s_true; - #endregion } }