-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: use field and method map to find original member
- Loading branch information
Showing
8 changed files
with
182 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,24 +1,73 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using Mono.Cecil; | ||
using Mono.Cecil.Cil; | ||
|
||
namespace HKReflect.Fody; | ||
|
||
public sealed partial class ModuleWeaver { | ||
private TypeDefinition FindOrigType(TypeReference typeRef) => | ||
FindTypeDefinition(typeRef.FullName.StripStart("HKReflect").StripStart(".")); | ||
private static readonly Dictionary<string, (Dictionary<string, FieldReference> fieldMap, Dictionary<string, MethodReference> methodMap)> origMap = new(); | ||
|
||
private TypeDefinition FindOrigTypeStatic(TypeReference typeRef) => FindTypeDefinition( | ||
typeRef.FullName.StripStart("HKReflect.Static").StripStart(".").StripEnd("R") | ||
); | ||
private static void BuildOrigMap( | ||
TypeDefinition type, | ||
out Dictionary<string, FieldReference> fieldMap, | ||
out Dictionary<string, MethodReference> methodMap | ||
) { | ||
fieldMap = new(); | ||
methodMap = new(); | ||
|
||
Instruction[] rawFieldMap = type.Methods.Single(method => method.Name == "<OrigFields>") | ||
.Body.Instructions.ToArray(); | ||
|
||
for (int i = 0; i < rawFieldMap.Length; i += 2) { | ||
if (rawFieldMap[i].OpCode.Code == Code.Ret) { | ||
break; | ||
} | ||
|
||
fieldMap.Add((string) rawFieldMap[i].Operand, (FieldReference) rawFieldMap[i + 1].Operand); | ||
} | ||
|
||
Instruction[] rawMethodMap = type.Methods.Single(method => method.Name == "<OrigMethods>") | ||
.Body.Instructions.ToArray(); | ||
|
||
for (int i = 0; i < rawMethodMap.Length; i += 2) { | ||
if (rawMethodMap[i].OpCode.Code == Code.Ret) { | ||
break; | ||
} | ||
|
||
methodMap.Add((string) rawMethodMap[i].Operand, (MethodReference) rawMethodMap[i + 1].Operand); | ||
} | ||
} | ||
|
||
private void GetOrigMap( | ||
TypeReference type, | ||
out Dictionary<string, FieldReference> fieldMap, | ||
out Dictionary<string, MethodReference> methodMap | ||
) { | ||
string fullName = type.FullName; | ||
|
||
lock (origMap) { | ||
if (origMap.ContainsKey(fullName)) { | ||
(fieldMap, methodMap) = origMap[fullName]; | ||
} else { | ||
BuildOrigMap(type as TypeDefinition ?? type.Resolve(), out fieldMap, out methodMap); | ||
origMap[fullName] = (fieldMap, methodMap); | ||
} | ||
} | ||
} | ||
|
||
private TypeDefinition FindOrigTypeSingleton(TypeReference typeRef) => FindTypeDefinition( | ||
typeRef.FullName.StripStart("HKReflect.Singleton").StripStart(".").StripEnd("R") | ||
private TypeDefinition FindOrigType(TypeReference typeRef) => FindTypeDefinition( | ||
typeRef.FullName.StripStart("HKReflect").StripStart(".") | ||
); | ||
|
||
private FieldDefinition FindOrigField(TypeDefinition origType, FieldReference fieldRef) => | ||
origType.Fields.First(fieldDef => fieldDef.Name == fieldRef.Name); | ||
private FieldReference FindOrigField(TypeReference type, FieldReference fieldRef) { | ||
GetOrigMap(type, out Dictionary<string, FieldReference> fieldMap, out _); | ||
return fieldMap[fieldRef.Name]; | ||
} | ||
|
||
private MethodDefinition FindOrigMethod(TypeDefinition origType, MethodReference methodRef) => | ||
origType.Methods.First(methodDef => methodDef.Name == methodRef.Name); | ||
private MethodReference FindOrigMethod(TypeReference type, MethodReference methodRef) { | ||
GetOrigMap(type, out _, out Dictionary<string, MethodReference> methodMap); | ||
return methodMap[methodRef.GetElementMethod().FullName]; | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.