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

Add move semantics support #438

Closed
mloskot opened this issue Mar 4, 2020 · 11 comments
Closed

Add move semantics support #438

mloskot opened this issue Mar 4, 2020 · 11 comments
Labels
cat/enhancement Improvements, but not fixes addressing identified bugs
Milestone

Comments

@mloskot
Copy link
Member

mloskot commented Mar 4, 2020

Since GIL switched to C++11, it's the right time to consider enabling move semantics for the core types which own resources, especially the image class.

Contributions are welcome. Please, see the CONTRIBUTING.md for guidelines on how to.

@mloskot mloskot added cat/enhancement Improvements, but not fixes addressing identified bugs status/need-help Issues where help and contributions are welcome labels Mar 4, 2020
@mocabe
Copy link

mocabe commented Mar 5, 2020

Speaking of gil::image, move constructor seems relatively straightforward.

Signature of move constructor should be like:

image(image&& img)

Although there're some design spaces here:

  • Should we make move constructor noexcept?
    • This requires copy construction of view_t is nothrow operation.
  • Should we add templated version of this like copy constructor?
    • I'm not really sure why there's templated copy constructor, to be honest. Can someone show me use case of it?
image_t a(...);
image_t b(std::move(a));

here's list of internal states on move construction I can think of:

a before move Move constructed b Moved after state of a
view_t _view a._view view_t()
unsigned char* _memory a._memory nullptr
std::size_t _align_in_bytes a._align_in_bytes 0
allocator_type _alloc std::move(a._alloc) Allocator specific moved-after state
std::size_t _allocated_bytes a._allocated_bytes 0

@sdebionne
Copy link
Contributor

@mocabe Any chance that you are working on this issue? Just to make sure we are not duplicating work...

About the template copy constructor of image, same pixel types but different allocators could be a use case, but not very likely.

@mocabe
Copy link

mocabe commented Mar 19, 2020

@sdebionne I haven't really worked on this issue extensively yet, and it'll take several weeks before I can put more effort into this over other tasks I have right now. So if you can actively work on this right now, go for it!

About the template copy constructor of image, same pixel types but different allocators could be a use case, but not very likely.

Yeah, I though of that but current templated copy constructor is actually copying private allocator member so it's not really usable like that since there's no friend declaration for other image classes...

    template <typename P2, bool IP2, typename Alloc2>
    image(const image<P2,IP2,Alloc2>& img) : _memory(nullptr), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
                                           , _allocated_bytes( img._allocated_bytes ) {
       allocate_and_copy(img.dimensions(),img._view);
    }

@mocabe
Copy link

mocabe commented Mar 19, 2020

On move constructors, I'm still not sure we can just put noexcept on it. When there are some image view implementations which can possible throw on copy construction, they just crashes with noexcept. We can set conditional noexcept with checks like noexcept(std::is_nothrow_copy_constructible<view_t>::value) but now we also need to add noexcept for common image views for optimal performance.

Move assignments are even more tricky when we think of allocators.
Some allocators are not move assignable, which means we need to fallback to copy construction on some specific conditions. As far as I looked though the code, supporting allocators property in gil::image requires some large rewrite including copy constructors, so this can be ignored this time I guess.

@mloskot
Copy link
Member Author

mloskot commented Mar 25, 2020

@sdebionne & @mocabe FYI, I'm not ignoring this discussion and the move semantics is a big deal for GIL, it is just taking time for me to find moment and think about it myself. Thanks for your help!

@sdebionne
Copy link
Contributor

@mloskot Thanks for your support!

@mocabe Shall we just remove the noexcept for now then and leave the optimization for later? I can add // TODO in the code. I'll try to handle the case with allocators that are not move assignable. Shall we add a new "Support allocators properly in gil::image" issue?

@mloskot mloskot removed the status/need-help Issues where help and contributions are welcome label Mar 26, 2020
@mloskot
Copy link
Member Author

mloskot commented Mar 27, 2020

@sdebionne & @mocabe I posted my initial review of #457

Shall we add a new "Support allocators properly in gil::image" issue?

Feel free to open issues/tasks/todos whenever you think it's useful for yourself or others.

Actually, the move assignment operator sample I attached to the review may address some of those issues that you have in mind.

@sdebionne
Copy link
Contributor

@mloskot Do you see any other core types that need move semantic or shall we mark this issue as done?

@mloskot
Copy link
Member Author

mloskot commented May 12, 2022

@sdebionne I haven't checked in details for any other obvious types. The iterators and locators could be. I don't think we have any that manage heavy resources.
If see no obvious types either, then this can be closed as done.

@mloskot mloskot modified the milestones: Boost 1.80, Boost 1.73, Boost 1.74 May 12, 2022
@sdebionne
Copy link
Contributor

AFAIK, all iterators and locators are lightweight (as one would expect such objects to be).

@mloskot
Copy link
Member Author

mloskot commented May 12, 2022

Yes, they are, and all simple values should be moveable, in the standard library they must be. Here is nice summary of the gist https://stackoverflow.com/a/14316175/151641 That's why I mentioned them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cat/enhancement Improvements, but not fixes addressing identified bugs
Projects
None yet
Development

No branches or pull requests

3 participants