Potential StringBuffer to StringBuilder clean-up (where warranted)

Martijn Verburg martijnverburg at gmail.com
Fri May 4 07:17:03 PDT 2012


Hi all,

Stefan Reich has submitted a patch to the Adopt OpenJDK program which
we're looking to pre-review that converts (in theory, all easily and
automatically convertible) uses of StringBuffer into StringBuilder.
The motivation is to bring some performance benefits where
synchronization is not required. It covers all files in
src/share/classes, but could be extended to cover tools and platform
behaviour as well.

* The refactoring deals with instances of StringBuffers that are
created in a method, and which don't escape out of their scope.
* There are a few instances where fields are converted, but they are
in classes that are not serializable, for example in LogStream.
* A lot of the changes are in the toString() methods, but there are
more frequently used methods in RandomAccessFile (for example,
readLine()) and Double that use StringBuffer.
* We're going to check that there really is no synchronization that is
going on (to the best of our ability)

Quick Q:  Is moving from StringBuffer to StringBuilder generally
desirable? I'm concerned it might be too dangerous given that whether
or not it's being used in a synchronized context may not be easily
detectable.

If it is generally desirable, then any extra advice on what we should
look out for in pre-reviewing this patch is most welcome.

Cheers,
Martijn


More information about the jdk8-dev mailing list