-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Optimize RawBsonDocument encode and decode #1913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vbabanin
wants to merge
8
commits into
mongodb:main
Choose a base branch
from
vbabanin:JAVA-6133
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cd52b6b
Optimize RawBsonDocument encode and decode by eliminating intermediat…
vbabanin 03978bf
Merge branch 'main' into JAVA-6133
vbabanin fc3726d
Refines RawBsonDocument encode optimization
vbabanin a543455
Merge branch 'main' into JAVA-6133
vbabanin 81ec442
Make pipe default method.
vbabanin a5790af
Add tests.
vbabanin 103f461
Merge branch 'main' into JAVA-6133
vbabanin 9f49d2d
Remove unused imports.
vbabanin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -334,6 +334,17 @@ public void pipe(final BsonReader reader) { | |
| pipeDocument(reader, null); | ||
| } | ||
|
|
||
| @Override | ||
| public void pipe(final byte[] bytes, final int offset, final int length) { | ||
| if (getState() == State.VALUE) { | ||
| bsonOutput.writeByte(BsonType.DOCUMENT.getValue()); | ||
| writeCurrentName(); | ||
| } | ||
| int pipedDocumentStartPosition = bsonOutput.getPosition(); | ||
| bsonOutput.writeBytes(bytes, offset, length); | ||
| completePipeDocument(pipedDocumentStartPosition); | ||
|
vbabanin marked this conversation as resolved.
vbabanin marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Override | ||
| public void pipe(final BsonReader reader, final List<BsonElement> extraElements) { | ||
| notNull("reader", reader); | ||
|
|
@@ -355,9 +366,7 @@ private void pipeDocument(final BsonReader reader, final List<BsonElement> extra | |
| } | ||
| int pipedDocumentStartPosition = bsonOutput.getPosition(); | ||
| bsonOutput.writeInt32(size); | ||
| byte[] bytes = new byte[size - 4]; | ||
| bsonInput.readBytes(bytes); | ||
| bsonOutput.writeBytes(bytes); | ||
|
Comment on lines
-358
to
-360
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This avoids an extra temporary byte[] allocation by reading directly into the target buffer, reducing both allocation pressure and byte-copy/processing overhead. |
||
| bsonInput.pipe(bsonOutput, size - 4); | ||
|
|
||
| binaryReader.setState(AbstractBsonReader.State.TYPE); | ||
|
|
||
|
|
@@ -371,24 +380,27 @@ private void pipeDocument(final BsonReader reader, final List<BsonElement> extra | |
| setContext(getContext().getParentContext()); | ||
| } | ||
|
|
||
| if (getContext() == null) { | ||
| setState(State.DONE); | ||
| } else { | ||
| if (getContext().getContextType() == BsonContextType.JAVASCRIPT_WITH_SCOPE) { | ||
| backpatchSize(); // size of the JavaScript with scope value | ||
| setContext(getContext().getParentContext()); | ||
| } | ||
| setState(getNextState()); | ||
| } | ||
|
|
||
| validateSize(bsonOutput.getPosition() - pipedDocumentStartPosition); | ||
| completePipeDocument(pipedDocumentStartPosition); | ||
| } else if (extraElements != null) { | ||
| super.pipe(reader, extraElements); | ||
| } else { | ||
| super.pipe(reader); | ||
| } | ||
| } | ||
|
|
||
| private void completePipeDocument(final int pipedDocumentStartPosition) { | ||
| if (getContext() == null) { | ||
| setState(State.DONE); | ||
| } else { | ||
| if (getContext().getContextType() == BsonContextType.JAVASCRIPT_WITH_SCOPE) { | ||
| backpatchSize(); // size of the JavaScript with scope value | ||
| setContext(getContext().getParentContext()); | ||
| } | ||
| setState(getNextState()); | ||
| } | ||
| validateSize(bsonOutput.getPosition() - pipedDocumentStartPosition); | ||
| } | ||
|
|
||
| /** | ||
| * Sets a maximum size for documents from this point. | ||
| * | ||
|
|
||
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
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of routing through a
BsonReader(which wraps a BsonInput but doesn’t expose the underlying array unless copied), we write the bytes directly to the output.