Skip to content

Avoid boxing booleans and allocations in UncommonField<bool> #10681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -10,56 +10,66 @@ namespace System.Windows
internal class UncommonField<T>
{
/// <summary>
/// Create a new UncommonField.
/// Zero-based, globally unique index, retrieved via <see cref="DependencyProperty.GetUniqueGlobalIndex"/>.
/// </summary>
public UncommonField() : this(default(T))
internal int GlobalIndex { get; }

/// <summary>
/// Determines the default value for this field, assigned during initial construction.
/// </summary>
private readonly T _defaultValue;
/// <summary>
/// An optimization detail representing whether a non-default value is currently stored.
/// </summary>
private bool _hasBeenSet;

/// <summary>
/// Create a new UncommonField. Stores <see langword="default"/>(<typeparamref name="T"/>) value as default.
/// </summary>
public UncommonField() : this(default)
{
}

/// <summary>
/// Create a new UncommonField.
/// Create a new UncommonField.
/// </summary>
/// <param name="defaultValue">The default value of the field.</param>
public UncommonField(T defaultValue)
{
_defaultValue = defaultValue;
_hasBeenSet = false;

lock (DependencyProperty.Synchronized)
{
_globalIndex = DependencyProperty.GetUniqueGlobalIndex(null, null);
GlobalIndex = DependencyProperty.GetUniqueGlobalIndex(null, null);

DependencyProperty.RegisteredPropertyList.Add();
}
}

/// <summary>
/// Write the given value onto a DependencyObject instance.
/// Write the given value onto a DependencyObject instance.
/// </summary>
/// <param name="instance">The DependencyObject on which to set the value.</param>
/// <param name="value">The value to set.</param>
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<T, bool>(ref value));
}
else
{
valueObject = value;
}
// Use shared boxed instances rather than creating new objects for each SetValue call.
object valueObject = BooleanBoxes.Box(Unsafe.BitCast<T, bool>(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
Expand All @@ -69,7 +79,7 @@ public void SetValue(DependencyObject instance, T value)
}

/// <summary>
/// Read the value of this field on a DependencyObject instance.
/// Read the value of this field on a DependencyObject instance.
/// </summary>
/// <param name="instance">The DependencyObject from which to get the value.</param>
/// <returns></returns>
Expand All @@ -79,7 +89,7 @@ public T GetValue(DependencyObject instance)

if (_hasBeenSet)
{
EntryIndex entryIndex = instance.LookupEntry(_globalIndex);
EntryIndex entryIndex = instance.LookupEntry(GlobalIndex);

if (entryIndex.Found)
{
Expand All @@ -100,32 +110,16 @@ public T GetValue(DependencyObject instance)


/// <summary>
/// Clear this field from the given DependencyObject instance.
/// Clear this field from the given DependencyObject instance.
/// </summary>
/// <param name="instance"></param>
/// <param name="instance">An instance on which to clear the value from.</param>
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
}
}