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

Improve String.join() performance for large String arrays

    Details

      Description

      A DESCRIPTION OF THE PROBLEM :
      ## initial problem:

      if i have a set of `char[]` it's expensive in terms of memory to combine/concat them into one String

      current optimal behavior is:

      - calculate total length
      - allocate StringBuilder with corresponding size
      - append all char[] arrays into StringBuilder
      - cast StringBuilder to String

      but at one moment there are three copies of my `char[]` data in the memory:

      - original set of `char[]`
      - StringBuilder.value[] (the internal buffer of StringBuilder)
      - `StringBuilder.toString()` calls `String(value, 0, count)` that makes a copy if internal buffer of StringBuilder

      I tried to use `String.join(CharSequence delimiter, CharSequence... elements)` but it uses the same algorithm and even worth - it allocates StringBuilder dynamically

      ## the idea

      rewrite `String.join(CharSequence delimiter, CharSequence... elements)` with the following algorithm

      - calculate total length (get length of all CharSequences)
      - allocate StringBuilder with corresponding size
      - append all CharSequences into StringBuilder
      - use `String( stringBuilder.getValue() , true)` constructor with shared buffer to avoid extra copy of data


      to remove the last copy


        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: