Optimize RawBsonDocument encode and decode #1913
Closed
vbabanin wants to merge 12 commits into
Closed
Conversation
…e allocations - Add BsonWriter.pipe(byte[], int, int) with BsonBinaryWriter override to write raw BSON bytes directly to the output, avoiding intermediate object allocation on the encode path - Add BsonInput.pipe(BsonOutput, int) to remove the temporary byte[] copy in BsonBinaryWriter.pipeDocument() on both encode and decode paths - Add public getByteBacking(), getByteOffset(), getByteLength() on RawBsonDocument to expose the backing byte array JAVA-6133
- Removes pipe(byte[], int, int) from BsonWriter interface to avoid coupling it to concrete IO classes; dispatches via instanceof BsonBinaryWriter in the codec instead - Renames getByteBacking() to getBackingArray() for clarity - Validates minimum BSON document size before writing raw bytes, consistent with the reader-based pipe path - Adds tests for the raw-byte pipe happy path and invalid-size rejection JAVA-6133
vbabanin
commented
May 21, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes RawBsonDocument encode/decode paths by enabling direct piping of raw BSON bytes between inputs/writers/outputs, reducing intermediate allocations and copies.
Changes:
- Added
BsonBinaryWriter.pipe(byte[], int, int)to write raw BSON document bytes directly to the output. - Added
BsonInput.pipe(BsonOutput, int)and implemented it inByteBufferBsonInputto avoid temporary byte array copies during piping. - Exposed
RawBsonDocumentbacking byte array, offset, and length via new public accessors, with accompanying tests.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| bson/src/test/unit/org/bson/RawBsonDocumentSpecification.groovy | Adds coverage for new RawBsonDocument backing-array/offset/length accessors. |
| bson/src/test/unit/org/bson/BsonBinaryWriterTest.java | Adds tests for piping raw BSON bytes and invalid-size handling. |
| bson/src/main/org/bson/RawBsonDocument.java | Adds public accessors to expose the backing byte array, offset, and length. |
| bson/src/main/org/bson/io/ByteBufferBsonInput.java | Implements BsonInput.pipe with an array-backed fast path. |
| bson/src/main/org/bson/io/BsonInput.java | Introduces the new pipe(BsonOutput, int) API on BsonInput. |
| bson/src/main/org/bson/codecs/RawBsonDocumentCodec.java | Uses a fast-path to pipe raw bytes when the writer is a BsonBinaryWriter. |
| bson/src/main/org/bson/BsonWriter.java | Minor formatting cleanup. |
| bson/src/main/org/bson/BsonBinaryWriter.java | Implements raw-byte piping and refactors pipe-document completion logic. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
rozza
reviewed
May 21, 2026
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
strogiyotec
requested changes
May 22, 2026
- Use String.getBytes(UTF_8) instead of raw hex bytes in pipe tests for readability - Add RawBsonDocumentTest with Named parametrized padding permutations (before, after, both) - Move backing array accessor test from Groovy spec to Java with broader coverage - Fix checkstyle violations in ForwardingBsonInput delegate methods JAVA-6133
rozza
previously approved these changes
May 27, 2026
Member
rozza
left a comment
There was a problem hiding this comment.
Looking good - only one suggestion
Member
Member
|
Closed and merged via #1988 |
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.
Performance analyzer: link
JAVA-6133