JIT not optimizing loop that involves Integer.MAX_VALUE

安稳与你 提交于 2019-11-28 20:03:21

I have not dug up the Java Language Specification, but I'd guess that it has to do with this difference:

  • i++ < (Integer.MAX_VALUE - 1) never overflows. Once i reaches Integer.MAX_VALUE - 1 it is incremented to Integer.MAX_VALUE and then the loop terminates.

  • i++ < Integer.MAX_VALUE contains an integer overflow. Once i reaches Integer.MAX_VALUE, it is incremented by one causing an overflow and then the loop terminates.

I assume that the JIT compiler is "reluctant" to optimize-out loops with such corner conditions - there was a whole bunch of bugs w.r.t. loop optimization in integer overflow conditions, so that reluctance is probably quite warranted.

There may also be some hard requirement that does not allow integer overflows to be optimized-out, although I somehow doubt that since integer overflows are not directly detectable or otherwise handled in Java.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!