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

Should use Integer.valueOf() instead of new Integer(), where appropriate



    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.6-rc-1
    • None
    • I have tested it only under Groovy 1.0, not under the upcoming 1.1
      But I guess it hasn't been fixed as this is the first time to come up in the discussions.


      This stems from a discussion in the list : http://www.nabble.com/forum/ViewPost.jtp?post=10789037&framed=y
      In that particular code, a Groovy expression such as:

      if (x <= 0)

      is translated into the Java equivalent of:

      if(ScriptBytecodeAdapter.compareLessThanEqual(x, new Integer(0)))

      This occurs in several other similar cases in that script's translation, and I'm guessing it also occurs in many other circumstances in the Groovy-->bytecode generation.
      At least for these cases, the suggested improvement would be to use Integer.valueOf(0), instead of creating a new Integer each time (this happens inside a loop, inside a recursive function!!)
      Many Java implementations (i.e. Sun's) return an immutable Integer from a pre-created Map of already-used integers, or for small values. For example, Sun has a pre-created list of Integer values
      ranging from -128 to +127.

      This small number comparisons and indexes (for example against zero) happen very often in normal algorithms and it could be a big improvement in speed and memory footprint to implement this "fix". Also, it seems to me that it's not such a complicated refactoring (but I haven't looked at the internals)


        Issue Links



              paulk Paul King
              barspi Barzilai Spinak
              0 Vote for this issue
              2 Start watching this issue