Size the buffer to the input for one-shot Base-N encode#439
Closed
nishantmehta wants to merge 1 commit into
Closed
Size the buffer to the input for one-shot Base-N encode#439nishantmehta wants to merge 1 commit into
nishantmehta wants to merge 1 commit into
Conversation
BaseNCodec.encode(byte[], int, int) (which backs encode(byte[]), encodeToString and the static Base64/Base32 helpers) created a Context whose buffer was lazily allocated by ensureBufferSize at max(size, 8192). For a one-shot encode of a small input this allocated the full 8192-byte default streaming buffer regardless of the actual output size. The exact output size is already computable via getEncodedLength, so pre-size the context buffer to it before encoding. The streaming path (Base64OutputStream etc.) is unchanged and still grows from the default size. When the encoded length does not fit an int the code falls back to the streaming buffer; such an output cannot be returned as a single array anyway. getEncodedLength(byte[]) is refactored to delegate to a private length-based helper so the one-shot encode can size from the requested range length. Measured with a ThreadMXBean allocation driver (200k warmed ops, 25-byte input): Base64.encodeToString 8479 B/op -> 345 B/op (-96%) Base32.encodeToString 8592 B/op -> 458 B/op (-95%) Base64Test, Base32Test, Base16Test, BaseNCodecTest and the Base64 input/output stream tests pass unchanged (360 tests). Signed-off-by: Nishant Mehta <nishantmehta.n@gmail.com>
Member
|
Closing: change for the sake of change and not worth the extra complications and bloat. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BaseNCodec.encode(byte[], int, int)— which backsencode(byte[]),encodeToStringand the staticBase64/Base32helpers — created aContextwhose buffer was lazily allocated byensureBufferSizeatmax(size, 8192). For a one-shot encode of a small input this allocated the full 8192-byte default streaming buffer regardless of the actual output size.The exact output size is already computable via
getEncodedLength, so this pre-sizes the context buffer to it before encoding. The streaming path (Base64OutputStreametc.) is unchanged and still grows from the default size. When the encoded length does not fit anintthe code falls back to the streaming buffer (such an output cannot be returned as a single array anyway).getEncodedLength(byte[])is refactored to delegate to a private length-based helper so the one-shot encode can size from the requested range length.Measurement
ThreadMXBean allocation driver, 200k warmed ops, 25-byte input:
Base64.encodeToStringBase32.encodeToStringTesting
Base64Test,Base32Test,Base16Test,BaseNCodecTestand the Base64 input/output stream tests pass unchanged (360 tests). The streaming buffer growth path is exercised by the existing stream tests.