8229022: BufferedReader performance can be improved by using StringBuilder

Brian Burkhalter brian.burkhalter at oracle.com
Wed Oct 2 00:13:15 UTC 2019


While the performance improvement that I measured for this proposed change [1] using JMH was only in the 2% to 8% range as opposed to the 13% claimed in the issue description, given the simplicity of the change [2] it is probably worth doing. All use of the StringBuilder is within a synchronized block within a single package-scope method, so there should be no problem with access by multiple threads.

Thanks,

Brian

[1] https://bugs.openjdk.java.net/browse/JDK-8229022
[2] diff

--- a/src/java.base/share/classes/java/io/BufferedReader.java
+++ b/src/java.base/share/classes/java/io/BufferedReader.java
@@ -314,7 +314,7 @@
      * @throws     IOException  If an I/O error occurs
      */
     String readLine(boolean ignoreLF, boolean[] term) throws IOException {
-        StringBuffer s = null;
+        StringBuilder s = null;
         int startChar;
 
         synchronized (lock) {
@@ -372,7 +372,7 @@
                 }
 
                 if (s == null)
-                    s = new StringBuffer(defaultExpectedLineLength);
+                    s = new StringBuilder(defaultExpectedLineLength);
                 s.append(cb, startChar, i - startChar);
             }
         }



More information about the core-libs-dev mailing list