Skip to content

Commit

Permalink
* Reduce allocations
Browse files Browse the repository at this point in the history
* Ensure correct return type
* Ensure correct declaring type
* Improve output in call analysis
  • Loading branch information
ds5678 authored and gompoc committed Oct 24, 2024
1 parent 11c3cce commit d9048e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
15 changes: 13 additions & 2 deletions Cpp2IL.Core/Model/Contexts/ConcreteGenericMethodAnalysisContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,25 @@ private ConcreteGenericMethodAnalysisContext(Cpp2IlMethodRef methodRef, Assembly
DeclaringAsm = declaringAssembly;
BaseMethodContext = ResolveBaseMethod(methodRef, declaringAssembly.GetTypeByDefinition(methodRef.DeclaringType)!);

var genericTypeParameters = ResolveTypeArray(methodRef.TypeGenericParams, declaringAssembly);
var genericMethodParameters = ResolveTypeArray(methodRef.MethodGenericParams, declaringAssembly);

for (var i = 0; i < BaseMethodContext.Parameters.Count; i++)
{
var parameter = BaseMethodContext.Parameters[i];
var parameterType = parameter.ParameterTypeContext;
var instantiatedType = GenericInstantiation.Instantiate(
parameter.ParameterTypeContext,
ResolveTypeArray(methodRef.TypeGenericParams, declaringAssembly),
ResolveTypeArray(methodRef.MethodGenericParams, declaringAssembly));
genericTypeParameters,
genericMethodParameters);

Parameters.Add(parameterType == instantiatedType
? parameter
: new InjectedParameterAnalysisContext(parameter.Name, instantiatedType, i, BaseMethodContext));
}

InjectedReturnType = GenericInstantiation.Instantiate(BaseMethodContext.ReturnTypeContext, genericTypeParameters, genericMethodParameters);

if (UnderlyingPointer != 0)
rawMethodBody = AppContext.InstructionSet.GetRawBytesForMethod(this, false);
}
Expand All @@ -64,13 +69,19 @@ private static TypeAnalysisContext ResolveDeclaringType(Cpp2IlMethodRef methodRe
var baseType = declaringAssembly.AppContext.ResolveContextForType(methodRef.DeclaringType)
?? throw new($"Unable to resolve declaring type {methodRef.DeclaringType.FullName} for generic method {methodRef}");

if (methodRef.TypeGenericParams.Length == 0)
return baseType;

var genericParams = ResolveTypeArray(methodRef.TypeGenericParams, declaringAssembly);

return new GenericInstanceTypeAnalysisContext(baseType, genericParams, declaringAssembly);
}

private static TypeAnalysisContext[] ResolveTypeArray(Il2CppTypeReflectionData[] array, AssemblyAnalysisContext declaringAssembly)
{
if (array.Length == 0)
return [];

var ret = new TypeAnalysisContext[array.Length];
for (var i = 0; i < array.Length; i++)
{
Expand Down
8 changes: 1 addition & 7 deletions Cpp2IL.Core/ProcessingLayers/CallAnalysisProcessingLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,8 @@ private static void AddAttribute((InjectedMethodAnalysisContext, InjectedFieldAn

var memberField = (callsAttributeInfo.Item2[1], targetMethod.Name);

var concreteMethod = targetMethod as ConcreteGenericMethodAnalysisContext;

(FieldAnalysisContext, object)? typeParametersField;
if (concreteMethod is not null)
if (targetMethod is ConcreteGenericMethodAnalysisContext concreteMethod)
{
if (concreteMethod.MethodRef.MethodGenericParams.Length > 0)
{
Expand Down Expand Up @@ -269,10 +267,6 @@ private static void AddAttribute((InjectedMethodAnalysisContext, InjectedFieldAn
{
returnType = targetMethod.InjectedReturnType;
}
else if (concreteMethod is { BaseMethodContext: { Definition: not null } or { InjectedReturnType: not null } })
{
returnType = concreteMethod.BaseMethodContext.ReturnTypeContext;
}
else if (targetMethod.Definition is null)
{
returnType = null;
Expand Down

0 comments on commit d9048e9

Please sign in to comment.