Skip to content

RUST-1062 More efficiently serialize array indexes #384

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

patrickfreed
Copy link
Contributor

RUST-1062

This PR updates the array serialization logic in the raw document serializer to avoid unnecessary heap allocations, resulting in an ~8% improvement in performance in a simple benchmark I'm using.

The solution used here is a bit different than the originally proposed one to just push bytes directly onto the buffer, but it was necessary in order to preserve the expected byte order (pushing them would result in a backwards string). As a result, the performance improvements were not as significant as originally envisioned unfortunately.

@patrickfreed patrickfreed marked this pull request as ready for review November 14, 2022 19:07
@patrickfreed patrickfreed requested review from a team and abr-egn and removed request for a team November 14, 2022 19:07
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!

@@ -225,7 +226,10 @@ impl<'a> serde::Serializer for KeySerializer<'a> {

#[inline]
fn serialize_u64(self, v: u64) -> Result<Self::Ok> {
Err(Self::invalid_key(v))
use std::io::Write;
Copy link

Choose a reason for hiding this comment

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

should we be concerned at all about users accidentally encountering this and being surprised by the behavior, e.g. if they are working with a HashMap<i64, String> and try to serialize that to BSON?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh good point, I hadn't realized this affected that. From looking at past times this was requested, it seems intentional that we don't convert numbers to strings when serializing (#150), so I've updated this to only use this code path when serializing arrays.

@patrickfreed patrickfreed merged commit b202364 into mongodb:main Nov 17, 2022
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.

4 participants