RFR: 8358533: Improve performance of java.io.Reader.readAllLines

Xueming Shen sherman at openjdk.org
Fri Jun 20 17:19:37 UTC 2025


On Fri, 20 Jun 2025 15:52:08 GMT, Roger Riggs <rriggs at openjdk.org> wrote:

>> My suggestion is to call `new StringBuilder(0)` as it is possible this is completely unused because we always hit the `eol && sb.length() == 0` path below.
>
> The change is motivated by performance, but there will be many inputs that are less than the transfer buffer size and those will not use the StringBuilder, so creating it before it is needed could be avoided.
> When a partial line is left in the transfer buffer, copy it to the beginning of the buffer and read more characters for the remaining size of the buffer. It would save some copying into and out of the SB. 
> You might still need a fallback for really long lines (> transfer buffer size), but that might be more easily handled by reallocating the transfer buffer to make it larger.

resizing/newCapacity is always expensive and tricky, string builder included. so maybe we should decide if 'long lines' (> transfer buffer size) is the goal of this pr.  if not, it might be reasonable/make sense (???) to simply go with "string" +its the built-in string concatenation. i do agree we probably want to avoid paying the cost of copying in & out of the sb, but tweaking the transfer buffer resizing might also be tricky and potentially out of the scope as well. yes, it's always a trade off.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/25863#discussion_r2159408170


More information about the core-libs-dev mailing list