Skip to content

Commit ad4885d

Browse files
committed
Make GetCustomAttribute calls optional in JniTypeManager
Optionally do not look for the `JniAddNativeMethodRegistrationAttribute`. It turns out that this attribute is not commonly present in Xamarin.Android applications and so all of the calls to `GetCustomAttribute` failed to find it, incurring unnecessary performance penalty on the application. Make the code consult a global flag (in case of Xamarin.Android it is set on the build time) which tells it whether or not to skip querying for the above custom attribute. The flag is `true` by default to maintain backward compatibility.
1 parent 4f47ec8 commit ad4885d

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ protected bool TryRegisterNativeMembers (JniType nativeClass, Type type, string?
286286

287287
static Type [] registerMethodParameters = new Type [] { typeof (JniNativeMethodRegistrationArguments) };
288288

289-
static bool TryLoadJniMarshalMethods (JniType nativeClass, Type type, string? methods)
289+
bool TryLoadJniMarshalMethods (JniType nativeClass, Type type, string? methods)
290290
{
291291
var marshalType = type?.GetNestedType ("__<$>_jni_marshal_methods", BindingFlags.NonPublic);
292292
if (marshalType == null)
@@ -299,7 +299,7 @@ static bool TryLoadJniMarshalMethods (JniType nativeClass, Type type, string? me
299299

300300
static List<JniNativeMethodRegistration> sharedRegistrations = new List<JniNativeMethodRegistration> ();
301301

302-
static bool TryRegisterNativeMembers (JniType nativeClass, Type marshalType, string? methods, MethodInfo? registerMethod)
302+
bool TryRegisterNativeMembers (JniType nativeClass, Type marshalType, string? methods, MethodInfo? registerMethod)
303303
{
304304
bool lockTaken = false;
305305
bool rv = false;
@@ -331,8 +331,11 @@ static bool TryRegisterNativeMembers (JniType nativeClass, Type marshalType, str
331331
return rv;
332332
}
333333

334-
static bool FindAndCallRegisterMethod (Type marshalType, JniNativeMethodRegistrationArguments arguments)
334+
bool FindAndCallRegisterMethod (Type marshalType, JniNativeMethodRegistrationArguments arguments)
335335
{
336+
if (!Runtime.Options.JniAddNativeMethodRegistrationAttributePresent)
337+
return false;
338+
336339
bool found = false;
337340

338341
foreach (var methodInfo in marshalType.GetRuntimeMethods ()) {

src/Java.Interop/Java.Interop/JniRuntime.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public partial class CreationOptions {
6363
public JniObjectReferenceManager? ObjectReferenceManager {get; set;}
6464
public JniTypeManager? TypeManager {get; set;}
6565
public string? JvmLibraryPath {get; set;}
66+
public bool JniAddNativeMethodRegistrationAttributePresent { get; set; } = true;
6667

6768
public CreationOptions ()
6869
{
@@ -182,11 +183,11 @@ public static void SetCurrent (JniRuntime newCurrent)
182183

183184
internal bool TrackIDs {get; private set;}
184185
internal bool NewObjectRequired {get; private set;}
186+
internal CreationOptions Options { get; }
185187

186188
protected JniRuntime (CreationOptions options)
187189
{
188-
if (options == null)
189-
throw new ArgumentNullException (nameof (options));
190+
Options = options ?? throw new ArgumentNullException (nameof (options));
190191
if (options.InvocationPointer == IntPtr.Zero)
191192
throw new ArgumentException ("options.InvocationPointer is null", nameof (options));
192193

0 commit comments

Comments
 (0)