RFR: 8358533: Improve performance of java.io.Reader.readAllLines [v9]

Roger Riggs rriggs at openjdk.org
Tue Jul 1 01:39:44 UTC 2025


On Mon, 30 Jun 2025 19:38:59 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> Replaces the implementation `readAllCharsAsString().lines().toList()` with reading into a temporary `char` array which is then processed to detect line terminators and copy non-terminating characters into strings which are added to the list.
>
> Brian Burkhalter has updated the pull request incrementally with one additional commit since the last revision:
> 
>   8358533: Sherman's version + decrease initial buffer size

src/java.base/share/classes/java/io/Reader.java line 482:

> 480:                 if (pos == limit) {
> 481:                     int len = limit - start;
> 482:                     if (len >= cb.length) {

Observation: this algorithm will get less efficient as the line length approaches cb.length.
Starting at sb.length/2 It will take 2 reads for every line and will not resize the buffer.
I think resizing the buffer when the line length exceeds cb.length/2 will be more efficient (cpu wise).
The decoding of the bytes to chars and the setup of the decoder is the expensive part.
For most typical line length distributions it would not make a difference but would improve the performance of the outliners.

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

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


More information about the core-libs-dev mailing list