Skip to content

Commit

Permalink
[Mono.Android] treat AllocObjectSupported as always true
Browse files Browse the repository at this point in the history
This removes an API < 10 code path, which should speed up all
`JNIEnv.StartCreateInstance` / `FinishCreateInstance` calls by a very
small amount. They no longer check a `AllocObjectSupported` field,
which is now always `true`.

This also removes the need to pass in `androidSdkVersion` /
`android_api_level` from the native side at startup.

We can also remove the `_monodroid_get_android_api_level()` p/invoke,
which is unused.
  • Loading branch information
jonathanpeppers committed Aug 22, 2024
1 parent 90daa1c commit 1c1dcb9
Show file tree
Hide file tree
Showing 12 changed files with 11 additions and 53 deletions.
4 changes: 0 additions & 4 deletions src/Mono.Android/Android.Runtime/AndroidRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ class AndroidRuntime : JniRuntime {

internal AndroidRuntime (IntPtr jnienv,
IntPtr vm,
bool allocNewObjectSupported,
IntPtr classLoader,
IntPtr classLoader_loadClass,
bool jniAddNativeMethodRegistrationAttributePresent)
: base (new AndroidRuntimeOptions (jnienv,
vm,
allocNewObjectSupported,
classLoader,
classLoader_loadClass,
jniAddNativeMethodRegistrationAttributePresent))
Expand Down Expand Up @@ -87,7 +85,6 @@ public override void RaisePendingException (Exception pendingException)
class AndroidRuntimeOptions : JniRuntime.CreationOptions {
public AndroidRuntimeOptions (IntPtr jnienv,
IntPtr vm,
bool allocNewObjectSupported,
IntPtr classLoader,
IntPtr classLoader_loadClass,
bool jniAddNativeMethodRegistrationAttributePresent)
Expand All @@ -96,7 +93,6 @@ public AndroidRuntimeOptions (IntPtr jnienv,
ClassLoader = new JniObjectReference (classLoader, JniObjectReferenceType.Global);
ClassLoader_LoadClass_id= classLoader_loadClass;
InvocationPointer = vm;
NewObjectRequired = !allocNewObjectSupported;
ObjectReferenceManager = new AndroidObjectReferenceManager ();
TypeManager = new AndroidTypeManager (jniAddNativeMethodRegistrationAttributePresent);
ValueManager = new AndroidValueManager ();
Expand Down
18 changes: 3 additions & 15 deletions src/Mono.Android/Android.Runtime/JNIEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ public static IntPtr AllocObject (Type type)

public static unsafe IntPtr StartCreateInstance (IntPtr jclass, IntPtr constructorId, JValue* constructorParameters)
{
if (JNIEnvInit.AllocObjectSupported) {
return AllocObject (jclass);
}
return NewObject (jclass, constructorId, constructorParameters);
return AllocObject (jclass);
}

public static unsafe IntPtr StartCreateInstance (IntPtr jclass, IntPtr constructorId, params JValue[] constructorParameters)
Expand All @@ -186,8 +183,6 @@ public static unsafe IntPtr StartCreateInstance (IntPtr jclass, IntPtr construct

public static unsafe void FinishCreateInstance (IntPtr instance, IntPtr jclass, IntPtr constructorId, JValue* constructorParameters)
{
if (!JNIEnvInit.AllocObjectSupported)
return;
CallNonvirtualVoidMethod (instance, jclass, constructorId, constructorParameters);
}

Expand All @@ -199,10 +194,7 @@ public static unsafe void FinishCreateInstance (IntPtr instance, IntPtr jclass,

public static unsafe IntPtr StartCreateInstance (Type type, string jniCtorSignature, JValue* constructorParameters)
{
if (JNIEnvInit.AllocObjectSupported) {
return AllocObject (type);
}
return CreateInstance (type, jniCtorSignature, constructorParameters);
return AllocObject (type);
}

public static unsafe IntPtr StartCreateInstance (Type type, string jniCtorSignature, params JValue[] constructorParameters)
Expand All @@ -213,9 +205,7 @@ public static unsafe IntPtr StartCreateInstance (Type type, string jniCtorSignat

public static unsafe IntPtr StartCreateInstance (string jniClassName, string jniCtorSignature, JValue* constructorParameters)
{
if (JNIEnvInit.AllocObjectSupported)
return AllocObject (jniClassName);
return CreateInstance (jniClassName, jniCtorSignature, constructorParameters);
return AllocObject (jniClassName);
}

public static unsafe IntPtr StartCreateInstance (string jniClassName, string jniCtorSignature, params JValue[] constructorParameters)
Expand All @@ -226,8 +216,6 @@ public static unsafe IntPtr StartCreateInstance (string jniClassName, string jni

public static unsafe void FinishCreateInstance (IntPtr instance, string jniCtorSignature, JValue* constructorParameters)
{
if (!JNIEnvInit.AllocObjectSupported)
return;
InvokeConstructor (instance, jniCtorSignature, constructorParameters);
}

Expand Down
5 changes: 1 addition & 4 deletions src/Mono.Android/Android.Runtime/JNIEnvInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ internal struct JnienvInitializeArgs {
#pragma warning restore 0649

internal static AndroidValueManager? AndroidValueManager;
internal static bool AllocObjectSupported;
internal static bool IsRunningOnDesktop;
internal static bool jniRemappingInUse;
internal static bool LogAssemblyCategory;
Expand Down Expand Up @@ -94,12 +93,10 @@ internal static unsafe void Initialize (JnienvInitializeArgs* args)

mid_Class_forName = new JniMethodInfo (args->Class_forName, isStatic: true);

bool androidNewerThan10 = args->androidSdkVersion > 10;
BoundExceptionType = (BoundExceptionType)args->ioExceptionType;
androidRuntime = new AndroidRuntime (args->env, args->javaVm, androidNewerThan10, args->grefLoader, args->Loader_loadClass, args->jniAddNativeMethodRegistrationAttributePresent != 0);
androidRuntime = new AndroidRuntime (args->env, args->javaVm, args->grefLoader, args->Loader_loadClass, args->jniAddNativeMethodRegistrationAttributePresent != 0);
AndroidValueManager = (AndroidValueManager) androidRuntime.ValueManager;

AllocObjectSupported = androidNewerThan10;
IsRunningOnDesktop = args->isRunningOnDesktop == 1;

grefIGCUserPeer_class = args->grefIGCUserPeer;
Expand Down
1 change: 0 additions & 1 deletion src/java-runtime/java/mono/android/MonoPackageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public static void LoadApplication (Context context, ApplicationInfo runtimePack
localDateTimeOffset,
loader,
MonoPackageManager_Resources.Assemblies,
Build.VERSION.SDK_INT,
isEmulator (),
haveSplitApks
);
Expand Down
1 change: 0 additions & 1 deletion src/java-runtime/java/mono/android/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public static native void initInternal (
int localDateTimeOffset,
ClassLoader loader,
String[] assemblies,
int apiLevel,
boolean isEmulator,
boolean haveSplitApks
);
Expand Down
6 changes: 0 additions & 6 deletions src/native/monodroid/internal-pinvokes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,6 @@ _monodroid_gc_wait_for_bridge_processing ()
mono_gc_wait_for_bridge_processing ();
}

int
_monodroid_get_android_api_level ()
{
return monodroidRuntime.get_android_api_level ();
}

void
monodroid_clear_gdb_wait ()
{
Expand Down
2 changes: 1 addition & 1 deletion src/native/monodroid/mono_android_Runtime.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions src/native/monodroid/monodroid-glue-internal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ namespace xamarin::android::internal
jmethodID Class_forName;
unsigned int logCategories;
int version;
int androidSdkVersion;
int grefGcThreshold;
jobject grefIGCUserPeer;
int isRunningOnDesktop;
Expand All @@ -118,16 +117,11 @@ namespace xamarin::android::internal
void Java_mono_android_Runtime_register (JNIEnv *env, jstring managedType, jclass nativeClass, jstring methods);
void Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass klass, jstring lang, jobjectArray runtimeApksJava,
jstring runtimeNativeLibDir, jobjectArray appDirs, jint localDateTimeOffset,
jobject loader, jobjectArray assembliesJava, jint apiLevel, jboolean isEmulator,
jobject loader, jobjectArray assembliesJava, jboolean isEmulator,
jboolean haveSplitApks);

jint Java_JNI_OnLoad (JavaVM *vm, void *reserved);

int get_android_api_level () const
{
return android_api_level;
}

jclass get_java_class_System () const
{
return java_System;
Expand Down Expand Up @@ -252,7 +246,6 @@ namespace xamarin::android::internal
#endif
private:
MonoMethod *registerType = nullptr;
int android_api_level = 0;
volatile bool monodroid_gdb_wait = true;
jclass java_System;
jmethodID java_System_identityHashCode;
Expand Down
8 changes: 2 additions & 6 deletions src/native/monodroid/monodroid-glue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,6 @@ MonodroidRuntime::init_android_runtime (JNIEnv *env, jclass runtimeClass, jobjec
init.env = env;
init.logCategories = log_categories;
init.version = env->GetVersion ();
init.androidSdkVersion = android_api_level;
init.isRunningOnDesktop = is_running_on_desktop ? 1 : 0;
init.brokenExceptionTransitions = application_config.broken_exception_transitions ? 1 : 0;
init.packageNamingPolicy = static_cast<int>(application_config.package_naming_policy);
Expand Down Expand Up @@ -1369,7 +1368,7 @@ MonodroidRuntime::install_logging_handlers ()
inline void
MonodroidRuntime::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass klass, jstring lang, jobjectArray runtimeApksJava,
jstring runtimeNativeLibDir, jobjectArray appDirs, jint localDateTimeOffset,
jobject loader, jobjectArray assembliesJava, jint apiLevel, jboolean isEmulator,
jobject loader, jobjectArray assembliesJava, jboolean isEmulator,
jboolean haveSplitApks)
{
char *mono_log_mask_raw = nullptr;
Expand Down Expand Up @@ -1410,7 +1409,6 @@ MonodroidRuntime::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass kl
);
}

android_api_level = apiLevel;
AndroidSystem::detect_embedded_dso_mode (applicationDirs);
AndroidSystem::set_running_in_emulator (isEmulator);

Expand Down Expand Up @@ -1564,7 +1562,6 @@ Java_mono_android_Runtime_init (JNIEnv *env, jclass klass, jstring lang, jobject
0,
loader,
assembliesJava,
apiLevel,
/* isEmulator */ JNI_FALSE,
/* haveSplitApks */ JNI_FALSE
);
Expand All @@ -1573,7 +1570,7 @@ Java_mono_android_Runtime_init (JNIEnv *env, jclass klass, jstring lang, jobject
JNIEXPORT void JNICALL
Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass klass, jstring lang, jobjectArray runtimeApksJava,
jstring runtimeNativeLibDir, jobjectArray appDirs, jint localDateTimeOffset, jobject loader,
jobjectArray assembliesJava, jint apiLevel, jboolean isEmulator,
jobjectArray assembliesJava, jboolean isEmulator,
jboolean haveSplitApks)
{
monodroidRuntime.Java_mono_android_Runtime_initInternal (
Expand All @@ -1586,7 +1583,6 @@ Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass klass, jstring lang,
localDateTimeOffset,
loader,
assembliesJava,
apiLevel,
isEmulator,
application_config.ignore_split_configs ? false : haveSplitApks
);
Expand Down
1 change: 0 additions & 1 deletion src/native/pinvoke-override/generate-pinvoke-tables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const std::vector<std::string> internal_pinvoke_names = {
"monodroid_free",
"_monodroid_freeifaddrs",
"_monodroid_gc_wait_for_bridge_processing",
"_monodroid_get_android_api_level",
"_monodroid_get_dns_servers",
"monodroid_get_dylib",
"_monodroid_get_identity_hash_code",
Expand Down
8 changes: 3 additions & 5 deletions src/native/pinvoke-override/pinvoke-tables.include
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace {
#if INTPTR_MAX == INT64_MAX
//64-bit internal p/invoke table
std::array<PinvokeEntry, 49> internal_pinvokes {{
std::array<PinvokeEntry, 48> internal_pinvokes {{
{0x452e23128e42f0a, "monodroid_get_log_categories", reinterpret_cast<void*>(&monodroid_get_log_categories)},
{0xa50ce5de13bf8b5, "_monodroid_timezone_get_default_id", reinterpret_cast<void*>(&_monodroid_timezone_get_default_id)},
{0x19055d65edfd668e, "_monodroid_get_network_interface_up_state", reinterpret_cast<void*>(&_monodroid_get_network_interface_up_state)},
Expand Down Expand Up @@ -43,7 +43,6 @@ namespace {
{0xc2a21d3f6c8ccc24, "_monodroid_lookup_replacement_method_info", reinterpret_cast<void*>(&_monodroid_lookup_replacement_method_info)},
{0xc5b4690e13898fa3, "monodroid_timing_start", reinterpret_cast<void*>(&monodroid_timing_start)},
{0xcc873ea8493d1dd5, "monodroid_embedded_assemblies_set_assemblies_prefix", reinterpret_cast<void*>(&monodroid_embedded_assemblies_set_assemblies_prefix)},
{0xce439cfbe29dec11, "_monodroid_get_android_api_level", reinterpret_cast<void*>(&_monodroid_get_android_api_level)},
{0xd1e121b94ea63f2e, "_monodroid_gref_get", reinterpret_cast<void*>(&_monodroid_gref_get)},
{0xd5151b00eb33d85e, "monodroid_TypeManager_get_java_class_name", reinterpret_cast<void*>(&monodroid_TypeManager_get_java_class_name)},
{0xda517ef392b6a888, "java_interop_free", reinterpret_cast<void*>(&java_interop_free)},
Expand Down Expand Up @@ -553,14 +552,13 @@ constexpr hash_t system_security_cryptography_native_android_library_hash = 0x18
constexpr hash_t system_globalization_native_library_hash = 0x28b5c8fca080abd5;
#else
//32-bit internal p/invoke table
std::array<PinvokeEntry, 49> internal_pinvokes {{
std::array<PinvokeEntry, 48> internal_pinvokes {{
{0xb7a486a, "monodroid_TypeManager_get_java_class_name", reinterpret_cast<void*>(&monodroid_TypeManager_get_java_class_name)},
{0xf562bd9, "monodroid_embedded_assemblies_set_assemblies_prefix", reinterpret_cast<void*>(&monodroid_embedded_assemblies_set_assemblies_prefix)},
{0x1a8eab17, "_monodroid_get_identity_hash_code", reinterpret_cast<void*>(&_monodroid_get_identity_hash_code)},
{0x227a2636, "monodroid_get_namespaced_system_property", reinterpret_cast<void*>(&monodroid_get_namespaced_system_property)},
{0x2a0e1744, "java_interop_strdup", reinterpret_cast<void*>(&java_interop_strdup)},
{0x2aea7c33, "_monodroid_max_gref_get", reinterpret_cast<void*>(&_monodroid_max_gref_get)},
{0x2f7d0f53, "_monodroid_get_android_api_level", reinterpret_cast<void*>(&_monodroid_get_android_api_level)},
{0x30b9487b, "_monodroid_get_dns_servers", reinterpret_cast<void*>(&_monodroid_get_dns_servers)},
{0x3227d81a, "monodroid_timing_start", reinterpret_cast<void*>(&monodroid_timing_start)},
{0x333d4835, "_monodroid_lookup_replacement_method_info", reinterpret_cast<void*>(&_monodroid_lookup_replacement_method_info)},
Expand Down Expand Up @@ -1095,6 +1093,6 @@ constexpr hash_t system_security_cryptography_native_android_library_hash = 0x93
constexpr hash_t system_globalization_native_library_hash = 0xa66f1e5a;
#endif

constexpr size_t internal_pinvokes_count = 49;
constexpr size_t internal_pinvokes_count = 48;
constexpr size_t dotnet_pinvokes_count = 477;
} // end of anonymous namespace
1 change: 0 additions & 1 deletion src/native/runtime-base/internal-pinvokes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ void _monodroid_weak_gref_delete (jobject handle, char type, const char *threadN
void _monodroid_lref_log_new (int lrefc, jobject handle, char type, const char *threadName, int threadId, const char *from, int from_writable);
void _monodroid_lref_log_delete (int lrefc, jobject handle, char type, const char *threadName, int threadId, const char *from, int from_writable);
void _monodroid_gc_wait_for_bridge_processing ();
int _monodroid_get_android_api_level ();
void monodroid_clear_gdb_wait ();
void* _monodroid_get_identity_hash_code (JNIEnv *env, void *v);
void* _monodroid_timezone_get_default_id ();
Expand Down

0 comments on commit 1c1dcb9

Please sign in to comment.