Skip to content

Commit

Permalink
Split TryGetGenericMethodComponents into two overloads
Browse files Browse the repository at this point in the history
Resolves dotnet#83069. Hello World is now 1.45 MB, down from 1.65 MB.

Half of the callers don't care about the `MethodNameAndSignature` part - don't spend time computing it.

This also allows trimming `MethodNameAndSignature` from a hello world, which allows trimming pretty much all of the type loader.
  • Loading branch information
MichalStrehovsky committed Mar 31, 2023
1 parent a6ebab9 commit 123e2b2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -890,8 +890,7 @@ private unsafe bool TryGetMethodForOriginalLdFtnResult_InvokeMap_Inner(NativeFor
{
if ((entryFlags & InvokeTableFlags.RequiresInstArg) != 0)
{
MethodNameAndSignature dummyNameAndSignature;
bool success = TypeLoaderEnvironment.Instance.TryGetGenericMethodComponents(instantiationArgument, out declaringTypeHandle, out dummyNameAndSignature, out genericMethodTypeArgumentHandles);
bool success = TypeLoaderEnvironment.Instance.TryGetGenericMethodComponents(instantiationArgument, out declaringTypeHandle, out genericMethodTypeArgumentHandles);
Debug.Assert(success);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1101,9 +1101,8 @@ private unsafe IntPtr BuildGenericLookupTarget(TypeSystemContext typeSystemConte
if ((contextKind & GenericContextKind.FromMethodHiddenArg) != 0)
{
RuntimeTypeHandle declaringTypeHandle;
MethodNameAndSignature nameAndSignature;
RuntimeTypeHandle[] genericMethodArgHandles;
bool success = TypeLoaderEnvironment.Instance.TryGetGenericMethodComponents(context, out declaringTypeHandle, out nameAndSignature, out genericMethodArgHandles);
bool success = TypeLoaderEnvironment.Instance.TryGetGenericMethodComponents(context, out declaringTypeHandle, out genericMethodArgHandles);
Debug.Assert(success);

if (RuntimeAugments.IsGenericType(declaringTypeHandle))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ internal override bool MatchParsedEntry(ref NativeParser entryParser, ref Extern
RuntimeTypeHandle parsedDeclaringTypeHandle = externalReferencesLookup.GetRuntimeTypeHandleFromIndex(entryParser.GetUnsigned());

// Hash table names / sigs are indirected through to the native layout info
MethodNameAndSignature nameAndSignature;
if (!TypeLoaderEnvironment.Instance.TryGetMethodNameAndSignatureFromNativeLayoutOffset(moduleHandle, entryParser.GetUnsigned(), out nameAndSignature))
return false;
MethodNameAndSignature nameAndSignature = TypeLoaderEnvironment.Instance.GetMethodNameAndSignatureFromNativeLayoutOffset(moduleHandle, entryParser.GetUnsigned());

RuntimeTypeHandle[] parsedArgsHandles = GetTypeSequence(ref externalReferencesLookup, ref entryParser);

Expand Down Expand Up @@ -188,12 +186,26 @@ internal bool TryLookupGenericMethodDictionary(GenericMethodLookupData lookupDat
public bool TryGetGenericMethodComponents(IntPtr methodDictionary, out RuntimeTypeHandle declaringType, out MethodNameAndSignature nameAndSignature, out RuntimeTypeHandle[] genericMethodArgumentHandles)
{
if (!TryGetDynamicGenericMethodComponents(methodDictionary, out declaringType, out nameAndSignature, out genericMethodArgumentHandles))
if (!TryGetStaticGenericMethodComponents(methodDictionary, out declaringType, out nameAndSignature, out genericMethodArgumentHandles))
{
if (!TryGetStaticGenericMethodComponents(methodDictionary, out declaringType, out TypeManagerHandle typeManager, out uint nameAndSigOffset, out genericMethodArgumentHandles))
return false;

nameAndSignature = TypeLoaderEnvironment.Instance.GetMethodNameAndSignatureFromNativeLayoutOffset(typeManager, nameAndSigOffset);
}

return true;
}

public bool TryGetGenericMethodComponents(IntPtr methodDictionary, out RuntimeTypeHandle declaringType, out RuntimeTypeHandle[] genericMethodArgumentHandles)
{
if (!TryGetDynamicGenericMethodComponents(methodDictionary, out declaringType, out _, out genericMethodArgumentHandles))
if (!TryGetStaticGenericMethodComponents(methodDictionary, out declaringType, out _, out _, out genericMethodArgumentHandles))
return false;

return true;
}


public bool TryLookupExactMethodPointer(InstantiatedMethod method, out IntPtr result)
{
int lookupHashcode = method.OwningType.GetHashCode();
Expand Down Expand Up @@ -351,7 +363,7 @@ private bool TryGetDynamicGenericMethodComponents(IntPtr methodDictionary, out R
return true;
}
}
private unsafe bool TryGetStaticGenericMethodComponents(IntPtr methodDictionary, out RuntimeTypeHandle declaringType, out MethodNameAndSignature methodNameAndSignature, out RuntimeTypeHandle[] genericMethodArgumentHandles)
private unsafe bool TryGetStaticGenericMethodComponents(IntPtr methodDictionary, out RuntimeTypeHandle declaringType, out TypeManagerHandle typeManager, out uint nameAndSigOffset, out RuntimeTypeHandle[] genericMethodArgumentHandles)
{
// Generic method dictionaries have a header that has the hash code in it. Locate the header
IntPtr dictionaryHeader = IntPtr.Subtract(methodDictionary, IntPtr.Size);
Expand Down Expand Up @@ -379,9 +391,8 @@ private unsafe bool TryGetStaticGenericMethodComponents(IntPtr methodDictionary,
// We have a match - fill in the results
declaringType = externalReferencesLookup.GetRuntimeTypeHandleFromIndex(entryParser.GetUnsigned());

// Hash table names / sigs are indirected through to the native layout info
if (!TypeLoaderEnvironment.Instance.TryGetMethodNameAndSignatureFromNativeLayoutOffset(module.Handle, entryParser.GetUnsigned(), out methodNameAndSignature))
continue;
typeManager = module.Handle;
nameAndSigOffset = entryParser.GetUnsigned();

uint arity = entryParser.GetSequenceCount();
genericMethodArgumentHandles = new RuntimeTypeHandle[arity];
Expand All @@ -396,7 +407,8 @@ private unsafe bool TryGetStaticGenericMethodComponents(IntPtr methodDictionary,
}

declaringType = default(RuntimeTypeHandle);
methodNameAndSignature = null;
typeManager = default;
nameAndSigOffset = 0;
genericMethodArgumentHandles = null;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,13 +651,7 @@ public static bool TryGetVirtualResolveData(NativeFormatModuleInfo module,

uint nameAndSigPointerToken = entryParser.GetUnsigned();

MethodNameAndSignature nameAndSig;
if (!TypeLoaderEnvironment.Instance.TryGetMethodNameAndSignatureFromNativeLayoutOffset(module.Handle, nameAndSigPointerToken, out nameAndSig))
{
Debug.Assert(false);
continue;
}

MethodNameAndSignature nameAndSig = TypeLoaderEnvironment.Instance.GetMethodNameAndSignatureFromNativeLayoutOffset(module.Handle, nameAndSigPointerToken);
if (!methodSignatureComparer.IsMatchingNativeLayoutMethodNameAndSignature(nameAndSig.Name, nameAndSig.Signature))
{
continue;
Expand Down Expand Up @@ -972,12 +966,7 @@ public void GetNext(
else
{
uint nameAndSigToken = entryParser.GetUnsigned();
MethodNameAndSignature nameAndSig;
if (!TypeLoaderEnvironment.Instance.TryGetMethodNameAndSignatureFromNativeLayoutOffset(_moduleHandle, nameAndSigToken, out nameAndSig))
{
Debug.Assert(false);
return;
}
MethodNameAndSignature nameAndSig = TypeLoaderEnvironment.Instance.GetMethodNameAndSignatureFromNativeLayoutOffset(_moduleHandle, nameAndSigToken);
Debug.Assert(nameAndSig.Signature.IsNativeLayoutSignature);
if (!methodSignatureComparer.IsMatchingNativeLayoutMethodNameAndSignature(nameAndSig.Name, nameAndSig.Signature))
return;
Expand All @@ -1004,11 +993,7 @@ public void GetNext(
Debug.Assert((_hasEntryPoint || ((_flags & InvokeTableFlags.HasVirtualInvoke) != 0)) && ((_flags & InvokeTableFlags.RequiresInstArg) != 0));

uint nameAndSigPointerToken = entryParser.GetUnsigned();
if (!TypeLoaderEnvironment.Instance.TryGetMethodNameAndSignatureFromNativeLayoutOffset(_moduleHandle, nameAndSigPointerToken, out _nameAndSignature))
{
Debug.Assert(false); //Error
_isMatchingMethodHandleAndDeclaringType = false;
}
_nameAndSignature = TypeLoaderEnvironment.Instance.GetMethodNameAndSignatureFromNativeLayoutOffset(_moduleHandle, nameAndSigPointerToken);
}
else if (((_flags & InvokeTableFlags.RequiresInstArg) != 0) && _hasEntryPoint)
_entryDictionary = extRefTable.GetGenericDictionaryFromIndex(entryParser.GetUnsigned());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,11 @@ public bool TryGetMethodNameAndSignaturePointersFromNativeLayoutSignature(TypeMa
return true;
}

public bool TryGetMethodNameAndSignatureFromNativeLayoutOffset(TypeManagerHandle moduleHandle, uint nativeLayoutOffset, out MethodNameAndSignature nameAndSignature)
public MethodNameAndSignature GetMethodNameAndSignatureFromNativeLayoutOffset(TypeManagerHandle moduleHandle, uint nativeLayoutOffset)
{
nameAndSignature = null;

NativeReader reader = GetNativeLayoutInfoReader(moduleHandle);
NativeParser parser = new NativeParser(reader, nativeLayoutOffset);
if (parser.IsNull)
return false;

nameAndSignature = GetMethodNameAndSignature(ref parser, moduleHandle, out _, out _);
return true;
return GetMethodNameAndSignature(ref parser, moduleHandle, out _, out _);
}

internal static MethodNameAndSignature GetMethodNameAndSignature(ref NativeParser parser, TypeManagerHandle moduleHandle, out RuntimeSignature methodNameSig, out RuntimeSignature methodSig)
Expand Down
7 changes: 3 additions & 4 deletions src/tests/nativeaot/SmokeTests/HardwareIntrinsics/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@ static int Main()
Console.WriteLine($"* Size of the executable is {fileSize / 1024,7:n0} kB *");
Console.WriteLine("****************************************************");

const int Meg = 1024 * 1024;
const int HalfMeg = Meg / 2;
long lowerBound, upperBound;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
const int Meg = 1024 * 1024;
lowerBound = 2 * Meg; // 2 MB
upperBound = 4 * Meg; // 4 MB
}
else
{
lowerBound = Meg + HalfMeg; // 1.5 MB
upperBound = 2 * Meg; // 2 MB
lowerBound = 1300 * 1024; // ~1.3 MB
upperBound = 1600 * 1024; // ~1.6 MB
}

if (fileSize < lowerBound || fileSize > upperBound)
Expand Down

0 comments on commit 123e2b2

Please sign in to comment.