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) {
- csr of
-
JDK-8333396 Use StringBuilder internally for java.text.Format.* formatting
- Resolved
- relates to
-
JDK-8336787 Examine java.text.Format API for implSpec usage
- Resolved