Skip to content
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

JNI benchmarks #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

JNI benchmarks #119

wants to merge 1 commit into from

Conversation

ivnsch
Copy link
Collaborator

@ivnsch ivnsch commented Jul 6, 2020

Some findings:

  1. JNI calls are at least (no parameters and return values) 3-5x slower than non JNI calls.
  2. Sending and returning an int primitive has about the same performance as sending / returning nothing.
  3. Converting Java strings to Rust strings (parameters) or returning Java strings from Rust is expensive. A call that returns a string is ~30x slower than a call that returns nothing.
  4. Passing or returning classes (even if only with an integer primitive) is very expensive. Passing is about 4x slower than sending a string. Returning is ~16x slower than returning a string. Note: "passing" means processing the fields, such that they're usable in Rust, and "returning", constructing an object that can be used in Kotlin. It would be interesting to compare this with point 3 using a JSON based api.

A general consequence of this would be to prefer to return int-only codes (instead of a class with code + optional message), at least for performance critical calls. Otherwise, it should be evaluated on per-case basis.

The documented benchmarks were done with a million iterations.

I expect iOS to be much quicker, as it shouldn't do any serialization / deserialization. Without the JSON based api, of course.

@ivnsch ivnsch force-pushed the test/benchmarks-android-ffi branch 2 times, most recently from d1368be to dd4f3de Compare July 6, 2020 11:26
@ivnsch ivnsch requested a review from a team July 6, 2020 11:28
@ivnsch ivnsch linked an issue Jul 6, 2020 that may be closed by this pull request
@ivnsch ivnsch force-pushed the test/benchmarks-android-ffi branch from dd4f3de to fc37c86 Compare July 6, 2020 12:00
Copy link
Collaborator

@duskoo duskoo left a comment

Choose a reason for hiding this comment

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

I wonder if Rust compiler optimizations are impacting test cases. There is a short discussion of the problem here: https://doc.rust-lang.org/1.2.0/book/benchmark-tests.html

@ivnsch
Copy link
Collaborator Author

ivnsch commented Jul 8, 2020

That's a good point. Rust here is compiled in debug mode, so there are probably no optimizations. The unused variables in e.g. sendCreateStringForBenchmarks and sendClassForBenchmarks did affect significantly the benchmark, which backs it too. I doubt that other than this, using release mode will affect the JNI performance, but we should try it out.

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.

Measure FFI performance (Android)
2 participants