RFR: 4926314: Optimize Reader.read(CharBuffer) [v5]

Brian Burkhalter bpb at openjdk.java.net
Mon Feb 8 21:46:56 UTC 2021


On Tue, 26 Jan 2021 18:22:02 GMT, Philippe Marschall <github.com+471021+marschall at openjdk.org> wrote:

>> Implement three optimiztations for Reader.read(CharBuffer)
>> 
>> * Add a code path for heap buffers in Reader#read to use the backing array instead of allocating a new one.
>> * Change the code path for direct buffers in Reader#read to limit the intermediate allocation to `TRANSFER_BUFFER_SIZE`.
>> * Implement `InputStreamReader#read(CharBuffer)` and delegate to `StreamDecoder`.
>> * Implement `StreamDecoder#read(CharBuffer)` and avoid buffer allocation.
>
> Philippe Marschall has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Limit amount read to avoid BufferOverflowException
>   
>   - limit the amount read
>   - add tests

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

> 192:             nread = this.read(cbuf, off, len);
> 193:             if (nread > 0)
> 194:                 target.position(target.position() + nread);

As `target` is mutable, I think you would do better to change lines 189-194 to something like:
            char cbuf[] = target.array();
            int pos = target.position();
            int rem = target.limit() - pos;
            if (rem <= 0)
                return -1;
            int off = target.arrayOffset() + pos;
            nread = this.read(cbuf, off, rem);
            if (nread > 0)
                target.position(pos + nread);

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

PR: https://git.openjdk.java.net/jdk/pull/1915


More information about the core-libs-dev mailing list