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

Special-case Dictionary for string keys #99990

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions src/coreclr/jit/importercalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3336,6 +3336,13 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis,
JITDUMP("\nExpanding RuntimeHelpers.IsKnownConstant to true early\n");
// We can also consider FTN_ADDR here
}
else if (op1->IsHelperCall() && gtIsTypeHandleToRuntimeTypeHelper(op1->AsCall()) &&
op1->AsCall()->gtArgs.GetArgByIndex(0)->GetNode()->IsIconHandle(GTF_ICON_CLASS_HDL))
{
// We don't use gtIsTypeof here because it may return true for shared generic types
retNode = gtNewIconNode(1);
JITDUMP("\nExpanding RuntimeHelpers.IsKnownConstant(typeof(T)) to true early\n");
}
else if (opts.OptimizationDisabled())
{
// It doesn't make sense to carry it as GT_INTRINSIC till Morph in Tier0
Expand Down
5 changes: 3 additions & 2 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9660,9 +9660,10 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA
assert(!optValnumCSE_phase);

JITDUMP("\nExpanding RuntimeHelpers.IsKnownConstant to ");
if (op1->OperIsConst() || gtIsTypeof(op1))
if (op1->OperIsConst())
{
// We're lucky to catch a constant here while importer was not
// We're lucky to catch a constant here while importer was not.
// typeof(T) is expected to be expanded into a nongc constant handle by now.
JITDUMP("true\n");
DEBUG_DESTROY_NODE(tree, op1);
tree = gtNewIconNode(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,73 @@ public virtual void GetObjectData(SerializationInfo info, StreamingContext conte
}
}

internal ref TValue FindValue_OrdinalComparer(string? key)
{
if (key == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
}

ref Entry entry = ref Unsafe.NullRef<Entry>();
if (_buckets != null)
{
Debug.Assert(_entries != null, "expected entries to be != null");
Debug.Assert(_comparer is NonRandomizedStringEqualityComparer.OrdinalComparer);

uint hashCode = (uint)key.GetNonRandomizedHashCode();
int i = GetBucket(hashCode);
Entry[]? entries = _entries;
uint collisionCount = 0;
i--; // Value in _buckets is 1-based; subtract 1 from i. We do it here so it fuses with the following conditional.
do
{
// Should be a while loop https://github.com/dotnet/runtime/issues/9422
// Test in if to drop range check for following array access
if ((uint)i >= (uint)entries.Length)
{
goto ReturnNotFound;
}

entry = ref entries[i];
if (entry.hashCode == hashCode && string.Equals(Unsafe.As<TKey, string>(ref entry.key), key))
{
goto ReturnFound;
}

i = entry.next;

collisionCount++;
} while (collisionCount <= (uint)entries.Length);

// The chain of entries forms a loop; which means a concurrent update has happened.
// Break out of the loop and throw, rather than looping forever.

ThrowHelper.ThrowInvalidOperationException_ConcurrentOperationsNotSupported();
}

goto ReturnNotFound;

ReturnFound:
ref TValue value = ref entry.value;
Return:
return ref value;
ReturnNotFound:
value = ref Unsafe.NullRef<TValue>();
goto Return;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ref TValue FindValue(TKey key)
{
if (RuntimeHelpers.IsKnownConstant(typeof(TKey)) && typeof(TKey) == typeof(string) &&
_comparer is NonRandomizedStringEqualityComparer.OrdinalComparer)
{
return ref FindValue_OrdinalComparer((string)(object)key);
}
return ref FindValueHelper(key);
}

internal ref TValue FindValueHelper(TKey key)
{
if (key == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void ISerializable.GetObjectData(SerializationInfo info, StreamingContext contex
info.SetType(typeof(GenericEqualityComparer<string>));
}

private sealed class OrdinalComparer : NonRandomizedStringEqualityComparer
internal sealed class OrdinalComparer : NonRandomizedStringEqualityComparer
{
internal OrdinalComparer(IEqualityComparer<string?> wrappedComparer)
: base(wrappedComparer)
Expand Down