Skip to content

Use a field for GC references #2435

Open
@jonpryor

Description

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

Metadata

Assignees

No one assigned

    Labels

    Area: App RuntimeIssues in `libmonodroid.so`.Area: PerformanceIssues with performance.enhancementProposed change to current functionality.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions