Encode base64 into exactly-sized char[] instead of StringBuilder - #1674
Open
pjfanning wants to merge 1 commit into
Open
Encode base64 into exactly-sized char[] instead of StringBuilder#1674pjfanning wants to merge 1 commit into
pjfanning wants to merge 1 commit into
Conversation
Base64Variant.encode() appended char-at-a-time to a StringBuilder sized at a 1.375x estimate. Both public overloads now share a private helper that computes the exact output length and encodes straight into a char[] via the existing public char[] chunk/partial helpers. Output is unchanged. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0161vnbY6rGfPFAYBMnbv64c
Contributor
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.
Base64Variant.encode(byte[], boolean)andencode(byte[], boolean, String)each built their result by appending char-at-a-time to aStringBuildersized at an estimate ofinputEnd + (inputEnd >> 2) + (inputEnd >> 3)(1.375x input).Both overloads now delegate to a private
_encodeToString(...)that computes the exact output length up front and encodes straight into achar[], using the already-publicencodeBase64Chunk(int, char[], int)andencodeBase64Partial(int, int, char[], int)helpers.Notes
StringBuilder-takingencodeBase64Chunk(StringBuilder, int)/encodeBase64Partial(StringBuilder, int, int)remain public and unchanged; they are simply no longer used internally.chunksPerLineisMath.max(1, getMaxLineLength() >> 2). The constructor does not validatemaxLineLength, and values below 4 made the old counter emit a linefeed after every chunk; the guard reproduces that exactly and keeps the length calculation from dividing by zero.NegativeArraySizeExceptionfrom the wrapped-negative length), as it did fromnew StringBuilder(...).Correctness
A throwaway differential test ran the verbatim old algorithm against the new one: 28,872 comparisons, all byte-identical — 9 variants (MIME, MIME_NO_LINEFEEDS, PEM, MODIFIED_FOR_URL, plus degenerate line lengths 0/1/4/7/8), input lengths 0-400, quoted and unquoted, and four linefeed strings including
"". Re-run with a hardoutPtr != buffer.lengthassertion in the encoder it was also clean, so the buffer is exactly sized in every case.Three permanent tests added to
Base64CodecTest(short inputs, line-boundary output, sub-chunk line lengths). Fullverifygreen, 1839 tests.Performance
These numbers are provisional. They come from a crude loop benchmark, not JMH, on a machine that was noisy enough that absolute timings drifted 2-4x between batches. @pjfanning will write a JMH benchmark to verify before this is relied on.
Min-of-5 separate JVMs, PEM + quotes, 200k iterations:
The middle column matters for interpreting this: merely fixing the capacity estimate recovers only ~1.2-1.4x, so the win is coming from the bulk array stores rather than from avoiding a resize. (The 1.375x estimate does under-shoot for shorter line lengths — PEM lands exactly on it and quotes push it over — but that turns out to be the minor effect.)
No release-notes entry, as there is no issue number for this yet.
🤖 Generated with Claude Code
https://claude.ai/code/session_0161vnbY6rGfPFAYBMnbv64c