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

Roger Riggs rriggs at openjdk.org
Fri Jun 20 15:54:32 UTC 2025


On Fri, 20 Jun 2025 15:50:53 GMT, Chen Liang <liach at openjdk.org> wrote:

>> As this PR explicitly targets performance and as the aim of this method is to keep **all** content in-memory anyways, I wonder if it would be acceptable and even faster to pre-allocate `new StringBuilder(TRANSFER_BUFFER_SIZE)`? In the end, this allocation is just temporary.
>
> 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.

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

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


More information about the core-libs-dev mailing list