-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Speed up interface checking and casting #49257
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
855430a
Reduce branches in IsInstanceOfInterface/ChkCastInterface
benaadams 6617221
Drop extra var, lea; additional check for small counts
benaadams 8670404
Feedback
benaadams ef16c9b
Undo IsInstanceOfClass change
benaadams c60450e
Tidy usings
benaadams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,10 @@ | |
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using Internal.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
using System.Runtime.Intrinsics; | ||
using System.Runtime.Intrinsics.X86; | ||
|
||
using Internal.Runtime.CompilerServices; | ||
|
||
namespace System.Runtime.CompilerServices | ||
{ | ||
|
@@ -207,34 +206,57 @@ private static CastResult TryGet(nuint source, nuint target) | |
[DebuggerStepThrough] | ||
private static object? IsInstanceOfInterface(void* toTypeHnd, object? obj) | ||
{ | ||
const int unrollSize = 4; | ||
|
||
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 < unrollSize) | ||
{ | ||
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 | ||
{ | ||
if (interfaceMap[0] == toTypeHnd || | ||
interfaceMap[1] == toTypeHnd || | ||
interfaceMap[2] == toTypeHnd || | ||
interfaceMap[3] == toTypeHnd) | ||
{ | ||
goto done; | ||
if (--interfaceCount == 0) | ||
break; | ||
} | ||
|
||
interfaceMap += unrollSize; | ||
interfaceCount -= unrollSize; | ||
} while (interfaceCount >= unrollSize); | ||
|
||
if (interfaceCount == 0) | ||
{ | ||
// If none remaining, skip the short loop | ||
goto extra; | ||
} | ||
|
||
few: | ||
do | ||
{ | ||
if (interfaceMap[0] == toTypeHnd) | ||
{ | ||
goto done; | ||
} | ||
|
||
// Assign next offset | ||
interfaceMap++; | ||
interfaceCount--; | ||
} while (interfaceCount > 0); | ||
} | ||
|
||
extra: | ||
if (mt->NonTrivialInterfaceCast) | ||
{ | ||
goto slowPath; | ||
|
@@ -374,35 +396,60 @@ private static CastResult TryGet(nuint source, nuint target) | |
[DebuggerStepThrough] | ||
private static object? ChkCastInterface(void* toTypeHnd, object? obj) | ||
{ | ||
const int unrollSize = 4; | ||
|
||
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 < unrollSize) | ||
{ | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
|
||
do | ||
{ | ||
if (interfaceMap[0] == toTypeHnd || | ||
interfaceMap[1] == toTypeHnd || | ||
interfaceMap[2] == toTypeHnd || | ||
interfaceMap[3] == toTypeHnd) | ||
{ | ||
goto done; | ||
if (--interfaceCount == 0) | ||
goto slowPath; | ||
} | ||
|
||
// Assign next offset | ||
interfaceMap += unrollSize; | ||
interfaceCount -= unrollSize; | ||
} while (interfaceCount >= unrollSize); | ||
|
||
if (interfaceCount == 0) | ||
{ | ||
// If none remaining, skip the short loop | ||
goto slowPath; | ||
} | ||
|
||
few: | ||
do | ||
{ | ||
if (interfaceMap[0] == toTypeHnd) | ||
{ | ||
goto done; | ||
} | ||
|
||
// Assign next offset | ||
interfaceMap++; | ||
interfaceCount--; | ||
} while (interfaceCount > 0); | ||
|
||
goto slowPath; | ||
} | ||
|
||
done: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.