RFR: 8265891: (ch) InputStream returned by Channels.newInputStream should override transferTo

Markus KARG github.com+1701815+mkarg at openjdk.java.net
Thu Jun 17 17:03:21 UTC 2021


On Tue, 8 Jun 2021 19:26:14 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> This PR-*draft* is **work in progress** and an invitation to discuss a possible solution for issue [JDK-8265891](https://bugs.openjdk.java.net/browse/JDK-8265891). It is *not yet* intended for a final review.
>> 
>> As proposed in JDK-8265891, this PR provides an implementation for `Channels.newInputStream().transferTo()` which provide superior performance compared to the current implementation. The changes are:
>> * Prevents transfers through the JVM heap as much as possibly by offloading to deeper levels via NIO, hence allowing the operating system to optimize the transfer.
>> * Using more JRE heap in the fallback case when no NIO is possible (still only KiBs, hence mostl ynegligible even on SBCs) to better perform on modern hardware / fast I/O devides.
>> 
>> Using JMH I have benchmarked both, the original implementation and this implementation, and (depending on the used hardware and use case) performance change was approx. doubled performance. So this PoC proofs that it makes sense to finalize this work and turn it into an actual OpenJDK contribution. 
>> 
>> I encourage everybody to discuss this draft:
>> * Are there valid arguments for *not* doing this change?
>> * Is there a *better* way to improve performance of `Channels.newInputStream().transferTo()`?
>> * How to go on from here: What is missing to get this ready for an actual review?
>
> src/java.base/share/classes/sun/nio/ch/ChannelOutputStream.java line 113:
> 
>> 111:         if ((off < 0) || (off > bs.length) || (len < 0) ||
>> 112:             ((off + len) > bs.length) || ((off + len) < 0)) {
>> 113:             throw new IndexOutOfBoundsException();
> 
> Could the bounds checking be done as follows?
> 
> `Objects.checkFromIndexSize(off, len, bs.length);`
> 
> Same comment applies to lines 146 and 219 in `java.nio.channels.Channels`.

I'd like to abstain from changes in ChannelOutputStream, as I did not write that code at all. It is simply moved from being an inner class. Please let's concentrate on the code I actually wrote in this PR. Thanks.

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

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


More information about the nio-dev mailing list