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

StringBuilder should predict how large a buffer is needed

XMLWordPrintable

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

      A DESCRIPTION OF THE REQUEST :
      The enhancement request is this... predict how large a buffer is needed and pass that to StringBuilder's constructor.

      A first attempt might be to consider the longest-case. Each appended int will take 1+11 chars (sign + digits), each appended char will take 1, etc. All of the fundamental data types have a known maximum encoding length. Strings are dynamic length but their length can easily be computed. Other Objects could have toString() called before the StringBuilder's constructor is called and then use the resulting String's length.

      A refined attempt would be to add 3 statistics to the instrumented code. They are count, sum and sum of squares. As the instrumented code executes, count is incremented and the final length of StringBuilder is added to sum. The square of the length is added to sum of squares.

      When the code is finally optimized, these statistics are used to compute average and standard deviation. The value passed to StringBuilder's constructor is hard coded as average + standard deviation. The optimized code will create a buffer that will suffice for 68% of all of the method calls.

      Note: Statistics are not collected in the optimized code.



      JUSTIFICATION :
      Consider the example code below. StringBuilder is created with a default buffer size yet the code is extremely predictable in the length of the buffer required. If StringBuilder's default buffer size is too small, then there might be 1 or more allocations required. If StringBuilder's default buffer size is too large, then we will hit GC sooner.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The optimized code passes a hard coded value to StringBuilder so that the buffer is appropriately sized.
      ACTUAL -
      StringBuilder allocates a default-sized buffer. Either the buffer has to be resized one or more times or the buffer is too large.

      ---------- BEGIN SOURCE ----------
      ...

               StringBuilder result;
               
               result = new StringBuilder();
               
               result.append(system.year);
               result.append('/');
               result.append(system.month);
               result.append('/');
               result.append(system.day);
               result.append(' ');
               
               if (system.hour > 12)
                  result.append(system.hour - 12);
               else
                  result.append(system.hour);
               
               result.append(':');
               
               if (system.minute < 10)
                  result.append('0');
               
               result.append(system.minute);
               result.append(':');
               
               if (system.second < 10)
                  result.append('0');
               
               result.append(system.second);
               result.append('.');
               
               if (system.milliseconds < 100)
                  result.append('0');
               
               if (system.milliseconds < 10)
                  result.append('0');
               
               result.append(system.milliseconds);
               result.append(' ');
               
               if (system.hour > 12)
                  result.append('P');
               else
                  result.append('A');
               
               result.append('M');
               
               return(result.toString());

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Manually computing and hardcoding the buffer size in trillions of lines of code.

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: