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

(str) Hardcoded StringBuilder default capacity causes too many extendCapacity calls

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P5 P5
    • None
    • 5.0, 6u12
    • core-libs
    • x86, sparc
    • linux, solaris_10

      A DESCRIPTION OF THE REQUEST :

      J2SE 5 has an optimization that converts string concatenation to StringBuilder.append calls automatically.

      The problem is that these optimization use the default StringBuilder constructor, which has its initial capacity hardcoded to 16. This is a value which is way too low for existing implementations (which use string concatenation) and this causes lots of unneeded calls to StringBuilder.extendCapacity(). Under load, these calls waste both CPU time and memory.

      My suggestion is divided to two:

      1. Allow the default StringBuilder capacity to be read from a system property, so specific implementations can tune this value to minimize extra expandCapacity() calls

      2. The java compiler optimization should take the length of literal strings into account when performing this optimization - For example, if

      JUSTIFICATION :
      This will prevent many unneeded calls to expandCapacity(), which waste CPU time and memory.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      1. Default capacity of StringBuilder is configurable through a system property.

      2. Java compiler optimization taken into account the total length of literals when converting string concatenation to StringBuilder.append() calls.
      ACTUAL -

      1. StringBuilder default capacity is hardcoded in the class file.

      2. Java Compiler calls default StringBuilder constructor without taking literal string lengths into account.

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: