Files
gomobile-java/bind/testdata/interfaces.java.c.golden
Elias Naur ba0a725146 mobile/bind: avoid intermediate []rune copy converting Java string to Go
Converting a Go string to a string suitable use a specialized function,
UTF16Encode, that can encode the string directly to a malloc'ed buffer. That
way, only two copies are made when strings are passed from Go to Java; once
for UTF-8 to UTF-16 encoding and once for the creation of the Java String.

This CL implements the same optimization in the other direction, with a
UTF-16 to UTF-8 decoder implemented in C. Unfortunately, while calling into a
Go decoder also saves the extra copy, the Cgo overhead makes the calls much
slower for short strings.

To alleviate the risk of introducing decoding bugs, I've added the tests from
the encoding/utf16 package to SeqTest.

As a sideeffect, both Java and ObjC now always copy strings, regardless of
the argument mode. The cpy argument can therefore be removed from the string
conversion functions. Furthermore, the modeRetained and modeReturned modes
can be collapsed into just one.

While we're here, delete a leftover function from seq/strings.go that
wasn't removed when the old seq buffers went away.

Benchmarks, as compared with benchstat over 5 runs:

name                          old time/op  new time/op  delta
JavaStringShort               11.4µs ±13%  11.6µs ± 4%     ~     (p=0.859 n=10+5)
JavaStringShortDirect         19.5µs ± 9%  20.3µs ± 2%   +3.68%   (p=0.019 n=9+5)
JavaStringLong                 103µs ± 8%    24µs ± 4%  -77.13%   (p=0.001 n=9+5)
JavaStringLongDirect           113µs ± 9%    32µs ± 7%  -71.63%   (p=0.001 n=9+5)
JavaStringShortUnicode        11.1µs ±16%  10.7µs ± 5%     ~      (p=0.190 n=9+5)
JavaStringShortUnicodeDirect  19.6µs ± 7%  20.2µs ± 1%   +2.78%   (p=0.029 n=9+5)
JavaStringLongUnicode         97.1µs ± 9%  28.0µs ± 5%  -71.17%   (p=0.001 n=9+5)
JavaStringLongUnicodeDirect    105µs ±10%    34µs ± 5%  -67.23%   (p=0.002 n=8+5)
JavaStringRetShort            14.2µs ± 2%  13.9µs ± 1%   -2.15%   (p=0.006 n=8+5)
JavaStringRetShortDirect      20.8µs ± 2%  20.4µs ± 2%     ~      (p=0.065 n=8+5)
JavaStringRetLong             42.2µs ± 9%  42.4µs ± 3%     ~      (p=0.190 n=9+5)
JavaStringRetLongDirect       51.2µs ±21%  50.8µs ± 8%     ~      (p=0.518 n=9+5)
GoStringShort                 23.4µs ± 7%  22.5µs ± 3%   -3.55%   (p=0.019 n=9+5)
GoStringLong                  51.9µs ± 9%  53.1µs ± 3%     ~      (p=0.240 n=9+5)
GoStringShortUnicode          24.2µs ± 6%  22.8µs ± 1%   -5.54%   (p=0.002 n=9+5)
GoStringLongUnicode           58.6µs ± 8%  57.6µs ± 3%     ~      (p=0.518 n=9+5)
GoStringRetShort              27.6µs ± 1%  23.2µs ± 2%  -15.87%   (p=0.003 n=7+5)
GoStringRetLong                129µs ±12%    33µs ± 2%  -74.03%  (p=0.001 n=10+5)

Change-Id: Icb9481981493ffca8defed9fb80a9433d6048937
Reviewed-on: https://go-review.googlesource.com/20250
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-03-05 10:02:05 +00:00

191 lines
7.6 KiB
Plaintext

// JNI functions for the Go <=> Java bridge.
// gobind -lang=java interfaces
//
// File is generated by gobind. Do not edit.
#include <android/log.h>
#include <stdint.h>
#include "seq.h"
#include "interfaces.h"
#include "_cgo_export.h"
static jclass proxy_class_interfaces_Error;
static jmethodID proxy_class_interfaces_Error_cons;
static jmethodID mid_Error_Err;
static jclass proxy_class_interfaces_I;
static jmethodID proxy_class_interfaces_I_cons;
static jmethodID mid_I_Rand;
static jclass proxy_class_interfaces_I1;
static jmethodID proxy_class_interfaces_I1_cons;
static jmethodID mid_I1_H;
static jmethodID mid_I1_J;
static jclass proxy_class_interfaces_I2;
static jmethodID proxy_class_interfaces_I2_cons;
static jmethodID mid_I2_G;
static jmethodID mid_I2_f;
static jclass proxy_class_interfaces_I3;
static jmethodID proxy_class_interfaces_I3_cons;
static jmethodID mid_I3_F;
static jclass proxy_class_interfaces_WithParam;
static jmethodID proxy_class_interfaces_WithParam_cons;
static jmethodID mid_WithParam_HasParam;
JNIEXPORT void JNICALL
Java_go_interfaces_Interfaces_init(JNIEnv *env, jclass _unused) {
jclass clazz;
clazz = (*env)->FindClass(env, "go/interfaces/Interfaces$Error$Proxy");
proxy_class_interfaces_Error = (*env)->NewGlobalRef(env, clazz);
proxy_class_interfaces_Error_cons = (*env)->GetMethodID(env, clazz, "<init>", "(Lgo/Seq$Ref;)V");
clazz = (*env)->FindClass(env, "go/interfaces/Interfaces$Error");
mid_Error_Err = (*env)->GetMethodID(env, clazz, "Err", "()V");
clazz = (*env)->FindClass(env, "go/interfaces/Interfaces$I$Proxy");
proxy_class_interfaces_I = (*env)->NewGlobalRef(env, clazz);
proxy_class_interfaces_I_cons = (*env)->GetMethodID(env, clazz, "<init>", "(Lgo/Seq$Ref;)V");
clazz = (*env)->FindClass(env, "go/interfaces/Interfaces$I");
mid_I_Rand = (*env)->GetMethodID(env, clazz, "Rand", "()I");
clazz = (*env)->FindClass(env, "go/interfaces/Interfaces$I1$Proxy");
proxy_class_interfaces_I1 = (*env)->NewGlobalRef(env, clazz);
proxy_class_interfaces_I1_cons = (*env)->GetMethodID(env, clazz, "<init>", "(Lgo/Seq$Ref;)V");
clazz = (*env)->FindClass(env, "go/interfaces/Interfaces$I1");
mid_I1_J = (*env)->GetMethodID(env, clazz, "J", "()V");
clazz = (*env)->FindClass(env, "go/interfaces/Interfaces$I2$Proxy");
proxy_class_interfaces_I2 = (*env)->NewGlobalRef(env, clazz);
proxy_class_interfaces_I2_cons = (*env)->GetMethodID(env, clazz, "<init>", "(Lgo/Seq$Ref;)V");
clazz = (*env)->FindClass(env, "go/interfaces/Interfaces$I2");
mid_I2_G = (*env)->GetMethodID(env, clazz, "G", "()V");
clazz = (*env)->FindClass(env, "go/interfaces/Interfaces$I3$Proxy");
proxy_class_interfaces_I3 = (*env)->NewGlobalRef(env, clazz);
proxy_class_interfaces_I3_cons = (*env)->GetMethodID(env, clazz, "<init>", "(Lgo/Seq$Ref;)V");
clazz = (*env)->FindClass(env, "go/interfaces/Interfaces$I3");
mid_I3_F = (*env)->GetMethodID(env, clazz, "F", "()Lgo/interfaces/Interfaces$I1;");
clazz = (*env)->FindClass(env, "go/interfaces/Interfaces$WithParam$Proxy");
proxy_class_interfaces_WithParam = (*env)->NewGlobalRef(env, clazz);
proxy_class_interfaces_WithParam_cons = (*env)->GetMethodID(env, clazz, "<init>", "(Lgo/Seq$Ref;)V");
clazz = (*env)->FindClass(env, "go/interfaces/Interfaces$WithParam");
mid_WithParam_HasParam = (*env)->GetMethodID(env, clazz, "HasParam", "(Z)V");
}
JNIEXPORT jint JNICALL
Java_go_interfaces_Interfaces_Add3(JNIEnv* env, jclass clazz, jobject r) {
int32_t _r = go_seq_to_refnum(env, r);
int32_t r0 = proxyinterfaces__Add3(_r);
jint _r0 = (jint)r0;
return _r0;
}
JNIEXPORT void JNICALL
Java_go_interfaces_Interfaces_CallErr(JNIEnv* env, jclass clazz, jobject e) {
int32_t _e = go_seq_to_refnum(env, e);
nstring r0 = proxyinterfaces__CallErr(_e);
jstring _r0 = go_seq_to_java_string(env, r0);
go_seq_maybe_throw_exception(env, _r0);
}
JNIEXPORT jobject JNICALL
Java_go_interfaces_Interfaces_Seven(JNIEnv* env, jclass clazz) {
int32_t r0 = proxyinterfaces__Seven();
jobject _r0 = go_seq_from_refnum(env, r0, proxy_class_interfaces_I, proxy_class_interfaces_I_cons);
return _r0;
}
JNIEXPORT void JNICALL
Java_go_interfaces_Interfaces_00024Error_00024Proxy_Err(JNIEnv* env, jobject this) {
int32_t o = go_seq_to_refnum(env, this);
nstring r0 = proxyinterfaces_Error_Err(o);
jstring _r0 = go_seq_to_java_string(env, r0);
go_seq_maybe_throw_exception(env, _r0);
}
nstring cproxyinterfaces_Error_Err(int32_t refnum) {
JNIEnv *env = go_seq_push_local_frame(10);
jobject o = go_seq_from_refnum(env, refnum, proxy_class_interfaces_Error, proxy_class_interfaces_Error_cons);
(*env)->CallVoidMethod(env, o, mid_Error_Err);
jstring exc = go_seq_get_exception_message(env);
nstring _exc = go_seq_from_java_string(env, exc);
go_seq_pop_local_frame(env);
return _exc;
}
JNIEXPORT jint JNICALL
Java_go_interfaces_Interfaces_00024I_00024Proxy_Rand(JNIEnv* env, jobject this) {
int32_t o = go_seq_to_refnum(env, this);
int32_t r0 = proxyinterfaces_I_Rand(o);
jint _r0 = (jint)r0;
return _r0;
}
int32_t cproxyinterfaces_I_Rand(int32_t refnum) {
JNIEnv *env = go_seq_push_local_frame(10);
jobject o = go_seq_from_refnum(env, refnum, proxy_class_interfaces_I, proxy_class_interfaces_I_cons);
jint res = (*env)->CallIntMethod(env, o, mid_I_Rand);
int32_t _res = (int32_t)res;
go_seq_pop_local_frame(env);
return _res;
}
JNIEXPORT void JNICALL
Java_go_interfaces_Interfaces_00024I1_00024Proxy_J(JNIEnv* env, jobject this) {
int32_t o = go_seq_to_refnum(env, this);
proxyinterfaces_I1_J(o);
}
void cproxyinterfaces_I1_J(int32_t refnum) {
JNIEnv *env = go_seq_push_local_frame(10);
jobject o = go_seq_from_refnum(env, refnum, proxy_class_interfaces_I1, proxy_class_interfaces_I1_cons);
(*env)->CallVoidMethod(env, o, mid_I1_J);
go_seq_pop_local_frame(env);
}
JNIEXPORT void JNICALL
Java_go_interfaces_Interfaces_00024I2_00024Proxy_G(JNIEnv* env, jobject this) {
int32_t o = go_seq_to_refnum(env, this);
proxyinterfaces_I2_G(o);
}
void cproxyinterfaces_I2_G(int32_t refnum) {
JNIEnv *env = go_seq_push_local_frame(10);
jobject o = go_seq_from_refnum(env, refnum, proxy_class_interfaces_I2, proxy_class_interfaces_I2_cons);
(*env)->CallVoidMethod(env, o, mid_I2_G);
go_seq_pop_local_frame(env);
}
JNIEXPORT jobject JNICALL
Java_go_interfaces_Interfaces_00024I3_00024Proxy_F(JNIEnv* env, jobject this) {
int32_t o = go_seq_to_refnum(env, this);
int32_t r0 = proxyinterfaces_I3_F(o);
jobject _r0 = go_seq_from_refnum(env, r0, proxy_class_interfaces_I1, proxy_class_interfaces_I1_cons);
return _r0;
}
int32_t cproxyinterfaces_I3_F(int32_t refnum) {
JNIEnv *env = go_seq_push_local_frame(10);
jobject o = go_seq_from_refnum(env, refnum, proxy_class_interfaces_I3, proxy_class_interfaces_I3_cons);
jobject res = (*env)->CallObjectMethod(env, o, mid_I3_F);
int32_t _res = go_seq_to_refnum(env, res);
go_seq_pop_local_frame(env);
return _res;
}
JNIEXPORT void JNICALL
Java_go_interfaces_Interfaces_00024WithParam_00024Proxy_HasParam(JNIEnv* env, jobject this, jboolean p0) {
int32_t o = go_seq_to_refnum(env, this);
char _p0 = (char)p0;
proxyinterfaces_WithParam_HasParam(o, _p0);
}
void cproxyinterfaces_WithParam_HasParam(int32_t refnum, char p0) {
JNIEnv *env = go_seq_push_local_frame(12);
jobject o = go_seq_from_refnum(env, refnum, proxy_class_interfaces_WithParam, proxy_class_interfaces_WithParam_cons);
jboolean _p0 = p0 ? JNI_TRUE : JNI_FALSE;
(*env)->CallVoidMethod(env, o, mid_WithParam_HasParam, _p0);
go_seq_pop_local_frame(env);
}