Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cost of invoking a method on Android

I created a method that's only called in one place - from onBindViewHolder() in a RecyclerView. It was a logical unit of code, and I think that extracting that code block into a method improved readability. However, during a code review, I was advised that the method invocation was expensive, so it would negatively impact performance, and that I should inline the code rather than putting it in a separate method.

I thought the JVM or compiler would optimize this code by inlining the method, but I'm not sure if that's the case on Android. I haven't really been able to find any concrete information about what kind of optimizations the new ART JVM does.

Is invoking a method so expensive on Android that I should avoid it at the cost of readability in places where the method might get called many times? Also, is creating single-use methods like this frowned upon because of the DEX method limit? (this app is already using multidex).

This question is not a duplicate of other similar java questions, because I'm asking specifically about the performance on Android, which has it's own idiosyncrasies.

like image 717
starkej2 Avatar asked Apr 20 '16 18:04

starkej2


2 Answers

I totally disagree. In theory, method calls do add a bit of overhead. Things have to be pushed onto the stack and then a jump to the method. But the overhead is trivial.

Premature optimization is never a good idea. Benchmark your application and figure out where the real performance issues are. I'm positive that it won't be because of a single method call, even one that is called frequently. How that method is implemented might be an issue but not the call itself.

like image 142
stdunbar Avatar answered Sep 23 '22 16:09

stdunbar


However, during a code review, I was advised that the method invocation was expensive, so it would negatively impact performance, and that I should inline the code rather than putting it in a separate method.

Any advice of that nature during a code review is probably ill-founded.

  1. The cost of a method call only matters if the method is called frequently.

  2. The cost of a method call is going depend on how good the compiler / optimizer are. These will change over time, as the Android compilers evolve. In fact, it is likely that the method calls become less expensive ... until they reach the point that the compilers are as good as (or better than) the average programmer at optimizing straight-forward code.

My advice to you and your code reviewers is to avoid premature optimization. Get your app working, and see if it performs well enough. If not, then profile it to determine where the REAL performance bottlenecks are, and optimize (only) them.

Is invoking a method so expensive on Android that I should avoid it at the cost of readability in places where the method might get called many times?

Erm ... no. Most method calls do not contribute to the performance bottlenecks. And indeed, a lot of performance bottlenecks don't contributed to the perceived performance. For a typical interactive app, it is only slowness / laggyness that impacts on the user experience that really matters.

Any one-size-fits all advice to avoid method calls is missing the point.

Code, test, measure, profile ... and THEN optimize.

like image 28
Stephen C Avatar answered Sep 23 '22 16:09

Stephen C