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

Use StringBuilder internally for java.text.Format.* formatting

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 24
    • core-libs
    • None
    • behavioral
    • low
    • Hide
      Low risk as the replacement applies only to classes in `java.base` module, and is done via a proxy thus existing external subclasses use `StringBuffer` as before. Additionally, it is unlikely that any users relied on the supplementary information that is changed.
      Show
      Low risk as the replacement applies only to classes in `java.base` module, and is done via a proxy thus existing external subclasses use `StringBuffer` as before. Additionally, it is unlikely that any users relied on the supplementary information that is changed.
    • Java API
    • SE

      Summary

      Replace StringBuffer with StringBuilder for java.text.Format and implementing subclasses in the java.base module.

      Problem

      java.text.Format and implementing subclasses use the thread-safe StringBuffer as the string buffer internally for formatting. After the obsoleting of biased locking (JEP 374), there is a notable performance degradation when formatting.

      Solution

      To address this, this change introduces the non-thread safe StringBuilder as the internal string buffer without violating the API, so that compatibility is preserved. This can be done because the Format classes are not synchronized, and in fact, use StringBuffer simply because the classes predate StringBuilder.

      However, as a result, supplementary description in java.text.Format and java.text.MessageFormat which states that certain methods are equivalent to invoking their StringBuffer overloads, are no longer true. Instead of stating that the methods calls are equivalent, we can update the description to state that the output returned by both methods would be equivalent.

      Specification

      In java.text.Format,

           /**
      -     * Formats an object to produce a string. This is equivalent to
      +     * Formats an object to produce a string.
      +     * This method returns a string that would be equal to the string returned by
            * <blockquote>
            * {@link #format(Object, StringBuffer, FieldPosition) format}<code>(obj,
            *         new StringBuffer(), new FieldPosition(0)).toString();</code>
            * </blockquote>
            *
            * @param obj    The object to format
            * @return       Formatted string.
            * @throws    IllegalArgumentException if the Format cannot format the given
            *            object
            */
           public final String format (Object obj) {

      In java.text.MessageFormat,

           /**
            * Creates a MessageFormat with the given pattern and uses it
      -     * to format the given arguments. This is equivalent to
      +     * to format the given arguments.
      +     * This method returns a string that would be equal to the string returned by
            * <blockquote>
            *     <code>(new {@link #MessageFormat(String) MessageFormat}(pattern)).{@link #format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) format}(arguments, new String
      Buffer(), null).toString()</code>
            * </blockquote>
            *
            * @param pattern   the pattern string
            * @param arguments object(s) to format
            * @return the formatted string
            * @throws    IllegalArgumentException if the pattern is invalid,
            *            or if an argument in the {@code arguments} array
            *            is not of the type expected by the format element(s)
            *            that use it.
            * @throws    NullPointerException if {@code pattern} is {@code null}
            */
           public static String format(String pattern, Object ... arguments) {

            syan Sendao Yan
            syan Sendao Yan
            Justin Lu, Naoto Sato
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: