This discussion is archived
4 Replies Latest reply: Sep 29, 2010 1:21 PM by jschellSomeoneStoleMyAlias RSS

How to insure range check elimination optimization is occurring

801436 Newbie
Currently Being Moderated
I'm wondering if there is a way to tell whether or not the optimizer is employing the range check elimination optimization on a particular loop. Although I'd rather not, I can generate the assembly code via -XX:+PrintOptoAssembly, but I'm not exactly sure what I'm looking for. Any help is greatly appreciated.

I'm also wondering if the range check optimization only occurs for arrays, or if it is used when the object being accessed is a List for example, although once I have an answer to my first question, I can investigate this on my own.

Thanks!
  • 1. Re: How to insure range check elimination optimization is occurring
    796440 Guru
    Currently Being Moderated
    I don't think you can tell for sure whether it will occur on a given loop. It's only spec'ed that it can do it when hotspot can prove that the index is always in bounds.

    Also, it's only for arrays, not Lists, although you can look at the code for ArrayList and see if it's doing an explicit check there or just relying on the fact that it's an array access. Of course, that only tells you about that particular implementation.

    Why is it that you want to know this? Is there a particular loop that you've identified as a bottleneck? If you find out that the optimization is or is not being done, what will you do as a result?
  • 2. Re: How to insure range check elimination optimization is occurring
    801436 Newbie
    Currently Being Moderated
    There are a number of loops that I've identified as bottlenecks. If the optimization is not being done (but could be done), then I was hoping to re-write the loops in a way that allows hotspot to prove the index is in bounds. I've just learned that this is an available optimization, but I haven't seen enough examples to be able to figure out when the optimization can/will occur.
  • 3. Re: How to insure range check elimination optimization is occurring
    796440 Guru
    Currently Being Moderated
    I suspect it's left deliberately unspecified to allow flexibility among versions and platforms, and even within a given run on a given JVM. However, everything I've read strongly suggests that [url http://java.sun.com/developer/technicalArticles/Interviews/devinsight_1/]writing dumb code, such as your basic, standard array loop, will enable that optimization to be performed (as hotspot is specifically tuned to common idioms):
    for (int i = 0; i < arr.length; i++) {
      // do stuff that does change the value of i or arr
    }
    Alternatively, using a foreach loop should be expected to generate this kind of code.

    As for the bottleneck, is what you're doing in one loop iteration so small that it's reasonable to expect that that operation, plus incrementing i will not dwarf the array bounds comparison? Or have you been able to determine that the check itself is the bottleneck?
  • 4. Re: How to insure range check elimination optimization is occurring
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    798433 wrote:
    There are a number of loops that I've identified as bottlenecks.
    Naturally if you could eliminate the loops rather than attempting to make them faster then that would be a better optimization. Regardless of how it is optimized.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points