Skip to content
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

#161 use reconstruction logic for std::vector #214

Merged
merged 10 commits into from
Oct 15, 2021

Conversation

jstrzebonski
Copy link
Contributor

Fixes #161

@jstrzebonski jstrzebonski self-assigned this Jun 18, 2021
@jstrzebonski jstrzebonski force-pushed the 161-use-Reconstructor-for-containers-elements branch 3 times, most recently from 326eac6 to 2a37326 Compare June 18, 2021 17:08
@jstrzebonski
Copy link
Contributor Author

jstrzebonski commented Jun 18, 2021

In general, I took reconstruct code almost 1:1 to be able to pass reconstruct tag directly to emplace_back(). Without that I would have to do emplace_back(std::move(reconstructed_obj)) for all reconstruct cases, including tagged ctor, but if that's not a problem I think I could call dispatch::Standard::construct() directly, something like this:

template <typename T, typename VectorAllocator>
void reconstructVectorData(
  SerialSizeType const vec_size, std::vector<T, VectorAllocator>& vec) {
  auto* buf = dispatch::Standard::allocate<T>();
  for (SerialSizeType i = 0; i < vec_size; ++i) {
    auto* t = dispatch::Standard::construct<T>(buf);
    vec.emplace_back(std::move(*t));
  }
}

@jstrzebonski
Copy link
Contributor Author

jstrzebonski commented Jun 18, 2021

@lifflander Do you think that's (more or less) the direction we wanted to head?

@jstrzebonski jstrzebonski force-pushed the 161-use-Reconstructor-for-containers-elements branch from c10c363 to 1ea3f1f Compare June 18, 2021 19:18
@PhilMiller
Copy link
Member

I think vec.resize(vec_size, SERIALIZE_RECONSTRUCT_TAG) is a feasible and preferable form for the case where we have the tagged reconstruction, since it avoids the intermediate allocation and move, and potentially even allows complete elision of all moves/copies if it gets inlined.

@codecov
Copy link

codecov bot commented Jun 23, 2021

Codecov Report

Merging #214 (c7d41a0) into develop (50889a5) will increase coverage by 0.17%.
The diff coverage is 99.14%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #214      +/-   ##
===========================================
+ Coverage    94.14%   94.32%   +0.17%     
===========================================
  Files          100      102       +2     
  Lines         3093     3187      +94     
===========================================
+ Hits          2912     3006      +94     
  Misses         181      181              
Impacted Files Coverage Δ
src/checkpoint/container/list_serialize.h 88.23% <85.71%> (+0.73%) ⬆️
src/checkpoint/container/map_serialize.h 93.10% <100.00%> (+0.24%) ⬆️
src/checkpoint/container/vector_serialize.h 100.00% <100.00%> (ø)
src/checkpoint/dispatch/allocator.h 100.00% <100.00%> (ø)
src/checkpoint/dispatch/reconstructor.h 79.31% <100.00%> (ø)
tests/unit/test_vector_serializer.cc 100.00% <100.00%> (ø)
src/checkpoint/serializers/base_serializer.h 100.00% <0.00%> (ø)

@lifflander
Copy link
Contributor

@lifflander Do you think that's (more or less) the direction we wanted to head?

Sorry I was slow to reply. I agree with @PhilMiller that doing the resize is probably preferable for optimization.

@jstrzebonski jstrzebonski force-pushed the 161-use-Reconstructor-for-containers-elements branch 2 times, most recently from 984b3cd to 51efb95 Compare July 3, 2021 22:23
@jstrzebonski jstrzebonski force-pushed the 161-use-Reconstructor-for-containers-elements branch from 51efb95 to da6c6d7 Compare July 16, 2021 19:22
@jstrzebonski jstrzebonski force-pushed the 161-use-Reconstructor-for-containers-elements branch from 5298aca to 7184e53 Compare October 8, 2021 16:50
@jstrzebonski jstrzebonski force-pushed the 161-use-Reconstructor-for-containers-elements branch from 7184e53 to 3c7e20f Compare October 8, 2021 17:19
@jstrzebonski jstrzebonski marked this pull request as ready for review October 8, 2021 17:19
Copy link
Contributor

@lifflander lifflander left a comment

Choose a reason for hiding this comment

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

I think the approach makes sense. The only overload that might be improved by another issue is the vector case that seems very un-optimized.

src/checkpoint/container/vector_serialize.h Show resolved Hide resolved
@jstrzebonski jstrzebonski merged commit dc92754 into develop Oct 15, 2021
> deserializeOrderedElems(
std::is_same<Serializer, checkpoint::Footprinter>::value, void
>
deserializeOrderedElems(
Serializer& s, ContainerT& cont, typename ContainerT::size_type size
Copy link
Member

Choose a reason for hiding this comment

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

A potential refactoring, based on comments I had pointed out to me elsewhere: this case can avoid using enable_if, and just be an overload taking checkpoint::Footprinter rather than templating over Serializer at all.

Copy link
Member

Choose a reason for hiding this comment

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

The same applies for related cases in the other files, too.

std::is_same<SerializerT, checkpoint::Footprinter>::value, void
>
serialize(SerializerT& s, std::vector<bool, VectorAllocator>& vec) {
s.countBytes(vec);
Copy link
Member

Choose a reason for hiding this comment

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

This could/should also s.addBytes(vec.capacity()*sizeof(bool)) or s.addBytes(vec.capacity()*sizeof(bool)/CHAR_BIT).

I'm unconcerned which exactly, but I'd like something whose resulting value is O(N) so that if we or an application have code with an overgrown vector<bool>, it shows up.

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.

Unpacking of containers default-constructs elements rather than using reconstruction logic
3 participants