-
Notifications
You must be signed in to change notification settings - Fork 148
RUST-2239 Fix performance of bson 3.0 #596
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
---|---|---|
|
@@ -732,6 +732,15 @@ impl Document { | |
let raw = crate::raw::RawDocumentBuf::from_reader(reader)?; | ||
raw.try_into() | ||
} | ||
|
||
pub(crate) fn append_to(&self, buf: &mut Vec<u8>) -> crate::error::Result<()> { | ||
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. With |
||
let mut writer = crate::raw::doc_writer::DocWriter::open(buf); | ||
for (k, v) in self { | ||
writer.append_key(v.element_type(), k.as_str().try_into()?); | ||
v.append_to(writer.buffer())?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// A view into a single entry in a document, which may either be vacant or occupied. | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::{raw::CStr, spec::ElementType}; | ||
|
||
pub(crate) struct DocWriter<'a> { | ||
data: &'a mut Vec<u8>, | ||
start: usize, | ||
} | ||
|
||
impl<'a> DocWriter<'a> { | ||
pub(crate) fn open(data: &'a mut Vec<u8>) -> Self { | ||
let start = data.len(); | ||
data.extend(crate::raw::MIN_BSON_DOCUMENT_SIZE.to_le_bytes()); | ||
Self { data, start } | ||
} | ||
|
||
pub(crate) fn resume(data: &'a mut Vec<u8>, start: usize) -> Self { | ||
Self { data, start } | ||
} | ||
|
||
pub(crate) fn append_key(&mut self, element_type: ElementType, name: &CStr) { | ||
self.data.push(element_type as u8); | ||
name.append_to(self.data); | ||
} | ||
|
||
pub(crate) fn buffer(&mut self) -> &mut Vec<u8> { | ||
self.data | ||
} | ||
} | ||
|
||
impl<'a> Drop for DocWriter<'a> { | ||
fn drop(&mut self) { | ||
self.data.push(0); | ||
let new_len = ((self.data.len() - self.start) as i32).to_le_bytes(); | ||
self.data[self.start..self.start + 4].copy_from_slice(&new_len); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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.
This is the "avoiding copies on individual elements" part; for the most part this can just delegate to
RawBsonRef
. Unfortunately, for the more complex types that aren't just a simple reference conversion there's some duplication of logic that was unavoidable. I reworked theRawWriter
helper intoDocWriter
to boil down what I could.