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

Pass StringBuilder's buffer directly to String

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 6
    • core-libs
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      Consider the example code. StringBuilder is not going to escape. A String is going to be created at the end. We can eliminate a memory allocation and buffer copy by simply having the String object reference the char[] in StringBuilder. Since the StringBuilder can't be used any more, the optimized code can simply have String reference the char[] in StringBuilder.

      JUSTIFICATION :
      Consider the example code. StringBuilder is not going to escape. A String is going to be created at the end. We can eliminate a memory allocation and buffer copy by simply having the String object reference the char[] in StringBuilder.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The char[] referenced in StringBuilder be passed to String.
      ACTUAL -
      A separate char[] is created for String and populated with the contents of StringBuilder's char[].

      ---------- BEGIN SOURCE ----------


      StringBuilder result;

      result = new StringBuilder();

      result.append("hello");
      ...

      return(result.toString());
      ---------- END SOURCE ----------

            Unassigned Unassigned
            rlewis Roger Lewis (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: