0

Is there any performance detriment to assigning variables vs using them inline. I'm guessing this WOULD be worse if a method was returning primitive and I was 'boxing' it (e.g. method returning int, and me storing to Integer). What about something like the below?

String foo = buildFoo();
String bar = buildBar(foo);
return bar;

vs.

return buildBar(buildFoo());

For context as to why I'm asking (given some votes):

  1. I'm specifically asking this question because I am on a team and someone more senior than me told me specifically not to store values, but return them directly for optimization reasons.
  2. From what I've found online it seems if you box a primitive in a loop where you only need a primitive, it will still box and not optimize that out so it seems the optimizations done are not as robust as I would expect
  3. There is a-lot of confusion for me around java's compilation(s) and optimizations, and this only get compounded by multiple vendors and understanding if certain optimizations are java standards or vendor standards

I've studied compilers and optimizations at the VLSI level, but there's a difference to me in 'likely optimized out' and 'IS optimized out' especially when I'm getting feedback that it's not, and some things that seem trivial to optimize out seemingly aren't.

2
  • 3
    Local variables should be considered free from a performance perspective, and self-documenting from a readability perspective, so feel free to use local variables to keep the code simple and clear; to keep expressions simple. This is also a debugging technique: simplify lines and interconnect them with variables to get better focus on compiler and/or runtime error messages; to better see intermediate results.
    – Erik Eidt
    Mar 14, 2022 at 16:26
  • 1
    Unless you have profiling data telling you that your app spends most of its cycles in those specific lines, this is most of all an absurd premature optimization. The thing you should be much more interested in is which of the options is more readable. Mar 22, 2022 at 12:09

1 Answer 1

5

The JVM and Java compiler have been continuously optimized for literally decades now, so its extremely unlikely that they would miss a trick so basic that this would make a difference.

However, we don't have to speculate. Simply inspect the byte code that both code fragments generate and check whether there is even a difference.

1
  • 6
    The JVM bytecode does make a distinction between local variables and values on the stack. But the bytecode is not the level on which optimizations are performed. That would be the job of the JIT compiler. And eliding unnecessary stores/loads in favour of storing intermediate values in registers is an extremely common optimization to do.
    – amon
    Mar 14, 2022 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.