Skip to content

Change A2: Use fixed-length byte array rather than ByteArrayOutputStream #10

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: change-A1_initialise-ByteArrayOutputStream-with-required-capacity
Choose a base branch
from

Conversation

rtyley
Copy link
Owner

@rtyley rtyley commented Sep 2, 2023

Reading out of a ByteArrayOutputStream

There's no properly supported way to get the ultimate cumulative byte array stored in a ByteArrayOutputStream without doing an array copy - that's what ByteArrayOutputStream.toByteArray() does, and that means:

  • While copying, the JVM heap must briefly hold both the old & new byte arrays - roughly speaking, doubling the memory requirements.
  • Copying the bytes from one array to another takes a little bit of CPU time (obviously this varies: System.arraycopy() for 40MB of bytes takes ~2ms on my M1 machine).

Writing into a ByteArrayOutputStream

The new KnownLengthStore implementation does 1 array copy for a write.

The BaosStore implementation:

  • Allocates a new array 'X' with the size of the new incoming data chunk
  • Copies data into that array 'X'
  • Copies array 'X' into the instance's cumulative byte array

Required RAM: 644 MB

Reading out of a `ByteArrayOutputStream`
----------------------------------------

There's no properly supported way to get the ultimate cumulative byte
array stored in a `ByteArrayOutputStream` without doing an array copy -
that's what `ByteArrayOutputStream.toByteArray()` does, and that means:

* While copying, the JVM heap must briefly hold both the old & new byte arrays -
  roughly speaking, doubling the memory requirements.
* Copying the bytes from one array to another takes a little bit of CPU time
  (obviously this varies: `System.arraycopy()` for 40MB of bytes takes ~2ms
  on my M1 machine).

Writing into a `ByteArrayOutputStream`
--------------------------------------

The new `KnownLengthStore` implementation does 1 array copy for a write.

The `BaosStore` implementation:

* Allocates a new array 'X' with the size of the new incoming data chunk
* Copies data into that array 'X'
* Copies array 'X' into the instance's cumulative byte array
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