Skip to content

Commit 63b5d6d

Browse files
radekdoulikgrendello
authored andcommitted
Lazy magic marshaling types map initialization (#2126)
Initialize the types map in `Prefill` method, filled by the linker. Also pass the types count to the dictionary constructor to improve performance and optimize memory allocation. An example of `MagicRegistrationMap::Prefill` method, updated by the linker, with the new code: using System.Collections.Generic; private static void Prefill () { MagicRegistrationMap.typesMap = new Dictionary<string, int> (5); MagicRegistrationMap.typesMap ["xatemplateaot.MainActivity"] = 0; MagicRegistrationMap.typesMap ["Android.Runtime.UncaughtExceptionHandler"] = 1; MagicRegistrationMap.typesMap ["Android.Views.View+IOnClickListenerImplementor"] = 2; MagicRegistrationMap.typesMap ["Java.Lang.Thread+RunnableImplementor"] = 3; MagicRegistrationMap.typesMap ["Java.Interop.TypeManager+JavaTypeManager"] = 4; }
1 parent 79cf460 commit 63b5d6d

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/Mono.Android/Android.Runtime/AndroidRuntime.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,12 @@ static void Prefill ()
291291

292292
static MagicRegistrationMap ()
293293
{
294-
typesMap = new Dictionary<string, int> ();
295-
296294
Prefill ();
297295
}
298296

299297
static public bool Filled {
300298
get {
301-
return typesMap.Count > 0;
299+
return typesMap != null && typesMap.Count > 0;
302300
}
303301
}
304302

src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidMarkStep.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,23 @@ void UpdateMagicPrefill (TypeDefinition magicType)
200200
return;
201201

202202
var typeDictionary = GetType ("mscorlib", "System.Collections.Generic.Dictionary`2");
203+
var ctorDictionary = GetMethod (typeDictionary, ".ctor", new string[] { "System.Int32" });
203204
var methodSetItem = GetMethod (typeDictionary, "set_Item", new string[] { "TKey", "TValue" });
204205
var genericTypeDictionary = new GenericInstanceType (typeDictionary);
205206
genericTypeDictionary.GenericArguments.Add (GetType ("mscorlib", "System.String"));
206207
genericTypeDictionary.GenericArguments.Add (GetType ("mscorlib", "System.Int32"));
207208

209+
var genericMethodDictionaryCtor = CreateGenericMethodReference (ctorDictionary, genericTypeDictionary);
208210
var genericMethodDictionarySetItem = CreateGenericMethodReference (methodSetItem, genericTypeDictionary);
209211
var importedMethodSetItem = magicType.Module.ImportReference (genericMethodDictionarySetItem);
210212

211213
var instructions = methodPrefill.Body.Instructions;
212214
instructions.Clear ();
213215

216+
instructions.Add (CreateLoadArraySizeOrOffsetInstruction (marshalTypes.Count));
217+
instructions.Add (Instruction.Create (OpCodes.Newobj, magicType.Module.ImportReference (genericMethodDictionaryCtor)));
218+
instructions.Add (Instruction.Create (OpCodes.Stsfld, fieldTypesMap));
219+
214220
int idx = 0;
215221

216222
foreach (var type in marshalTypes) {

0 commit comments

Comments
 (0)