Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6709423

Optimization of String concatenation '+=' expressions

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      A DESCRIPTION OF THE REQUEST :
      The code:
          s = s + a + b; // (1)
      is compiled into the following equivalent code:
          s = (new StringBuilder()).append(s).append(a).append(b).toString();

      In contrast, the code:
          s += a; // (2)
          s += b;
      compiles into:
          s = (new StringBuilder()).append(s).append(a).toString();
          s = (new StringBuilder()).append(s).append(b).toString();

      The latter case could be optimized by the compiler/code generator into the same code as the first case (1) above. This would reduce the number of objects allocated and improve performance.

      Related bug: 4059189

      JUSTIFICATION :
      Improved performance.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      (See above.)
      ACTUAL -
      (See above.)

      ---------- BEGIN SOURCE ----------
      (See code fragements above.)
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use sequences of '+' operations in a single (large) statement, instead of coding separate '+=' statements.

        Attachments

          Activity

            People

            Assignee:
            vromero Vicente Arturo Romero Zaldivar
            Reporter:
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: