Home > database >  Does the java compiler know how to optimize such code?
Does the java compiler know how to optimize such code?

Time:08-17

While debugging SpringBoot sources, I began to notice similar code often:

String username = determineUsername(authentication);

private String determineUsername(Authentication authentication) {
    return (authentication.getPrincipal() == null) ?
           "NONE_PROVIDED" : authentication.getName();
}

The determineUsername method is called only 1 time, and has only 1 line of code.This code is common, and I had 2 questions.

  1. Does the Java compiler know how to optimize method calls if it sees that they are called only 1 time?
  2. Does such code constructs affect the speed of work, on the scale of a large application (for example, Spring Boot itself)?

CodePudding user response:

  1. Does the Java compiler know how to optimize method calls if it sees that they are called only 1 time?

Knows how to, and does are different things.

The JIT compiler knows how to optimize it. But on the flipside, it also knows that since it only calls it once, it is not worth optimizing it.

The latter "knowledge" is encoded as a policy that the JIT compiler uses to decide when to compile and optimize. A method will only be compiled if the stats gathering determines that it already has been used enough to be worth compiling.

If a method like the above is only called only 1 time, the JIT compiler knows not to compile it.

(However, I suspect that you are confusing the number of times that the method is called with the number of places it is called from. And that could change the analysis.)

  1. Does such code constructs affect the speed of work, on the scale of a large application (for example, Spring Boot itself)?

In a scenario like described, the time take to execute (interpret) the call once will be measured in 100s of nano-seconds. But the time taken to compile and optimize is likely to be measure in 10s of micro-seconds.

Even (hypothetically!) if you had hundreds of thousands of examples on a (large) SpringBoot-based application, the mathematics is the same. It doesn't pay to compile and optimize code that is rarely used. In fact, you will waste CPU time by doing that.

So ... for the scenario you described the answer is again No.


You commented:

... and if there are a million such calls? I want to know if this will affect performance.

Well yes. That is a different scenario.

If there is one call-site for the above method, but the call is executed frequently (e.g. once per request processed), then the JVM is likely to decide to compile and optimize the method ... after a few calls have been made.

Note that the JIT compiler makes use of statistics gathered by the interpreter to decide how to optimize code. If it didn't interpret first, it wouldn't know that (for example) that user.getPrincipal() normally returns a null. Therefore, it wouldn't be able to work out know the optimal sequence for the branch instructions.

  •  Tags:  
  • java
  • Related