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

Conversation

richarddd
Copy link
Contributor

Description of changes:

JNI class/fieldID lookup can be an expensive operation. This PR moves out the class/fieldId lookup outside the next call to minimise the overhead.

More information:
https://developer.android.com/training/articles/perf-jni#jclass,-jmethodid,-and-jfieldid
https://developer.ibm.com/articles/j-jni/#notc
http://normanmaurer.me/blog/2014/01/07/JNI-Performance-Welcome-to-the-dark-side/

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@andclt andclt requested review from carlzogh and smirnoal August 10, 2022 09:03
@msailes
Copy link
Collaborator

msailes commented Aug 10, 2022

@smirnoal can you take a look at this?

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

@smirnoal
Copy link
Contributor

thanks Richard

@smirnoal smirnoal merged commit 6a3ffd5 into aws:master Aug 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants