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

Speed up interface checking and casting #49257

Merged
merged 5 commits into from
Mar 7, 2021
Merged
Changes from 2 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
Expand Up @@ -210,31 +210,59 @@ private static CastResult TryGet(nuint source, nuint target)
if (obj != null)
{
MethodTable* mt = RuntimeHelpers.GetMethodTable(obj);
nuint interfaceCount = mt->InterfaceCount;
nint interfaceCount = mt->InterfaceCount;
if (interfaceCount != 0)
{
MethodTable** interfaceMap = mt->InterfaceMap;
for (nuint i = 0; ; i += 4)
if (interfaceCount >= 4)
benaadams marked this conversation as resolved.
Show resolved Hide resolved
{
if (interfaceMap[i + 0] == toTypeHnd)
goto done;
if (--interfaceCount == 0)
break;
if (interfaceMap[i + 1] == toTypeHnd)
goto done;
if (--interfaceCount == 0)
break;
if (interfaceMap[i + 2] == toTypeHnd)
goto done;
if (--interfaceCount == 0)
break;
if (interfaceMap[i + 3] == toTypeHnd)
// If not enough for unrolled, jmp straight to small loop
// as we already know there is one or more interfaces so don't need to check again.
goto few;
}

do
{
// Calculate next offset now to hide latency
benaadams marked this conversation as resolved.
Show resolved Hide resolved
MethodTable** map = interfaceMap + 4;

if (interfaceMap[0] == toTypeHnd ||
interfaceMap[1] == toTypeHnd ||
interfaceMap[2] == toTypeHnd ||
interfaceMap[3] == toTypeHnd)
benaadams marked this conversation as resolved.
Show resolved Hide resolved
{
goto done;
if (--interfaceCount == 0)
break;
}

// Assign next offset
interfaceMap = map;
interfaceCount -= 4;
} while (interfaceCount >= 4);

if (interfaceCount == 0)
{
// If none remaining, skip the short loop
goto extra;
}

few:
do
{
// Calculate next offset now to hide latency
MethodTable** map = interfaceMap + 1;
benaadams marked this conversation as resolved.
Show resolved Hide resolved

if (interfaceMap[0] == toTypeHnd)
{
goto done;
}

// Assign next offset
interfaceMap = map;
interfaceCount--;
} while (interfaceCount > 0);
}

extra:
if (mt->NonTrivialInterfaceCast)
{
goto slowPath;
Expand All @@ -255,10 +283,14 @@ private static CastResult TryGet(nuint source, nuint target)
[DebuggerStepThrough]
private static object? IsInstanceOfClass(void* toTypeHnd, object? obj)
{
if (obj == null || RuntimeHelpers.GetMethodTable(obj) == toTypeHnd)
if (obj == null)
return obj;

MethodTable* mt = RuntimeHelpers.GetMethodTable(obj);
benaadams marked this conversation as resolved.
Show resolved Hide resolved
if (mt == toTypeHnd)
return obj;

MethodTable* mt = RuntimeHelpers.GetMethodTable(obj)->ParentMethodTable;
mt = mt->ParentMethodTable;
for (; ; )
{
if (mt == toTypeHnd)
Expand Down Expand Up @@ -377,32 +409,61 @@ private static CastResult TryGet(nuint source, nuint target)
if (obj != null)
{
MethodTable* mt = RuntimeHelpers.GetMethodTable(obj);
nuint interfaceCount = mt->InterfaceCount;
nint interfaceCount = mt->InterfaceCount;
if (interfaceCount == 0)
{
goto slowPath;
}

MethodTable** interfaceMap = mt->InterfaceMap;
for (nuint i = 0; ; i += 4)
if (interfaceCount >= 4)
{
if (interfaceMap[i + 0] == toTypeHnd)
goto done;
if (--interfaceCount == 0)
goto slowPath;
if (interfaceMap[i + 1] == toTypeHnd)
goto done;
if (--interfaceCount == 0)
goto slowPath;
if (interfaceMap[i + 2] == toTypeHnd)
goto done;
if (--interfaceCount == 0)
goto slowPath;
if (interfaceMap[i + 3] == toTypeHnd)
// If not enough for unrolled, jmp straight to small loop
// as we already know there is one or more interfaces so don't need to check again.
goto few;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If interface table is allocated as 4 elements aligned and zero-filled, we could remove the "few" case entirely. At least in Chk case which is expected to succeed.
That would come with size increase though, so does not seem worth it.

}

do
{
// Calculate next offset now to hide latency
MethodTable** map = interfaceMap + 4;

if (interfaceMap[0] == toTypeHnd ||
interfaceMap[1] == toTypeHnd ||
interfaceMap[2] == toTypeHnd ||
interfaceMap[3] == toTypeHnd)
{
goto done;
if (--interfaceCount == 0)
goto slowPath;
}

// Assign next offset
interfaceMap = map;
interfaceCount -= 4;
} while (interfaceCount >= 4);

if (interfaceCount == 0)
{
// If none remaining, skip the short loop
goto slowPath;
}

few:
do
{
// Calculate next offset now to hide latency
MethodTable** map = interfaceMap + 1;

if (interfaceMap[0] == toTypeHnd)
{
goto done;
}

// Assign next offset
interfaceMap = map;
interfaceCount--;
} while (interfaceCount > 0);

goto slowPath;
}

done:
Expand Down