[SPARK-55683][SQL][FOLLOWUP] Optimize VectorizedPlainValuesReader.readUnsignedLongs to reuse scratch buffer and avoid per-element allocations#54510
Open
LuciferYang wants to merge 3 commits intoapache:masterfrom
Conversation
pan3793
reviewed
Feb 26, 2026
Comment on lines
+282
to
+283
| // putByteArray copies the bytes into arrayData(), so scratch can be safely reused | ||
| c.putByteArray(rowId, scratch, 0, totalLen); |
Member
There was a problem hiding this comment.
could you leave the same comments at line 262?
pan3793
approved these changes
Feb 26, 2026
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 changes were proposed in this pull request?
This pr refer to the suggestion from Copilot: #54479 (review), further optimizes
VectorizedPlainValuesReader.readUnsignedLongsby introducing a reusable scratch buffer to eliminate per-elementbyte[]allocations introduced in the previous refactoring.The previous implementation allocates a new
byte[]per element for the encoded output:The new implementation allocates a single
byte[9]scratch buffer once per batch and reuses it across all elements. SinceWritableColumnVector.putByteArraycopies the bytes into its internal storage immediately, the scratch buffer can be safely overwritten on the next iteration:The scratch buffer is sized at 9 bytes to accommodate the worst case: 1
0x00sign byte + 8 value bytes. The zero value special case is also handled via scratch, avoiding the previousnew byte[]{0}allocation.Why are the changes needed?
The previous implementation still allocates one
byte[]per element for the encoded output. For a typical batch of 4096 values this means 4096 heap allocations perreadUnsignedLongscall, creating GC pressure in workloads that read largeUINT_64columns. With the scratch buffer approach, the entire batch produces only 2 allocations:byte[9](scratch) andbyte[8](direct buffer fallback read buffer), regardless of batch size.Does this PR introduce any user-facing change?
No
How was this patch tested?
VectorizedPlainValuesReader.readUnsignedLongs#54479, and the test results are as follows:Java 17
Java 21
Judging from the test results, the onHeap path demonstrates approximately a 17-22% improvement, while the offHeap path shows roughly a 3-9% improvement across Java 17 and Java 21.
Was this patch authored or co-authored using generative AI tooling?
Yes, Claude Sonnet 4.6 was used to assist in completing the code writing.