Skip to content

Cache JNI jClass jfieldID lookups #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ dependency-reduced-pom.xml
.gradle
.settings
.classpath
.project
.project

# OSX
.DS_Store
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ RUN /usr/bin/c++ -c \
-laws-lambda-runtime \
-lcurl \
-static-libstdc++ \
-lrt
-lrt \
-O2
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ RUN /usr/bin/c++ -c \
-laws-lambda-runtime \
-lcurl \
-static-libstdc++ \
-static-libgcc
-static-libgcc \
-O2
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,45 @@

static aws::lambda_runtime::runtime * CLIENT = nullptr;

static jint JNI_VERSION = JNI_VERSION_1_8;

static jclass invocationRequestClass;
static jfieldID invokedFunctionArnField;
static jfieldID deadlineTimeInMsField;
static jfieldID idField;
static jfieldID contentField;
static jfieldID clientContextField = nullptr;
static jfieldID cognitoIdentityField = nullptr;
static jfieldID xrayTraceIdField = nullptr;


jint JNI_OnLoad(JavaVM* vm, void* reserved) {

JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION) != JNI_OK) {
return JNI_ERR;
}

jclass tempInvocationRequestClassRef;
tempInvocationRequestClassRef = env->FindClass("com/amazonaws/services/lambda/runtime/api/client/runtimeapi/InvocationRequest");
invocationRequestClass = (jclass) env->NewGlobalRef(tempInvocationRequestClassRef);
env->DeleteLocalRef(tempInvocationRequestClassRef);

idField = env->GetFieldID(invocationRequestClass , "id", "Ljava/lang/String;");
invokedFunctionArnField = env->GetFieldID(invocationRequestClass , "invokedFunctionArn", "Ljava/lang/String;");
deadlineTimeInMsField = env->GetFieldID(invocationRequestClass , "deadlineTimeInMs", "J");
contentField = env->GetFieldID(invocationRequestClass , "content", "[B");

return JNI_VERSION;
}

void JNI_OnUnload(JavaVM *vm, void *reserved) {
JNIEnv* env;
vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION);

env->DeleteGlobalRef(invocationRequestClass);
}

static void throwLambdaRuntimeClientException(JNIEnv *env, std::string message, aws::http::response_code responseCode){
jclass lambdaRuntimeExceptionClass = env->FindClass("com/amazonaws/services/lambda/runtime/api/client/runtimeapi/LambdaRuntimeClientException");
jstring jMessage = env->NewStringUTF(message.c_str());
Expand Down Expand Up @@ -56,50 +95,40 @@ JNIEXPORT jobject JNICALL Java_com_amazonaws_services_lambda_runtime_api_client_
return NULL;
}

jclass invocationRequestClass;
jfieldID invokedFunctionArnField;
jfieldID deadlineTimeInMsField;
jfieldID xrayTraceIdField;
jfieldID idField;
jobject invocationRequest;
jfieldID clientContextField;
jfieldID cognitoIdentityField;
jbyteArray jArray;
const jbyte* bytes;
jfieldID contentField;
auto response = outcome.get_result();

CHECK_EXCEPTION(env, invocationRequestClass = env->FindClass("com/amazonaws/services/lambda/runtime/api/client/runtimeapi/InvocationRequest"));
CHECK_EXCEPTION(env, invocationRequest = env->AllocObject(invocationRequestClass));

CHECK_EXCEPTION(env, idField = env->GetFieldID(invocationRequestClass , "id", "Ljava/lang/String;"));
CHECK_EXCEPTION(env, env->SetObjectField(invocationRequest, idField, env->NewStringUTF(response.request_id.c_str())));

CHECK_EXCEPTION(env, invokedFunctionArnField = env->GetFieldID(invocationRequestClass , "invokedFunctionArn", "Ljava/lang/String;"));
CHECK_EXCEPTION(env, env->SetObjectField(invocationRequest, invokedFunctionArnField, env->NewStringUTF(response.function_arn.c_str())));

CHECK_EXCEPTION(env, deadlineTimeInMsField = env->GetFieldID(invocationRequestClass , "deadlineTimeInMs", "J"));
CHECK_EXCEPTION(env, env->SetLongField(invocationRequest, deadlineTimeInMsField, std::chrono::duration_cast<std::chrono::milliseconds>(response.deadline.time_since_epoch()).count()));

if(response.xray_trace_id != ""){
CHECK_EXCEPTION(env, xrayTraceIdField = env->GetFieldID(invocationRequestClass , "xrayTraceId", "Ljava/lang/String;"));
if(xrayTraceIdField == nullptr){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are these conditions needed for xrayTraceIdField, clientContextField, cognitoIdentityField?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smirnoal Not all next responses contain them, so it would be more efficient to load them lazily than eagerly. Note that they are still cached once they are loaded.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move them to JNI_OnLoad to have all the fields initialisation in one place and reduce complexity here a bit. It would be good to know performance implications of this, I'm just assuming they are negligible

xrayTraceIdField = env->GetFieldID(invocationRequestClass , "xrayTraceId", "Ljava/lang/String;");
}
CHECK_EXCEPTION(env, env->SetObjectField(invocationRequest, xrayTraceIdField, env->NewStringUTF(response.xray_trace_id.c_str())));
}

if(response.client_context != ""){
CHECK_EXCEPTION(env, clientContextField = env->GetFieldID(invocationRequestClass , "clientContext", "Ljava/lang/String;"));
if(clientContextField == nullptr){
clientContextField = env->GetFieldID(invocationRequestClass , "clientContext", "Ljava/lang/String;");
}
CHECK_EXCEPTION(env, env->SetObjectField(invocationRequest, clientContextField, env->NewStringUTF(response.client_context.c_str())));
}

if(response.cognito_identity != ""){
CHECK_EXCEPTION(env, cognitoIdentityField = env->GetFieldID(invocationRequestClass , "cognitoIdentity", "Ljava/lang/String;"));
CHECK_EXCEPTION(env, env->SetObjectField(invocationRequest, cognitoIdentityField, env->NewStringUTF(response.cognito_identity.c_str())));
if(cognitoIdentityField == nullptr){
cognitoIdentityField = env->GetFieldID(invocationRequestClass , "cognitoIdentity", "Ljava/lang/String;");
}
CHECK_EXCEPTION(env, env->SetObjectField(invocationRequest, cognitoIdentityField, env->NewStringUTF(response.cognito_identity.c_str())));
}

bytes = reinterpret_cast<const jbyte*>(response.payload.c_str());
CHECK_EXCEPTION(env, jArray = env->NewByteArray(response.payload.length()));
CHECK_EXCEPTION(env, env->SetByteArrayRegion(jArray, 0, response.payload.length(), bytes));
CHECK_EXCEPTION(env, contentField = env->GetFieldID(invocationRequestClass , "content", "[B"));
CHECK_EXCEPTION(env, env->SetObjectField(invocationRequest, contentField, jArray));

return invocationRequest;
Expand Down