Repository navigation
Skip EqualityComparer<TKey>.Default creation in Dictionary .ctor - #31629
Conversation
…neric/Dictionary.cs Co-Authored-By: Stephen Toub <[email protected]>
|
Maybe instead the |
For if (typeof(TKey) == typeof(string) && _comparer == null)
{
// To start, move off default comparer for string which is randomised
_comparer = (IEqualityComparer<TKey>)NonRandomizedStringEqualityComparer.Default;
}Other types would use the default comparer when |
|
Sure, so the ctor would just do: if (capacity < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity);
if (capacity > 0) Initialize(capacity);
if (typeof(TKey) == typeof(string))
{
_comparer = (IEqualityComparer<TKey>)NonRandomizedStringEqualityComparer.Default;
}No? Maybe it's not better, just making sure we considered it. |
|
Meh, what you have is probably better. |
|
Was mostly thinking about generic instantiation size; otherwise having the second .ctor would be better |
|
The failures are known issue (looks like a bad PR got merged). |
Saw this in startup checking if the comparer is
EqualityComparer<TKey>.Defaultfor string instantiates the static comparer, however by default the value isnull; so check againstnullbefore checking thestatic.e.g. in
AppContext.SetupBefore
After
Note it doesn't use the created comparer as it switches to
NonRandomizedStringEqualityComparer/cc @jkotas