From f7d1f355baac42ca0a6363027ec3093b8452e7fa Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Mon, 30 Jul 2018 11:40:09 +0200 Subject: [PATCH] [jnimarshalmethod-gen] optimize type lookup Create a dictionary of cecil types at the beginning, instead of searching through the whole module every time we need to find a type. That saves about 12 seconds from 48, when running it on `Mono.Android.dll` --- tools/jnimarshalmethod-gen/App.cs | 52 ++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/tools/jnimarshalmethod-gen/App.cs b/tools/jnimarshalmethod-gen/App.cs index 82fffe398..23d488c7c 100644 --- a/tools/jnimarshalmethod-gen/App.cs +++ b/tools/jnimarshalmethod-gen/App.cs @@ -21,6 +21,7 @@ class App : MarshalByRefObject internal const string Name = "jnimarshalmethod-gen"; static DirectoryAssemblyResolver resolver = new DirectoryAssemblyResolver (logger: (l, v) => { Console.WriteLine (v); }, loadDebugSymbols: true, loadReaderParameters: new ReaderParameters () { ReadSymbols = true }); static Dictionary definedTypes = new Dictionary (); + static Dictionary typeMap = new Dictionary (); static public bool Debug; static public bool Verbose; static bool keepTemporary; @@ -217,6 +218,8 @@ void CreateMarshalMethodAssembly (string path) var ad = resolver.GetAssembly (path); + PrepareTypeMap (ad.MainModule); + foreach (var type in assembly.DefinedTypes) { if (matchType) { var matched = false; @@ -231,7 +234,7 @@ void CreateMarshalMethodAssembly (string path) if (type.IsGenericType || type.IsGenericTypeDefinition) continue; - var td = ad.MainModule.FindType (type); + var td = FindType (type); if (td == null) { if (Verbose) @@ -387,6 +390,35 @@ static void ColorMessage (string message, ConsoleColor color, TextWriter writer, public static void Error (string message) => ColorMessage ($"Error: {Name}: {message}", ConsoleColor.Red, Console.Error); public static void Warning (string message) => ColorMessage ($"Warning: {Name}: {message}", ConsoleColor.Yellow, Console.Error); + + static void AddToTypeMap (TypeDefinition type) + { + typeMap [type.FullName] = type; + + if (!type.HasNestedTypes) + return; + + foreach (var nested in type.NestedTypes) + AddToTypeMap (nested); + } + + static void PrepareTypeMap (ModuleDefinition md) + { + typeMap.Clear (); + + foreach (var type in md.Types) + AddToTypeMap (type); + } + + static TypeDefinition FindType (Type type) + { + TypeDefinition rv; + string cecilName = type.GetCecilName (); + + typeMap.TryGetValue (cecilName, out rv); + + return rv; + } } static class Extensions @@ -511,23 +543,5 @@ public static bool NeedsMarshalMethod (this MethodDefinition md, DirectoryAssemb return false; } - - static TypeDefinition FindType (Collection types, string name) - { - foreach (var type in types) { - if (type.FullName == name) - return type; - - if (name.StartsWith (type.FullName + '/', StringComparison.InvariantCulture)) - return FindType (type.NestedTypes, name); - } - - return null; - } - - public static TypeDefinition FindType (this ModuleDefinition md, Type type) - { - return FindType (md.Types, type.GetCecilName ()); - } } }