From f1629824d70eca532db379a7a4d42323fe471155 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Thu, 14 Nov 2019 16:39:42 +0100 Subject: [PATCH] Performance: Pre-calculate all method start and end pointers --- Il2CppInspector/Il2CppInspector.cs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Il2CppInspector/Il2CppInspector.cs b/Il2CppInspector/Il2CppInspector.cs index 330d828..b4c984b 100644 --- a/Il2CppInspector/Il2CppInspector.cs +++ b/Il2CppInspector/Il2CppInspector.cs @@ -19,8 +19,8 @@ namespace Il2CppInspector private Il2CppBinary Binary { get; } private Metadata Metadata { get; } - // All method pointers sorted in ascending order (for finding the end of a method) - private List sortedMethodPointers { get; } + // All method pointers (start => end) + private Dictionary methodPointers { get; } // Attribute indexes (>=24.1) arranged by customAttributeStart and token public Dictionary> AttributeIndicesByToken { get; } @@ -157,10 +157,16 @@ namespace Il2CppInspector } // Get sorted list of method pointers - if (Version <= 24.1) - sortedMethodPointers = Binary.GlobalMethodPointers.OrderBy(m => m).ToList(); - if (Version >= 24.2) - sortedMethodPointers = Binary.ModuleMethodPointers.SelectMany(module => module.Value).OrderBy(m => m).ToList(); + var sortedMethodPointers = (Version <= 24.1)? + Binary.GlobalMethodPointers.OrderBy(m => m).ToList() : + Binary.ModuleMethodPointers.SelectMany(module => module.Value).OrderBy(m => m).ToList(); + + // Guestimate method end addresses + methodPointers = new Dictionary(sortedMethodPointers.Count); + for (var i = 0; i < sortedMethodPointers.Count - 1; i++) + methodPointers.Add(sortedMethodPointers[i], sortedMethodPointers[i + 1]); + // The last method end pointer will be incorrect but there is no way of calculating it + methodPointers.Add(sortedMethodPointers[^1], sortedMethodPointers[^1]); // Organize custom attribute indices if (Version >= 24.1) { @@ -212,7 +218,7 @@ namespace Il2CppInspector // Consider the end of the method to be the start of the next method (or zero) // The last method end will be wrong but there is no way to calculate it - return (start, sortedMethodPointers.FirstOrDefault(p => p > start)); + return (start, methodPointers[start]); } public static List LoadFromFile(string codeFile, string metadataFile) {