RFR: 8304745: Lazily initialize byte[] in java.io.BufferedInputStream [v2]

Sergey Tsypanov stsypanov at openjdk.org
Thu Mar 23 20:36:47 UTC 2023


On Thu, 23 Mar 2023 19:55:04 GMT, Alan Bateman <alanb at openjdk.org> wrote:

>> src/java.base/share/classes/java/io/BufferedInputStream.java line 183:
>> 
>>> 181:         if (buffer == EMPTY) {
>>> 182:             buf = buffer = new byte[size];
>>> 183:         }
>> 
>> You should probably use compareAndSet here too like in fill() - to handle the case of asynchoronous close.
>> This patch makes me a bit nervous. I would advise to get feedback from @bplb and @AlanBateman .
>
> Yes, I'm nervous about changing BIS too. Most of the initial revisions of the patch introduced compatibility issues so it had to be restricted to the no sub-class case. Here, it needs to CAS buf as otherwise it will break async close. 
> 
> I think the starting point for this issue is to show examples where it avoids needing to create the byte[] on first usage. The first use of read, readAllBytes, readNBytes, .. will create the byte[] but Sergey may have spotted some usages where it does help.

Some implementations of `getInputStream()` return `BufferedInputStream` which is later wrapped into `BufferedInputStream` on call site, e.g. `FileURLConnection`. With this change we can avoid allocation of byte[] in internal BIS.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/13150#discussion_r1146801965


More information about the core-libs-dev mailing list