Skip to content

Conversation

abr-egn
Copy link
Contributor

@abr-egn abr-egn commented Aug 18, 2025

RUST-2239

This takes 3.0 benchmark performance from a 0-90% regression, depending on the benchmark, to a 0-50% improvement 😁

All of the delta is in converting a Document into bytes; the improvement is from (a) encoding everything into a single running buffer rather than letting subdocuments allocate their own and (b) avoiding copies when encoding individual elements.

Benchmark results from 2.15.x:

TEST: Flat BSON Decoding – Score: 299.481 MB/s, Median Iteration Time: 0.251s
TEST: Flat BSON Encoding – Score: 2204.125 MB/s, Median Iteration Time: 0.034s
TEST: Deep BSON Decoding – Score: 71.221 MB/s, Median Iteration Time: 0.276s
TEST: Deep BSON Encoding – Score: 580.878 MB/s, Median Iteration Time: 0.034s
TEST: Full BSON Decoding – Score: 212.965 MB/s, Median Iteration Time: 0.269s
TEST: Full BSON Encoding – Score: 1189.535 MB/s, Median Iteration Time: 0.048s
BSONBench Score = 759.701 MB/s

Benchmark results from 3.0 before this PR:

TEST: Flat BSON Decoding – Score: 292.714 MB/s, Median Iteration Time: 0.257s
TEST: Flat BSON Encoding – Score: 1435.658 MB/s, Median Iteration Time: 0.052s
TEST: Deep BSON Decoding – Score: 74.274 MB/s, Median Iteration Time: 0.264s
TEST: Deep BSON Encoding – Score: 62.359 MB/s, Median Iteration Time: 0.315s
TEST: Full BSON Decoding – Score: 244.282 MB/s, Median Iteration Time: 0.235s
TEST: Full BSON Encoding – Score: 477.151 MB/s, Median Iteration Time: 0.120s
BSONBench Score = 431.073 MB/s

Benchmark results from 3.0 after this PR:

TEST: Flat BSON Decoding -- Score: 357.983 MB/s, Median Iteration Time: 0.210s
TEST: Flat BSON Encoding -- Score: 2447.073 MB/s, Median Iteration Time: 0.031s
TEST: Deep BSON Decoding -- Score: 73.080 MB/s, Median Iteration Time: 0.269s
TEST: Deep BSON Encoding -- Score: 910.454 MB/s, Median Iteration Time: 0.022s
TEST: Full BSON Decoding -- Score: 278.466 MB/s, Median Iteration Time: 0.206s
TEST: Full BSON Encoding -- Score: 1573.123 MB/s, Median Iteration Time: 0.036s
BSONBench Score = 940.030 MB/s

}
}

pub(crate) fn append_to(&self, buf: &mut Vec<u8>) -> crate::error::Result<()> {
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 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 the RawWriter helper into DocWriter to boil down what I could.

raw.try_into()
}

pub(crate) fn append_to(&self, buf: &mut Vec<u8>) -> crate::error::Result<()> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

With Bson::append_to now present, the "share a buffer while encoding" becomes very straightforward.

@abr-egn abr-egn marked this pull request as ready for review August 18, 2025 20:15
@abr-egn abr-egn requested a review from a team as a code owner August 18, 2025 20:15
@abr-egn abr-egn requested a review from isabelatkinson August 18, 2025 20:15
Copy link
Contributor

@isabelatkinson isabelatkinson left a comment

Choose a reason for hiding this comment

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

nice!

@abr-egn abr-egn merged commit 46bc541 into mongodb:main Aug 20, 2025
12 checks passed
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