Spark: Avoid String and java.util.UUID allocations on UUID read/write paths#16349
Open
wombatu-kun wants to merge 2 commits into
Open
Spark: Avoid String and java.util.UUID allocations on UUID read/write paths#16349wombatu-kun wants to merge 2 commits into
wombatu-kun wants to merge 2 commits into
Conversation
… paths Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Use a local accumulator instead of mutating the `value` parameter so checkstyle's ParameterAssignment rule passes. Behavior is byte-for-byte unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
What & why
This implements the existing
// TODO: direct conversion from string to byte bufferinSparkValueWriters. The Spark data layer converted UUID values through an intermediateStringand ajava.util.UUIDobject on every value, in both directions: write didUUID.fromString(s.toString())then re-serialized the two longs to 16 bytes; read didUUIDUtil.convert(buf).toString()then wrapped that asUTF8String. The UUID arrives as the ASCII bytes of its canonical string and leaves as 16 raw bytes (and vice versa), so theString/UUIDobjects are pure per-row allocation overhead.Changes
UUIDUtil.convertToByteBuffer(byte[] uuidStringBytes, ByteBuffer reuse)— parses the 36 ASCII bytes of a canonical UUID string directly into the 16-byte big-endian form.UUIDUtil.convertToStringBytes(ByteBuffer uuidBytes, byte[] reuse)— renders 16 bytes back to the 36 ASCII bytes of the canonical string.Spark*Readers/Spark*Writersfor Spark 3.4, 3.5, 4.0, 4.1 to use these helpers.TestUUIDUtilcoverage for the new methods.Correctness
Both helpers pivot on the
(mostSigBits, leastSigBits)long pair: the parser reproducesjava.util.UUID.fromString(parse[0,8)→<<16 | [9,13)→<<16 | [14,18)for MSB;[19,23)→<<48 | [24,36)for LSB) and thenputLong(0, msb); putLong(8, lsb)exactly as the previousconvertToByteBuffer(UUID, reuse); the formatter is the inverse and matchesUUID.toString(). The output is therefore byte-for-byte identical to the previous code. The write side keeps the reusable thread-local buffer; the read side must allocate a fresh array becauseUTF8String.fromByteswraps without copying (a reused buffer would alias across rows).