JDK-8352891 Performance improvements to ByteArrayOutputStream

Archie Cobbs archie.cobbs at gmail.com
Thu May 15 14:04:22 UTC 2025


On Thu, May 15, 2025 at 2:02 AM Peter Levart <peter.levart at gmail.com> wrote:

> On 4/11/25 6:19 PM, Archie Cobbs wrote:
> > Sorry if that wasn't clear - I didn't mean power-of-two exponential
> > growth, I meant all segments were literally fixed size, like 256 or
> > something (or maybe this value would be provided to the constructor).
> > This keeps the access to individual elements constant time.
>
> You can have power-of-two exponentially growing segments and still
> access individual elements by index in constant time...


Good point! There are more clever ways to size the chunks that still retain
constant time access.

What I would love to see someday is all of this kind of cleverness baked
into a standard set of "ArrayBuilder" classes, so we can then declare
victory for cleverness and never have to worry about the fastest way to
buffer data into memory again. Then we could focus our optimization efforts
just on those classes, and the users of those classes all over the JDK
(e.g., ArrayList, StringBuilder, whatever) would automatically benefit.

This might be a pipe dream. At least, it brings up an interesting question
re: how you would design such a thing... e.g., how would you parameterize
it? E.g., Initial chunk size, maximum chunk size, growth rate exponent,
etc. Or could it be truly adaptive (one (initial) size fits all)? Etc.

-Archie

-- 
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250515/fbf879f6/attachment.htm>


More information about the core-libs-dev mailing list