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 89757905d9c..d858a0316f6 100644 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/UncommonField.cs +++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/UncommonField.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -10,31 +10,44 @@ namespace System.Windows internal class UncommonField { /// - /// Create a new UncommonField. + /// Zero-based, globally unique index, retrieved via . /// - public UncommonField() : this(default(T)) + internal int GlobalIndex { get; } + + /// + /// Determines the default value for this field, assigned during initial construction. + /// + private readonly T _defaultValue; + /// + /// An optimization detail representing whether a non-default value is currently stored. + /// + private bool _hasBeenSet; + + /// + /// Create a new UncommonField. Stores () value as default. + /// + public UncommonField() : this(default) { } /// - /// Create a new UncommonField. + /// Create a new UncommonField. /// /// The default value of the field. public UncommonField(T defaultValue) { _defaultValue = defaultValue; - _hasBeenSet = false; lock (DependencyProperty.Synchronized) { - _globalIndex = DependencyProperty.GetUniqueGlobalIndex(null, null); + GlobalIndex = DependencyProperty.GetUniqueGlobalIndex(null, null); DependencyProperty.RegisteredPropertyList.Add(); } } /// - /// Write the given value onto a DependencyObject instance. + /// Write the given value onto a DependencyObject instance. /// /// The DependencyObject on which to set the value. /// The value to set. @@ -42,24 +55,21 @@ public void SetValue(DependencyObject instance, T value) { ArgumentNullException.ThrowIfNull(instance); - EntryIndex entryIndex = instance.LookupEntry(_globalIndex); + EntryIndex entryIndex = instance.LookupEntry(GlobalIndex); - // Set the value if it's not the default, otherwise remove the value. - if (!object.ReferenceEquals(value, _defaultValue)) + // Special case boolean operations to avoid boxing with (mostly) UIA code paths + if (typeof(T) == typeof(bool)) { - object valueObject; - - if (typeof(T) == typeof(bool)) - { - // Use shared boxed instances rather than creating new objects for each SetValue call. - valueObject = BooleanBoxes.Box(Unsafe.As(ref value)); - } - else - { - valueObject = value; - } + // Use shared boxed instances rather than creating new objects for each SetValue call. + object valueObject = BooleanBoxes.Box(Unsafe.BitCast(value)); - instance.SetEffectiveValue(entryIndex, dp: null, _globalIndex, metadata: null, valueObject, BaseValueSourceInternal.Local); + instance.SetEffectiveValue(entryIndex, dp: null, GlobalIndex, metadata: null, valueObject, BaseValueSourceInternal.Local); + _hasBeenSet = true; + } + // Set the value if it's not the default, otherwise remove the value. + else if (!ReferenceEquals(value, _defaultValue)) + { + instance.SetEffectiveValue(entryIndex, dp: null, GlobalIndex, metadata: null, value, BaseValueSourceInternal.Local); _hasBeenSet = true; } else @@ -69,7 +79,7 @@ public void SetValue(DependencyObject instance, T value) } /// - /// Read the value of this field on a DependencyObject instance. + /// Read the value of this field on a DependencyObject instance. /// /// The DependencyObject from which to get the value. /// @@ -79,7 +89,7 @@ public T GetValue(DependencyObject instance) if (_hasBeenSet) { - EntryIndex entryIndex = instance.LookupEntry(_globalIndex); + EntryIndex entryIndex = instance.LookupEntry(GlobalIndex); if (entryIndex.Found) { @@ -100,32 +110,16 @@ public T GetValue(DependencyObject instance) /// - /// Clear this field from the given DependencyObject instance. + /// Clear this field from the given DependencyObject instance. /// - /// + /// An instance on which to clear the value from. public void ClearValue(DependencyObject instance) { ArgumentNullException.ThrowIfNull(instance); - EntryIndex entryIndex = instance.LookupEntry(_globalIndex); + EntryIndex entryIndex = instance.LookupEntry(GlobalIndex); instance.UnsetEffectiveValue(entryIndex, dp: null, metadata: null); } - - internal int GlobalIndex - { - get - { - return _globalIndex; - } - } - - #region Private Fields - - private T _defaultValue; - private int _globalIndex; - private bool _hasBeenSet; - - #endregion } }