Skip to content

Change A1: Tell ByteArrayOutputStream required capacity #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rtyley
Copy link
Owner

@rtyley rtyley commented Sep 2, 2023

The byte array in ByteArrayOutputStream doubles in size every time capacity is exhausted - for an object just over 1GB in size, the buffer will eventually grow to 2GB in size - unnecessarily large, and involving a lot of array copies (ceil(log_2 (N/32)) copies).

https://github.com/openjdk/jdk/blob/218829e0a2a3ae5599b81733df53557966392033/src/java.base/share/classes/java/io/ByteArrayOutputStream.java#L100-L101

If we just tell the Content Length to initialise the ByteArrayOutputStream with a byte array of the right size, the array will never get bigger than it needs to be - and it won't have do array-resizing either.

Required RAM: 934 MB

The byte array in `ByteArrayOutputStream` doubles in size every time
capacity is exhausted - for an object just over 1GB in size, the buffer
will eventually grow to 2GB in size - unnecessarily large, and involving
a lot of array copies (`ceil(log_2 (N/32))` copies).

https://github.com/openjdk/jdk/blob/218829e0a2a3ae5599b81733df53557966392033/src/java.base/share/classes/java/io/ByteArrayOutputStream.java#L100-L101

If we just tell the Content Length to initialise the `ByteArrayOutputStream`
with a byte array of the right size, the array will never get bigger than it
needs to be - and it won't have do array-resizing either.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant