JDK-8352891 Performance improvements to ByteArrayOutputStream

Alan Bateman alan.bateman at oracle.com
Thu Apr 3 08:17:34 UTC 2025


On 02/04/2025 19:04, Engebretson, John wrote:
>
>   Apologies, human error – here’s the message I intended:
>
>   Thank you!  I’ve updated the PR accordingly and summarized the 
> benchmarks in the description.  Here’s the short version:
>
>   * For small payloads, unsynchronized and optimized versions are 2-4x
>     faster than base
>   * For large payloads, optimized version is 3x faster than base or
>     unsynchronized
>
>   I discovered a capacity-related incompatibility between 
> ByteArrayOutputStream and MemoryOutputStream: the size() method 
> returns int, but MemoryOutputStream can exceed that value.  I added 
> range checking to size() and a new sizeAsLong() method… but it really 
> makes me wonder MemoryOutputStream belongs as a subclass of 
> ByteArrayOutputStream.  It now has two significant incompatibilities: 
> ignoring the protected fields, and size restrictions.
>

The protected fields are only accessible to subclasses so it's not an 
issue. BAOS::toByteArray (in addition to size) means the entire content 
must fit into a byte[]. A sink capable of accumulating but bytes that 
this is a different API.

I think we should at least pursue 
ByteArrayOutputStream.unsynchronized(int cap) and work through the 
javadoc changes to allow that. It does not need to use buf, we have 
flexibility on how the bytes are buffered.

-Alan

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250403/683be704/attachment.htm>


More information about the core-libs-dev mailing list