-
Notifications
You must be signed in to change notification settings - Fork 531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Mono.Android] treat AllocObjectSupported
as always true
#9146
[Mono.Android] treat AllocObjectSupported
as always true
#9146
Conversation
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
I'm not sure how much this change will actually help, as this code should effectively be "dead" with the death of (i.e. only very very old bindings should be hitting these methods.) Anything newer will be using or from our build tree: // from src/Mono.Android/obj/Debug/net9.0/android-35/mcw/Java.Lang.Number.cs
namespace Java.Lang {
public abstract partial class Number {
public unsafe Number () : base (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
{
const string __id = "()V";
if (((global::Java.Lang.Object) this).Handle != IntPtr.Zero)
return;
try {
var __r = _members.InstanceMethods.StartCreateInstance (__id, ((object) this).GetType (), null);
SetHandle (__r.Handle, JniHandleOwnership.TransferLocalRef);
_members.InstanceMethods.FinishCreateInstance (__id, this, null);
} finally {
}
}
}
} That said, we could probably remove the |
I was thinking of this as a "part 2" to: After looking at this, it only would help a very small amount at startup (and maybe cleans up code). We'd avoid passing in the API level from Java. Maybe it's worth removing |
Context: 972c5bc Context: dotnet/android#9146 As our minimum Android API level is now API-21 (Android 5.0), and .NET for Android only sets `Java.Interop.JniRuntime.CreationOptions.NewObjectRequired`=true on API-10 and lower, we can remove the support for this option. This has the added benefit of removing the TLS lookup that `JniEnvironment.Runtime` requires, and thus *should* improve perf. Add `[Obsolete]` to `JniRuntime.CreationOptions.NewObjectRequired`.
Like this? dotnet/java-interop#1247 |
Context: 972c5bc Context: dotnet/android#9146 As our minimum Android API level is now API-21 (Android 5.0), and .NET for Android only sets `Java.Interop.JniRuntime.CreationOptions.NewObjectRequired`=true on API-10 and lower, we can remove the support for this option. This has the added benefit of removing the TLS lookup that `JniEnvironment.Runtime` requires, and thus *should* improve perf. Add `[Obsolete]` to `JniRuntime.CreationOptions.NewObjectRequired`.
Context: 972c5bc Context: dotnet/android#9146 As our minimum Android API level is now API-21 (Android 5.0), and .NET for Android only sets `Java.Interop.JniRuntime.CreationOptions.NewObjectRequired`=true on API-10 and lower, we can remove the support for this option. This has the added benefit of removing the TLS lookup that `JniEnvironment.Runtime` requires, and thus *should* improve perf. Add `[Obsolete]` to `JniRuntime.CreationOptions.NewObjectRequired`.
cbed8f5
to
39fe3ed
Compare
This comment was marked as outdated.
This comment was marked as outdated.
39fe3ed
to
1c1dcb9
Compare
This comment was marked as outdated.
This comment was marked as outdated.
1c1dcb9
to
5be68a7
Compare
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.
5be68a7
to
6e32c64
Compare
@@ -22,7 +22,6 @@ internal struct JnienvInitializeArgs { | |||
public IntPtr Class_forName; | |||
public uint logCategories; | |||
public int version; // TODO: remove, not needed anymore | |||
public int androidSdkVersion; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per your comment on Discord:
Ok, I found maybe the size of
JnienvInitializeArgs
in C++ / C# didn't match
I removed from one and not the other
this means we should also add a public ulong struct_size
field to the beginning of the struct, and assert that the size matches:
internal partial struct JnienvInitializeArgs {
public static along ExpectedStructSize = IntPtr.Size == 8 ? XXX : YYY;
public ulong struct_size;
// …
}
Update the C++ side to be consistent:
In C++ land, set the new struct_size
field in init_android_runtime()
:
struct JnienvInitializeArgs init = {};
init.struct_size = sizeof(JnienvInitializeArgs);
And finally in Initialize()
, assert that things match:
if (args->struct_size != JnienvInitializeArgs.ExpectedStructSize) {
FailFast(…);
}
While doing this, we may also want to reorder the struct members so that pointers are stored together, to minimize internal alignment padding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sizeof()
on the native size won't work as expected in this case. It will include padding of the structure which may differ depending on the target platform (even if bitness is the same). We'd have to calculate structure size as a sum of individual field sizes (a compile-time calculation), but then the managed size would have to compute it at run time, a bit of time loss.
However, perhaps we can just add a "serial number" field to the structure and bump it every time the structure changes, with comments to update both the managed and native sides at the same time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a comment to both struct
s. I think if I had seen that, I would have caught it earlier.
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 aAllocObjectSupported
field, which is now alwaystrue
.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.