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

Jaikiran Pai jpai at openjdk.org
Wed Apr 5 07:01:17 UTC 2023


On Mon, 27 Mar 2023 16:18:22 GMT, Sergey Tsypanov <stsypanov at openjdk.org> wrote:

>> By default `BufferedInputStream` is constructed with internal buffer with capacity 8192. In some cases this buffer is never used, e.g. when we call `IS.readNBytes()` or `IS.readAllBytes()` (relying on `BIS.read1()`) or when `BufferedInputStream` is cascaded.
>
> Sergey Tsypanov has updated the pull request incrementally with one additional commit since the last revision:
> 
>   8304745: Add comments for benchmark

Marked as reviewed by jpai (Reviewer).

src/java.base/share/classes/java/io/BufferedInputStream.java line 181:

> 179:         byte[] buffer = buf;
> 180:         if (allocateIfEmpty && buffer == EMPTY) {
> 181:             buffer = new byte[initialSize];

It took me a few rounds of reading this code to realize that this array allocation will only be done once even when multiple threads are involved, because when `buffer == EMPTY` (i.e. `BufferedInputStream` is not subclassed), then the call to `getBufIfOpen(...)` always happens after acquiring the `lock`. So, only one thread will ever enter this block. Looks good to me.

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

PR Review: https://git.openjdk.org/jdk/pull/13150#pullrequestreview-1372264626
PR Review Comment: https://git.openjdk.org/jdk/pull/13150#discussion_r1158097110


More information about the core-libs-dev mailing list