-
Notifications
You must be signed in to change notification settings - Fork 144
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
Optimize Secure Message length #777
Conversation
Instead of immediately returning from this function, previously allocated resources need to be freed first. Also, use "goto err" just for the sake of error handling consistency.
JavaThemis has the same issue as GoThemis and ThemisPP had: it disregards the real size of Secure Message output as reported by the second call to themis_secure_message_...() functions. The first call will only report an upper bound to the output length, not the actual length--which is only know after processing. Unfortunately, JVM arrays cannot be resized after they are allocated and JavaThemis API returns plain arrays. Hence, in order for the array to have correct size, we need to buffer Themis output in the native code before allocating a JVM array. This has a side effect of adding more allocations to the happy path: previously, if you were lucky, Themis Core could write directly into JVM heap and no copying or reallocation would have taked place. Now JNI code always allocates a temporary buffer and makes a copy of it. (Another minor side effect is that in case of a failure JNI code no longer allocates a JVM array beforehand, slightly relieving the GC pressure.)
777 GET!
Port the test for overlong Secure Message signatures from GoThemis and ThemisPP into Java land. Just to make sure.
goto err; | ||
} | ||
|
||
output_bytes = (*env)->GetPrimitiveArrayCritical(env, output, NULL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so we pause garbage collectors here, moves bytes from output_buffer
to output_bytes
, and un-pause garbage collectors, right?
i followed description of GetPrimitiveArrayCritical
at SO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so we pause garbage collectors here, moves bytes from output_buffer to output_bytes, and un-pause garbage collectors, right?
Basically yeah. Either pause to synchronize access, or making a copy and synchronizing copying it back.
This may or may not have an effect on the overall system. I'm sure there are effects to using both critical and non-critical variants, but I'm not an expert on Dalvik or other JVMs so have no clue on how strong those effects can be.
Following #775, make the same changes to JavaThemis to slightly reduce the length of Secure Message signatures. At a cost, though.
JavaThemis has the same issue as GoThemis and ThemisPP had: it disregards the real size of Secure Message output as reported by the second call to
themis_secure_message_...()
functions. The first call will only report an upper bound to the output length, not the actual length—which is only know after processing.Unfortunately, JVM arrays cannot be resized after they are allocated and JavaThemis API returns plain arrays. Hence, in order for the array to have correct size, we need to buffer Themis output in the native code before allocating a JVM array.
This has a side effect of adding more allocations to the happy path: previously, if you were lucky, Themis Core could write directly into JVM heap and no copying or reallocation would have taken place. Now JNI code always allocates a temporary buffer and makes a copy of it.
(Another minor side effect is that in case of a failure JNI code no longer allocates a JVM array beforehand, slightly relieving the GC pressure.)
Checklist
Benchmark results are attached