Optimize output vector adapter write#3569
Merged
nlohmann merged 2 commits intonlohmann:developfrom Jul 8, 2022
Merged
Conversation
…adapter::write_characters This change increases a lot the performance when writing lots of binary data.
gregmarr
approved these changes
Jul 5, 2022
Owner
|
Thanks! |
1r0b1n0
pushed a commit
to ixblue/rosbridge_server_cpp
that referenced
this pull request
Jul 3, 2023
Apply the change made in the PR#3569 of the json lib nlohmann/json#3569
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The first commit of this pull request adds a benchmark for the CBOR serialization in order to compare the change introduced by the second commit of the pull request.
In the
output_vector_adapterclass, replace the usage ofstd::copy+std::back_inserterby the methodstd::vector::insert.The
std::back_insertergenerates a lot of calls tostd::vector::push_backwhich allocate the memory on the fly.For big datasets, usually binary data, the overhead is important.
Resizing the vector in a first step and then copying the data helps a lot.
But from my benchmarks, it appears that using the method
.insert()on a vector is almost as performant and shorter to write.I have added a benchmark similar to the
.dump()for JSON but usingjson::to_cbor()but the performance change is not significant.But because the application in which I have noticed a bottleneck in the use of
std::vector::push_back()was serializaing a lot of binary data to cbor, I have added a benchmark serializing vectors of bytes. For this case, the change proposed is significant.Original:
New version with
std::vector::insert():If you prefer an implementation with
std::vector::resize()+std::memcpy()orstd::copy(), the performance is equivalent.