docs: ByteBuffer interop + pledged-size zero-copy decode#6
Merged
Conversation
Document the ByteBuffer gap in the zero-copy design: the segment API already bridges NIO both ways at zero copy (MemorySegment.ofBuffer in, asByteBuffer out), so no parallel ByteBuffer overload set is needed. Note the rough edges that a proposed toByteBuffer() sugar would smooth (byte order, borrowed arena lifetime). Explain why pledging the source size is a correctness gate for zero-copy decode, not a micro-optimization: a streamed frame omits the content size, so decompress(arena, frame) cannot size the output arena and throws; withPledgedSize stamps the header and unlocks one-shot zero-copy decode. Add two unit tests covering the contrast: - a plain stream frame cannot be sized for zero-copy decode - a pledged frame decodes zero-copy into an arena in one shot Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
asByteBuffer() on a native segment already returns a direct buffer aliasing the off-heap bytes; the only wart is byte order. Replace the nonexistent toByteBuffer() headline with the working one-liner (asByteBuffer().order(nativeOrder())) and demote the sugar to an optional wrapper. Align the pledged-size example to level 6. Rework pledgedFrameDecodesZeroCopyIntoArenaInOneShot to exercise both zero-copy directions: direct ByteBuffer -> MemorySegment.ofBuffer in, out.asByteBuffer() hand-off out, instead of the copyIn heap path. Co-Authored-By: Claude Opus 4.8 <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
Two zero-copy design clarifications, plus tests for the pledged-size one.
ByteBuffer gap
docs/zero-copy.mdnow has a ByteBuffer interop section. The segment API already bridges the FFM↔NIO boundary both ways at zero copy —MemorySegment.ofBuffer(buf)in,segment.asByteBuffer()out — so no parallelByteBufferoverload set is warranted. Documents the two rough edges a proposedtoByteBuffer()convenience would smooth:asByteBuffer()returns aBIG_ENDIANbuffer regardless of platform, and the buffer borrows the arena's lifetime.Pledged size unlocks zero-copy decode
A streamed frame does not record its decompressed size, so
decompress(arena, frame)cannot size the output arena and throws — the consumer is forced back onto bounded streaming or a guessedmaxSize.ZstdOutputStream.withPledgedSize(...)stamps the content size into the header and unlocks one-shot zero-copy decode. Documented as a correctness gate, not a micro-optimization.Two new unit tests in
ZstdStreamTest$PledgedSize:plainStreamFrameCannotBeSizedForZeroCopyDecode—Zstd.decompressedSizethrows "not stored"pledgedFrameDecodesZeroCopyIntoArenaInOneShot— arena sized exactly from the header, round-tripsNo production code change —
withPledgedSizealready exists; this documents and tests its importance.Test
./mvnw -pl zstd -am test -Dtest='ZstdStreamTest$PledgedSize'— 3 green, checkstyle clean.🤖 Generated with Claude Code