Will the Java optimizer remove the parameter construct for empty method calls?

Suppose I have code like:

log.info("Now the amount" + amount + " seems a bit high") 

and I would replace the log method with a dummy implementation, for example:

 class Logger { ... public void info() {} } 

Will the optimizer be built into it and removing dead code will remove the parameter structure if no side effects are detected?

+2
source share
2 answers

I would suggest that the compiler (javac) will not, but it is very likely that the compiler is on time.

For it to work, it must be able to subtract that any instructions you use to generate the parameter have no side effects. It should be able to do this for a special case of strings, but it may not be for other methods.

Of course, do a little test that compares this with the code suggested by Jesper, and see, depending on which is faster or if they are equally fast.

Also see here: http://www.ibm.com/developerworks/java/library/j-jtp12214/index.html#3.0

+3
source

Important answer: he can do .

I do not think you should rely on any specific optimizer behavior. If your code works fast enough without optimization, then you may or may not improve performance, but the good news is that your solution will be fine. If performance is low when it is not optimized, this is not a good solution.

The problem with optimization is that you are setting yourself up to match an invisible contract that you are not aware of. Hotspot will work differently in other versions of the JDK / JRE, so there is no guarantee that just because it works fine on your specific JVM, it will work fine elsewhere. But beyond that, accurate optimization may depend on environmental issues such as the number of free heaps, the number of cores on a machine, etc.

And even if you manage to confirm it, it works fine in your situation right now, you just made your code base incredibly unstable. I know that one of the optimizations / inlinings that Hotspot does is dependent on the number of subclasses loaded and used for the unspecified class. If you start using another library / module loading the second log implementation - BANG, the optimization unwinds and the performance is again terrible. And good luck developing how adding a third-party library to your classpath improves the performance of your application ...


In any case, I do not think that you are asking a real question. In the case that you described, the best solution is not to change the implementation of the info method, but to change the calls to no-ops (i.e. Their comment). If you want to do this through a whole group of classes at the same time, at compile time , you can use the IFDEF type, for example:

 public class Log { public static final boolean USE_INFO = true; public void info() { ... } ... } 

and then in your class:

 if (Log.USE_INFO) { log.info("Now the amount" + amount + " seems a bit high"); } 

Now the compiler (javac, not Hotspot) can see that the logical condition is constant and will be deleted during compilation. If you set the boolean flag to false and recompile, then all information instructions will be completely deleted from the file, since javac can say that they will never be called.

If you want to enable / disable information logging at run time, then instead of a constant Boolean flag, you will need to use the method, and there is nothing here but to call the method every time. Depending on the implementation of the method, Hotspot may optimize the scan, but, as I mentioned above, do not count on it.

+1
source

Source: https://habr.com/ru/post/1313542/


All Articles