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

Unnecessary copies #75

Closed
sean-parent opened this issue Jul 24, 2017 · 3 comments · Fixed by #101
Closed

Unnecessary copies #75

sean-parent opened this issue Jul 24, 2017 · 3 comments · Fixed by #101
Assignees

Comments

@sean-parent
Copy link
Member

The following code creates an unnecessary copy (internally it is because get_try() is returning an optional instead of an optional<const T&>). It also does 4 moves. Can that be reduced?

#include <iostream>

#include <stlab/concurrency/future.hpp>
#include <stlab/concurrency/immediate_executor.hpp>

using namespace stlab;
using namespace std;

struct annotate {
    annotate() { cout << "annotate ctor" << endl; }
    annotate(const annotate&) { cout << "annotate copy-ctor" << endl; }
    annotate(annotate&&) noexcept { cout << "annotate move-ctor" << endl; }
    annotate& operator=(const annotate&) {
        cout << "annotate assign" << endl;
        return *this;
    }
    annotate& operator=(annotate&&) noexcept {
        cout << "annotate move-assign" << endl;
        return *this;
    }
    ~annotate() { cout << "annotate dtor" << endl; }
    friend inline void swap(annotate&, annotate&) { cout << "annotate swap" << endl; }
    friend inline bool operator==(const annotate&, const annotate&) { return true; }
    friend inline bool operator!=(const annotate&, const annotate&) { return false; }
};

template <class T>
inline auto promise_future() {
    return package<T(T)>(immediate_executor, [](auto&& x) -> decltype(x) { return std::forward<decltype(x)>(x); });
}

int main() {
    auto [promise, future] = promise_future<annotate>();
    promise(annotate());
    future.then([](const annotate&){ }); // copy happens here!
}
@sean-parent
Copy link
Member Author

I just started the branch copy-less to resolve this issue. It addresses this particular copy but still there are a fairly large number of other cases to handle. I published the branch for reference and will try and make time to complete it.

@FelixPetriconi
Copy link
Member

Use copy_less branch, but leave the issue open

@fosterbrereton
Copy link
Member

This issue has been fixed in master as of the 1.1 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants