Optimize MemorySegment.copy calls by avoiding wrapper allocation#2027
Merged
Conversation
…nsafeBufferEx.getBytes() MemorySegment.copy(segment, layout, srcOffset, dst, offset, length) copies directly into a plain byte[] array, so per-call construction of a fresh MemorySegment.ofArray(dst) wrapper is unnecessary. Applied to getBytes(int, byte[], int, int), the dstArray branch of getBytes(int, MutableDirectBuffer, int, int), and the equivalent methods on the nested Native variant. Profiling a downstream JSON tokenizer (zilla-plus) found this per-call allocation dominant for small documents with many verbatim key/value deliveries through WindowBuffer.getBytes().
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.
Description
Optimize
getBytes()methods inUnsafeBufferExby using the direct array overload ofMemorySegment.copy()instead of wrapping the destination array in aMemorySegment.ofArray()wrapper.Changes
getBytes()call sites (2 in the outer class, 2 in an inner class) to pass the byte array directly toMemorySegment.copy()instead of wrapping it firstMemorySegment.ofArray(dst)wrapper construction and correspondingBYTE_LAYOUTparameterMotivation
Profiling identified that per-call
MemorySegment.ofArray()wrapper construction was a dominant cost for this method when processing small documents with many verbatim key/value deliveries. The wrapper creates a fresh heap-session-scopedMemorySegmentwith no cacheable form for arbitrary caller-supplied arrays, making it wasteful for frequent small operations.The direct array overload of
MemorySegment.copy()copies directly into the destination array without constructing an intermediate wrapper, eliminating this allocation overhead.Test Plan
Existing tests cover these code paths. No new tests required for this optimization.
https://claude.ai/code/session_01FdmT7MqTozanMnH2rLiE8F