Cost of invoking a method on Android
Asked Answered
V

2

5

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.

Vicenta answered 20/4, 2016 at 18:13 Comment(5)
Possible duplicate of java how expensive is a method callCelie
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? I don't have numbers, but definitely not. The VM will do optimizations as it runs, and so will the compiler. Write maintainable, easy to read code.Teddie
If there is a performance issue you should get data before optimizing, if you are not getting data (e.g. benchmarking) then you are not optimizing but taking guesses at what would improve performance.Haddington
Using the method didn't cause a performance issue, but I was hoping someone would have a good resource that I could use to backup my point that it's okay to use a method in this case. I tried to argue that it's better to write readable and well-designed code, then optimize for performance where necessary, but I lost that battle. Performing some benchmarking is a good idea, but if the method causes even a small performance hit I have a feeling I'll be told not to use one.Vicenta
To answer this question, I would just do a comparison of the time it would take to complete the bindviewholder method with the method being called vs the method being inlined. That would give you a solid ground to work with to determine which way is more efficient. However, I would say the difference would only be a few milliseconds.Knurl
C
7

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.

Constanceconstancia answered 20/4, 2016 at 18:26 Comment(1)
Agreed. Code readability and maintainability are more important imo. Code that is hard to maintain will become a performance hog in itself simply because the code cannot be easily understood to improve its performance.Casavant
S
2

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.

Stuck answered 30/4, 2016 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.