Currently, the Java Callable Wrappers use an ArrayList and methods to help mirror the object graph from the Mono GC to the Android GC:
/* partial */ class Example {
ArrayList<Object> managedReferences = new ArrayList<Object>();
public void monodroidAddReference (Object o) {
managedReferences.add (o);
}
public void monodroidClearReferences () {
managedReferences.clear ();
}
}
monodroidAddReference() and monodroidClearReferences() are called via JNI within osbridge.cc:
https://github.com/xamarin/xamarin-android/blob/c38c58e9c5634819845350e9e687186c0808a781/src/monodroid/jni/osbridge.cc#L581-L587
An idea occurred to me: what if instead of invoking monodroidAddReference()/etc., we instead used a field?
/* partial */ class Example {
Object []managedReferences;
}
This would reduce Java-side memory allocations (no ArrayList<Object>), and would alter the number of JNI invocations needed, from num_xrefs JNIEnv::CallVoidMethod() invocations to num_xrefs calls to JNIEnv::SetObjectArrayElement() + 1 JNIEnv::SetObjectField(). (Presumably JNIEnv::SetObjectArrayElement() will be faster than JNIEnv::CallVoidMethod().)
https://github.com/xamarin/xamarin-android/blob/c38c58e9c5634819845350e9e687186c0808a781/src/monodroid/jni/osbridge.cc#L798-L806
Currently, the Java Callable Wrappers use an
ArrayListand methods to help mirror the object graph from the Mono GC to the Android GC:monodroidAddReference()andmonodroidClearReferences()are called via JNI withinosbridge.cc:https://github.com/xamarin/xamarin-android/blob/c38c58e9c5634819845350e9e687186c0808a781/src/monodroid/jni/osbridge.cc#L581-L587
An idea occurred to me: what if instead of invoking
monodroidAddReference()/etc., we instead used a field?This would reduce Java-side memory allocations (no
ArrayList<Object>), and would alter the number of JNI invocations needed, fromnum_xrefsJNIEnv::CallVoidMethod()invocations tonum_xrefscalls toJNIEnv::SetObjectArrayElement()+ 1JNIEnv::SetObjectField(). (PresumablyJNIEnv::SetObjectArrayElement()will be faster thanJNIEnv::CallVoidMethod().)https://github.com/xamarin/xamarin-android/blob/c38c58e9c5634819845350e9e687186c0808a781/src/monodroid/jni/osbridge.cc#L798-L806