Skip to content

RUST-877 Delay replacement document serialization until Operation::build #942

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

Conversation

isabelatkinson
Copy link
Contributor

No description provided.

@isabelatkinson isabelatkinson changed the title Delay replacement document serialization until Operation::build RUST-877 Delay replacement document serialization until Operation::build Aug 28, 2023
@isabelatkinson isabelatkinson force-pushed the delay-replace-serialization branch from a7e4c93 to 8fe50c3 Compare August 30, 2023 16:21
@isabelatkinson isabelatkinson marked this pull request as ready for review August 30, 2023 16:24
@@ -119,6 +125,17 @@ pub(crate) fn read_document_bytes<R: Read>(mut reader: R) -> Result<Vec<u8>> {
Ok(bytes)
}

pub(crate) fn extend_raw_document_buf(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A slightly optimized version of this could just append all of the non-size bytes from other to this and then re-calculate the size, might be something worth adding to the bson API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, filed RUST-1754 for this.

@@ -177,6 +178,19 @@ impl<T> Command<T> {
}
}

impl Command<RawDocumentBuf> {
pub(crate) fn into_bson_bytes(mut self) -> Result<Vec<u8>> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method (when it's called in Operation::serialize_command) bypasses running the body of the command back through the serializer (i.e. the default implementation for serialize_command) and instead appends the rest of the command to the body. The tradeoff here is that the raw BSON created on 187 is immediately copied on line 189; we could manually serialize and append each field directly to command instead, but I think the size of rest_of_command is small enough that it shouldn't make much of a difference in the vast majority of cases.

Most Commands for other operations currently use the default Document type for T, but in the future (maybe in tandem with RUST-1747) I think we should change them to use RawDocumentBuf and then standardize this approach for serializing commands.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on all points.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've talked in the past about deleting these low-value operation tests as they become outdated. These changes broke most of them so I just removed the whole file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto above

@isabelatkinson isabelatkinson requested a review from abr-egn August 30, 2023 17:52
@@ -177,6 +178,19 @@ impl<T> Command<T> {
}
}

impl Command<RawDocumentBuf> {
pub(crate) fn into_bson_bytes(mut self) -> Result<Vec<u8>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on all points.

@@ -119,6 +125,17 @@ pub(crate) fn read_document_bytes<R: Read>(mut reader: R) -> Result<Vec<u8>> {
Ok(bytes)
}

pub(crate) fn extend_raw_document_buf(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, filed RUST-1754 for this.

pub(crate) fn to_bson(&self) -> Bson {
match self {
Hint::Keys(ref d) => Bson::Document(d.clone()),
Hint::Name(ref s) => Bson::String(s.clone()),
}
}

pub(crate) fn to_raw_bson(&self) -> Result<RawBson> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this could just be to_bson().try_into()? ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this as a minor optimization to avoid an intermediary allocation of a regular Bson via to_bson. It did seem unnecessary to have both to_bson and to_raw_bson though so I just refactored the few uses of to_bson and removed it.

Ideally we'd have a to_raw_bson(value: impl Serialize) method in bson, but I'm not sure how that would actually work; the raw serializer spits out raw document bytes as opposed to the non-raw serializer, which generates BSON values.

ns: Namespace,
query: Document,
modification: Modification<'a, R>,
human_readable_serialization: Option<bool>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be an un-serialized field in FindAndModifyOptions rather than adding it as an explicit parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was slightly more ergonomic because it allows implementing From for the various options rather than writing custom methods. this is maybe just personal preference too but it made sense to me to have only fields that will be serialized and sent to the server within the options. this did make me realize that there's no need to unwrap_or_default the options anymore so I removed that logic

Copy link
Contributor

@abr-egn abr-egn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@isabelatkinson isabelatkinson merged commit 5b54bb6 into mongodb:main Aug 31, 2023
@isabelatkinson isabelatkinson deleted the delay-replace-serialization branch April 3, 2024 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants