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

Brian Burkhalter bpb at openjdk.org
Tue Jul 1 23:04:01 UTC 2025


On Tue, 1 Jul 2025 15:10:10 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> 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.
>
>> I think resizing the buffer when the line length exceeds cb.length/2 will be more efficient (cpu wise).
> 
> I concur. I missed that this had been changed in the most recent commit.

So changed in [9eeed76](https://github.com/openjdk/jdk/pull/25863/commits/9eeed767ced6e7b86ac7c2c73fb2d32063d88a7a).

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

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


More information about the core-libs-dev mailing list