Skip to content

Commit

Permalink
Fix FrozenDictionary/Set handling of ValueTuple keys (#84280)
Browse files Browse the repository at this point in the history
For small collections of comparable value types, we were using an implementation that sorted the keys in order to a) quickly rule out values outside of the known contained range, and b) stop searching when we hit a value that was too small.  However, this can break for some well-known types. In particular, `ValueTuple<...>` implements `IComparable<ValueTuple<...>>`, but it might throw an exception if you actually try to use its `IComparable<>` implementation if any of the `T` types in the tuple are themselves not comparable.  Since we have no good way then to dynamically select an implementation based on whether it implements `IComparable<>`, I've simply removed those checks / calls from the implementation.

Testing this also highlighted that our existing shared set tests don't like being given non-comparable types, as they use SortedSet which itself suffers from effectively the same issue (but there you can choose to not use SortedSet if it doesn't work for your data, and "sort"ing is part of the name).
  • Loading branch information
stephentoub committed Apr 4, 2023
1 parent 149e43e commit e579ccb
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ public void ISet_Generic_NullEnumerableArgument(int count)
}
}

public static IEnumerable<object[]> SetTestData() =>
new[] { EnumerableType.HashSet, EnumerableType.List }.SelectMany(GetEnumerableTestData);

[Theory]
[MemberData(nameof(EnumerableTestData))]
public void ISet_Generic_ExceptWith(EnumerableType enumerableType, int setLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements)
Expand All @@ -350,7 +353,7 @@ public void ISet_Generic_IntersectWith(EnumerableType enumerableType, int setLen
}

[Theory]
[MemberData(nameof(EnumerableTestData))]
[MemberData(nameof(SetTestData))]
public void ISet_Generic_IsProperSubsetOf(EnumerableType enumerableType, int setLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements)
{
ISet<T> set = GenericISetFactory(setLength);
Expand All @@ -359,7 +362,7 @@ public void ISet_Generic_IsProperSubsetOf(EnumerableType enumerableType, int set
}

[Theory]
[MemberData(nameof(EnumerableTestData))]
[MemberData(nameof(SetTestData))]
public void ISet_Generic_IsProperSupersetOf(EnumerableType enumerableType, int setLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements)
{
ISet<T> set = GenericISetFactory(setLength);
Expand All @@ -368,7 +371,7 @@ public void ISet_Generic_IsProperSupersetOf(EnumerableType enumerableType, int s
}

[Theory]
[MemberData(nameof(EnumerableTestData))]
[MemberData(nameof(SetTestData))]
public void ISet_Generic_IsSubsetOf(EnumerableType enumerableType, int setLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements)
{
ISet<T> set = GenericISetFactory(setLength);
Expand All @@ -377,7 +380,7 @@ public void ISet_Generic_IsSubsetOf(EnumerableType enumerableType, int setLength
}

[Theory]
[MemberData(nameof(EnumerableTestData))]
[MemberData(nameof(SetTestData))]
public void ISet_Generic_IsSupersetOf(EnumerableType enumerableType, int setLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements)
{
ISet<T> set = GenericISetFactory(setLength);
Expand All @@ -386,7 +389,7 @@ public void ISet_Generic_IsSupersetOf(EnumerableType enumerableType, int setLeng
}

[Theory]
[MemberData(nameof(EnumerableTestData))]
[MemberData(nameof(SetTestData))]
public void ISet_Generic_Overlaps(EnumerableType enumerableType, int setLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements)
{
ISet<T> set = GenericISetFactory(setLength);
Expand All @@ -395,7 +398,7 @@ public void ISet_Generic_Overlaps(EnumerableType enumerableType, int setLength,
}

[Theory]
[MemberData(nameof(EnumerableTestData))]
[MemberData(nameof(SetTestData))]
public void ISet_Generic_SetEquals(EnumerableType enumerableType, int setLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements)
{
ISet<T> set = GenericISetFactory(setLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static IEnumerable<object[]> EnumerableTestData() =>
public static IEnumerable<object[]> ListTestData() =>
GetEnumerableTestData(EnumerableType.List);

private static IEnumerable<object[]> GetEnumerableTestData(EnumerableType enumerableType)
protected static IEnumerable<object[]> GetEnumerableTestData(EnumerableType enumerableType)
{
foreach (object[] collectionSizeArray in ValidCollectionSizes())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ The System.Collections.Immutable library is built-in as part of the shared frame
<Compile Include="System\Collections\Frozen\KeysAndValuesFrozenDictionary.cs" />
<Compile Include="System\Collections\Frozen\SmallFrozenDictionary.cs" />
<Compile Include="System\Collections\Frozen\SmallFrozenSet.cs" />
<Compile Include="System\Collections\Frozen\SmallComparableValueTypeFrozenDictionary.cs" />
<Compile Include="System\Collections\Frozen\SmallComparableValueTypeFrozenSet.cs" />
<Compile Include="System\Collections\Frozen\SmallValueTypeDefaultComparerFrozenDictionary.cs" />
<Compile Include="System\Collections\Frozen\SmallValueTypeDefaultComparerFrozenSet.cs" />
<Compile Include="System\Collections\Frozen\ValueTypeDefaultComparerFrozenDictionary.cs" />
<Compile Include="System\Collections\Frozen\ValueTypeDefaultComparerFrozenSet.cs" />
<Compile Include="System\Collections\Frozen\Int32\Int32FrozenDictionary.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ internal static class Constants
/// SmallValueTypeDefaultComparerFrozenDictionary/Set to the
/// hashing ValueTypeDefaultComparerFrozenDictionary/Set.
/// </remarks>
public const int MaxItemsInSmallComparableValueTypeFrozenCollection = 10;
public const int MaxItemsInSmallValueTypeFrozenCollection = 10;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,9 @@ private static FrozenDictionary<TKey, TValue> ChooseImplementationOptimizedForRe
// the Equals/GetHashCode methods to be devirtualized and possibly inlined.
if (ReferenceEquals(comparer, EqualityComparer<TKey>.Default))
{
if (default(TKey) is IComparable<TKey> &&
source.Count <= Constants.MaxItemsInSmallComparableValueTypeFrozenCollection)
if (source.Count <= Constants.MaxItemsInSmallValueTypeFrozenCollection)
{
return (FrozenDictionary<TKey, TValue>)(object)new SmallComparableValueTypeFrozenDictionary<TKey, TValue>(source);
return (FrozenDictionary<TKey, TValue>)(object)new SmallValueTypeDefaultComparerFrozenDictionary<TKey, TValue>(source);
}

if (typeof(TKey) == typeof(int))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,9 @@ private static FrozenSet<T> ChooseImplementationOptimizedForReading<T>(HashSet<T
// the Equals/GetHashCode methods to be devirtualized and possibly inlined.
if (ReferenceEquals(comparer, EqualityComparer<T>.Default))
{
if (default(T) is IComparable<T> &&
source.Count <= Constants.MaxItemsInSmallComparableValueTypeFrozenCollection)
if (source.Count <= Constants.MaxItemsInSmallValueTypeFrozenCollection)
{
return (FrozenSet<T>)(object)new SmallComparableValueTypeFrozenSet<T>(source);
return (FrozenSet<T>)(object)new SmallValueTypeDefaultComparerFrozenSet<T>(source);
}

if (typeof(T) == typeof(int))
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;

namespace System.Collections.Frozen
{
/// <summary>Provides a frozen dictionary to use when the key is a value type, the default comparer is used, and the item count is small.</summary>
internal sealed class SmallValueTypeDefaultComparerFrozenDictionary<TKey, TValue> : FrozenDictionary<TKey, TValue>
where TKey : notnull
{
private readonly TKey[] _keys;
private readonly TValue[] _values;

internal SmallValueTypeDefaultComparerFrozenDictionary(Dictionary<TKey, TValue> source) : base(EqualityComparer<TKey>.Default)
{
Debug.Assert(default(TKey) is not null);
Debug.Assert(typeof(TKey).IsValueType);

Debug.Assert(source.Count != 0);
Debug.Assert(ReferenceEquals(source.Comparer, EqualityComparer<TKey>.Default));

_keys = source.Keys.ToArray();
_values = source.Values.ToArray();
}

private protected override TKey[] KeysCore => _keys;
private protected override TValue[] ValuesCore => _values;
private protected override Enumerator GetEnumeratorCore() => new Enumerator(_keys, _values);
private protected override int CountCore => _keys.Length;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private protected override ref readonly TValue GetValueRefOrNullRefCore(TKey key)
{
TKey[] keys = _keys;
for (int i = 0; i < keys.Length; i++)
{
if (EqualityComparer<TKey>.Default.Equals(keys[i], key))
{
return ref _values[i];
}
}

return ref Unsafe.NullRef<TValue>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;

namespace System.Collections.Frozen
{
/// <summary>Provides a frozen set to use when the item is a value type, the default comparer is used, and the item count is small.</summary>
internal sealed class SmallValueTypeDefaultComparerFrozenSet<T> : FrozenSetInternalBase<T, SmallValueTypeDefaultComparerFrozenSet<T>.GSW>
{
private readonly T[] _items;

internal SmallValueTypeDefaultComparerFrozenSet(HashSet<T> source) : base(EqualityComparer<T>.Default)
{
Debug.Assert(default(T) is not null);
Debug.Assert(typeof(T).IsValueType);

Debug.Assert(source.Count != 0);
Debug.Assert(ReferenceEquals(source.Comparer, EqualityComparer<T>.Default));

_items = source.ToArray();
}

private protected override T[] ItemsCore => _items;
private protected override Enumerator GetEnumeratorCore() => new Enumerator(_items);
private protected override int CountCore => _items.Length;

private protected override int FindItemIndex(T item)
{
T[] items = _items;
for (int i = 0; i < items.Length; i++)
{
if (EqualityComparer<T>.Default.Equals(item, items[i]))
{
return i;
}
}

return -1;
}

internal struct GSW : IGenericSpecializedWrapper
{
private SmallValueTypeDefaultComparerFrozenSet<T> _set;
public void Store(FrozenSet<T> set) => _set = (SmallValueTypeDefaultComparerFrozenSet<T>)set;

public int Count => _set.Count;
public IEqualityComparer<T> Comparer => _set.Comparer;
public int FindItemIndex(T item) => _set.FindItemIndex(item);
public Enumerator GetEnumerator() => _set.GetEnumerator();
}
}
}
Loading

0 comments on commit e579ccb

Please sign in to comment.