Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Limit JNI calls in nativeUpdateMarker #5103

Closed

Description

@tobrun

In jni.cpp we have a method nativeUpdateMarker. This method is used to update the position and icon of a Marker. Currently the method looks like:

void nativeUpdateMarker(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jobject* marker) {
    mbgl::Log::Debug(mbgl::Event::JNI, "nativeUpdateMarker");
    assert(nativeMapViewPtr != 0);
    NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);

    jlong markerId = jni::GetField<jlong>(*env, marker, *markerIdId);
    if (markerId == -1) {
        return;
    }

    jni::jobject* position = jni::GetField<jni::jobject*>(*env, marker, *markerPositionId);
    jni::jobject* icon = jni::GetField<jni::jobject*>(*env, marker, *markerIconId);

    jni::jstring* jid = reinterpret_cast<jni::jstring*>(jni::GetField<jni::jobject*>(*env, icon, *iconIdId));
    std::string iconId = std_string_from_jstring(env, jid);

    jdouble latitude = jni::GetField<jdouble>(*env, position, *latLngLatitudeId);
    jdouble longitude = jni::GetField<jdouble>(*env, position, *latLngLongitudeId);

    // Because Java only has int, not unsigned int, we need to bump the annotation id up to a long.
    nativeMapView->getMap().updatePointAnnotation(markerId, mbgl::PointAnnotation(mbgl::LatLng(latitude, longitude), iconId));
}

Following the guidelines related to JNI and mentioned in the Google I/O session related to NDK, we should limit the amount of JNI calls inside one JNI method. In case from above this would result in removing the jni::GetField items and replacing jobject marker with multiple parameters.

For example this will result in replacing the following in the method signature:

jobject marker -> jdouble lat, jdouble lon, jstring iconId

Activity

  1. added
    Android
    performance
    on May 23, 2016
  2. tobrun commented on May 23, 2016

    @tobrun
    MemberAuthor

    I have been doing some profiling on this.

    As-is

    Here you can see the as-is situation. The top level selected method is animateValue and is called by the animator framework to update the position of the marker.

    screen shot 2016-05-24 at 00 29 22

    With the as-is situation 81.2 % is allocated to call setPosition and 3.5% is used for calculateValue.

    Proposed improvement

    Here you can see the proposed alternative. You can see the same method selected as above only this time it's calling the suggested improvement mentioned in OP.

    screen shot 2016-05-24 at 00 30 05

    With the proposed solution 58.7 % is allocated to call setPosition while 32.8% is used for calculateValue. This means we have decreased the execution time in relation with calculateValue dramatically.

    Putting both methods side-by-side in one execution. Shows the idea behind the OP:

    screen shot 2016-05-24 at 00 37 50

    You can see that the as-is situation results in 26 method calls, while the suggested solution results in 4.

  3. bleege commented on Jun 9, 2016

    @bleege
    Contributor

    Looks like this particular case, nativeUpdateMarker, was done in #5076 and then again in #5303.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Androidperformancerefactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions