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

Request improved code for string concatenation

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 1.4.0
    • tools
    • generic
    • solaris_7

      for
      String s1, s2;
      s1 += s2;

      we can generate slightly better code by using

      s1 = s1.append(s2);

      which allocates twice: a String and a byte[] once, compared to what we currently generate

      new StringBuffer().append(s1).append(s2).toString();

      which allocates at least thrice: a StringBuffer, a byte[], a String, and possible more byte[] arrays during buffer expansion. An intermediary would be

      new StringBuffer(s1).append(s2).toString();

      which would at least avoid expansion for the first append.

      Apparently the old compiler performed this optimization.

            gafter Neal Gafter (Inactive)
            gafter Neal Gafter (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: