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

Brian Burkhalter bpb at openjdk.org
Fri Dec 1 22:29:31 UTC 2023


On Fri, 1 Dec 2023 13:33:01 GMT, Markus KARG <duke at openjdk.org> wrote:

>> 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 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.

There is a tradeoff here between number of invocations of `arraycopy` and amount of memory allocated for `tmp`. (We have seen this before in #14981 which I have allowed to languish.) The allocation limit is `MAX_TRANSFER_SIZE` which is presently 128 kB, so any transfer of size less than this will invoke `arraycopy` only once already.

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

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


More information about the core-libs-dev mailing list