Optimize output vector adapter write #3569
Merged
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_adapter
class, replace the usage ofstd::copy
+std::back_inserter
by the methodstd::vector::insert
.The
std::back_inserter
generates a lot of calls tostd::vector::push_back
which 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.