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 nativePixelForLatLng & nativeLatLngForPixel #5104

Closed

Description

@tobrun

In jni.cpp we have 2 methods that are called often:

  • nativePixelForLatLng
  • nativeLatLngForPixel

These methods are used to translate a pixel on the screen to a certain location coordinate and reversed.

Currently these methods look like:

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

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

    mbgl::ScreenCoordinate pixel = nativeMapView->getMap().pixelForLatLng(mbgl::LatLng(latitude, longitude));
    return &jni::NewObject(*env, *pointFClass, *pointFConstructorId, static_cast<jfloat>(pixel.x), static_cast<jfloat>(pixel.y));
}

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

    jfloat x = jni::GetField<jfloat>(*env, pixel, *pointFXId);
    jfloat y = jni::GetField<jfloat>(*env, pixel, *pointFYId);

    mbgl::LatLng latLng = nativeMapView->getMap().latLngForPixel(mbgl::ScreenCoordinate(x, y));
    return &jni::NewObject(*env, *latLngClass, *latLngConstructorId, latLng.latitude, latLng.longitude);
}

Each JNI method execution above results in 4 JNI rounds trips:

  • JNI method itself
  • jni::GetField
  • jni::GetField
  • jni::NewObject

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/ jni::NewObject items and replacing jobject pixel and jobject latlng with multiple parameters.

Activity

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

    @tobrun
    MemberAuthor

    I started looking into profiling small changes:

    Remove JNI calls to get LatLng values:

    jni::jobject* nativePixelForLatLng(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jobject* latLng) {
        mbgl::Log::Debug(mbgl::Event::JNI, "nativePixelForLatLng");
        assert(nativeMapViewPtr != 0);
        NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
    
        jdouble latitude = jni::GetField<jdouble>(*env, latLng, *latLngLatitudeId);
        jdouble longitude = jni::GetField<jdouble>(*env, latLng, *latLngLongitudeId);
    
        mbgl::ScreenCoordinate pixel = nativeMapView->getMap().pixelForLatLng(mbgl::LatLng(latitude, longitude));
        return &jni::NewObject(*env, *pointFClass, *pointFConstructorId, static_cast<jfloat>(pixel.x), static_cast<jfloat>(pixel.y));
    }
    
    jni::jobject* nativePixelForLatLng2(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jdouble latitude, jdouble longitude) {
        mbgl::Log::Debug(mbgl::Event::JNI, "nativePixelForLatLng2");
        assert(nativeMapViewPtr != 0);
        NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
    
        mbgl::ScreenCoordinate pixel = nativeMapView->getMap().pixelForLatLng(mbgl::LatLng(latitude, longitude));
        return &jni::NewObject(*env, *pointFClass, *pointFConstructorId, static_cast<jfloat>(pixel.x), static_cast<jfloat>(pixel.y));
    }

    Profiling execution time with 1000 calls:

    • nativePixelForLatLng: 74ms
    • nativePixelForLatLng2: 57ms

    Remove PointF object creation

    Profiling execution time with 1000 calls:

    • nativePixelForLatLng2: 38ms
    • nativePixelForLatLng3: 37ms
  3. tobrun commented on Jun 20, 2016

    @tobrun
    MemberAuthor
    Remove JNI calls to get Pixel values:
    jni::jobject* nativeLatLngForPixel(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jobject* pixel) {
        mbgl::Log::Debug(mbgl::Event::JNI, "nativeLatLngForPixel");
        assert(nativeMapViewPtr != 0);
        NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
    
        jfloat x = jni::GetField<jfloat>(*env, pixel, *pointFXId);
        jfloat y = jni::GetField<jfloat>(*env, pixel, *pointFYId);
    
        mbgl::LatLng latLng = nativeMapView->getMap().latLngForPixel(mbgl::ScreenCoordinate(x, y));
        return &jni::NewObject(*env, *latLngClass, *latLngConstructorId, latLng.latitude, latLng.longitude);
    }
    
    jni::jobject* nativeLatLngForPixel2(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jfloat x, jfloat y) {
        mbgl::Log::Debug(mbgl::Event::JNI, "nativeLatLngForPixel");
        assert(nativeMapViewPtr != 0);
        NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
        mbgl::LatLng latLng = nativeMapView->getMap().latLngForPixel(mbgl::ScreenCoordinate(x, y));
        return &jni::NewObject(*env, *latLngClass, *latLngConstructorId, latLng.latitude, latLng.longitude);
    }

    Profiling execution time with 1000 calls:

    • nativeLatLngForPixel1: 52ms
    • nativeLatLngForPixel2: 45ms

    Replace java object creation by array

    jni::jobject* nativePixelForLatLng2(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jdouble latitude, jdouble longitude) {
        mbgl::Log::Debug(mbgl::Event::JNI, "nativePixelForLatLng");
        assert(nativeMapViewPtr != 0);
        NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
    
        mbgl::ScreenCoordinate pixel = nativeMapView->getMap().pixelForLatLng(mbgl::LatLng(latitude, longitude));
        return &jni::NewObject(*env, *pointFClass, *pointFConstructorId, static_cast<jfloat>(pixel.x), static_cast<jfloat>(pixel.y));
    }
    
    jdoublearray nativeLatLngForPixel3(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jfloat x, jfloat y) {
        mbgl::Log::Debug(mbgl::Event::JNI, "nativeLatLngForPixel");
        assert(nativeMapViewPtr != 0);
        NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
        mbgl::LatLng latLng = nativeMapView->getMap().latLngForPixel(mbgl::ScreenCoordinate(x, y));
        jdoubleArray  output = env->NewDoubleArray(2);
        jdouble buf[2];
        buf[0] = latLng.latitude;
        buf[1] = latLng.longitude;
        env->SetDoubleArrayRegion(output, 0, 2, buf);
    
        if (output == nullptr) {
           env->ExceptionDescribe();
            return nullptr;
        }
    
        return output;
    }

    Profiling execution time with 1000 calls:

    • nativeLatLngForPixel2: 46ms
    • nativeLatLngForPixel3: 42ms
  4. added 2 commits that reference this issue on Jun 20, 2016
  5. self-assigned this
    on Jun 23, 2016
  6. tobrun commented on Jul 5, 2016

    @tobrun
    MemberAuthor

    I'm now looking into the possibility to used a cached field setup for high trafficking jni calls and see if that is faster.

    update: this approach was slower

  7. tobrun commented on Jul 5, 2016

    @tobrun
    MemberAuthor

    Closed with #5412 and squashed into 2bf4e20

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

Metadata

Metadata

Assignees

Labels

Androidperformancerefactor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions