Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-3123

while(true) {} could be optimized

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.5.7, 1.6-beta-2
    • 1.6-rc-1, 1.5.8, 1.7-beta-1
    • Compiler
    • None
    • All

    Description

      given the following loop:

      while(true) {
      }

      generates the following bytecode:

      48: getstatic #61; //Field java/lang/Boolean.TRUE:Ljava/lang/Boolean;
      51: invokestatic #67; //Method org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.booleanUnbox:(Ljava/lang/Object;)Z
      54: ifeq 60
      57: goto 48

      javac performs one of two optimization here depending on the body of the loop, which groovyc could easily do as well.
      1. If the body of the loop is empty, don't generate any bytecode at all.
      2. If the body of the loop is non-empty, then simply generate a goto to jump to the top of the loop, there's no need to getstatic, invokestatic, and ifeq.

      Since this kind of loop is a common idiom, it is worth optimizing in my opinion.

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            didge didge
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: