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

Optimization of String concatenation '+=' expressions

XMLWordPrintable

    • x86
    • windows_xp

      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.

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: