RFR: 8321053: Use ByteArrayInputStream.buf directly when parameter of transferTo() is trusted [v2]

Markus KARG duke at openjdk.org
Fri Dec 1 13:45:12 UTC 2023


On Thu, 30 Nov 2023 23:30:20 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> Pass `ByteArrayInputStream.buf ` directly to the `OutputStream` parameter of `BAIS.transferTo` only if the target stream is in the `java.io` package.
>
> Brian Burkhalter has updated the pull request incrementally with one additional commit since the last revision:
> 
>   8321053: Trust non-FilterOutputStreams in "java." packages

src/java.base/share/classes/java/io/ByteArrayInputStream.java line 211:

> 209:         if (len > 0) {
> 210:             byte[] tmp;
> 211:             if (out.getClass().getPackageName().startsWith("java.") &&

Has anybody actually estimated or measured if such an exception is actually useful / needed given the fact that System.arraycopy is fast native code and most buffers used by java.io-located streams are just few KB? Just asking as it could be the case that *interpreting* this Java bytecode could be slower than executing some ASM ops to create a few-KB copy, and we *might* do an "premature optimization" here.

src/java.base/share/classes/java/io/ByteArrayInputStream.java line 221:

> 219:                 int nbyte = Integer.min(len - nwritten, MAX_TRANSFER_SIZE);
> 220:                 if (tmp != null) {
> 221:                     System.arraycopy(buf, pos, tmp, 0, nbyte);

I assume the overall performance of transferTo will be faster if we use System.arraycopy *only once* in line 215 to create a safe copy of the *complete* buf instead of calling it multiple times in a loop to create copies *per slice*. In that case we can omit the tmp == null case but simply use tmp = buf, making the code in the loop if-free.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/16893#discussion_r1412118013
PR Review Comment: https://git.openjdk.org/jdk/pull/16893#discussion_r1412107929


More information about the core-libs-dev mailing list