Skip to content
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

Reuse bool box objects in UncommonField<bool>.SetValue #6528

Merged
merged 2 commits into from Jun 3, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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
Expand Down Expand Up @@ -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<T>.Default.Equals(value, _defaultValue))
{
instance.SetEffectiveValue(entryIndex, null /* dp */, _globalIndex, null /* metadata */, value, BaseValueSourceInternal.Local);
object valueObject;

if (typeof(T) == typeof(bool))
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
{
// There are only two possible values. Use shared boxed instances rather than creating new objects for each SetValue call.
valueObject = Unsafe.As<T, bool>(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
Expand Down Expand Up @@ -136,6 +146,11 @@ internal int GlobalIndex
private int _globalIndex;
private bool _hasBeenSet;

/// <summary>A lazily boxed false value.</summary>
private static object s_false;
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>A lazily boxed true value.</summary>
private static object s_true;

#endregion
}
}