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

Consider making containers trivially copyable when possible #173

Open
alfC opened this issue Mar 13, 2018 · 12 comments
Open

Consider making containers trivially copyable when possible #173

alfC opened this issue Mar 13, 2018 · 12 comments
Assignees

Comments

@alfC
Copy link
Contributor

alfC commented Mar 13, 2018

In principle, there is no fundamental reasons that a container of trivially_copyable elements can not be made trivially_copyable. This feature can be useful for optimizing and make safe certain operations for applications that access memory directly.

#include<boos/fusion/container/vector.hpp>
int main(){
	assert((std::is_trivially_copyable<boost::fusion::vector<double, int>>::value));
}

I understand the implementation can be tricky and std::pair and std::tuple have this "defect".
Simple "tuples" such as std::complex and std::array do not have this problem, showing that it is possible.

Some material:
https://stackoverflow.com/questions/49120246/which-type-trait-would-indicate-that-type-is-memcpy-assignable-tuple-pair/49142680?noredirect=1#comment85373972_49142680

https://groups.google.com/a/isocpp.org/forum/#!topic/std-discussion/PUZ9WUr2AOU

https://groups.google.com/a/isocpp.org/forum/#!msg/std-proposals/Zi2wriFMFvI/N1JlOE-FBQAJ

https://stackoverflow.com/questions/38779985/why-cant-stdtupleint-be-trivially-copyable?noredirect=1&lq=1

@djowel
Copy link
Collaborator

djowel commented Mar 14, 2018

Makes sense! Would you contribute a patch or a PR? ;-)

@Flast
Copy link
Collaborator

Flast commented Mar 14, 2018

I'm working on this issue incl. optimizing vector. It's not so hard.

@alfC
Copy link
Contributor Author

alfC commented Mar 14, 2018

The same can be said about any std::is_trivially_*** trait.

Also, for reference, Boost.Hana doesn't seem to have this limitation:

static_assert(std::is_trivially_copyable<hana::tuple<double, int>>{});

@vtnerd
Copy link
Contributor

vtnerd commented Mar 20, 2018

I did some initial testing on this a while back for C++11 vector. I didn't post it because I felt like there were already too many changes. Making it trivial to copy / move should also improve compile times since it is currently using a generic function with more template instantiations. Adding a conversion copy from vector also helped compile times (if implemented carefully). So there are multiple positive benefits to this ...

EDIT: Too many changes -> this was when I was fixing one bug and changed the constructor SFINAE implementation for C++11 vector.

@Flast
Copy link
Collaborator

Flast commented May 2, 2018

I have a concern what I want to confirm, following code is (or will be) valid and expected, right?

static_assert(std::is_trivially_copyable<fusion::vector<int&>>{}); // OK
// because the struct is trivially copyable even if contains reference
struct S
{
    int& r;
};
static_assert(std::is_trivially_copyable<S>{}); // also OK

@alfC
Copy link
Contributor Author

alfC commented May 3, 2018 via email

@alfC
Copy link
Contributor Author

alfC commented May 3, 2018 via email

@Flast
Copy link
Collaborator

Flast commented May 3, 2018

@alfC thanks.

My answer as of today is "no", sometimes I use bfn::vector or std::tuple, precisely to avoid the above anomaly of C++ structs, including recursive assignment, and recursive comparison

That usage was my concern. I thought it is natural to me that tuple<int&> is trivially copyable because basically I use tuple as just unnamed struct.

Unfortunately, In my research, makingvector<int&> non trivially copyable is incompatible with trivially copyable vector....

@alfC
Copy link
Contributor Author

alfC commented May 3, 2018 via email

@alfC
Copy link
Contributor Author

alfC commented May 3, 2018

Looks like in C++11 is not that difficult to solve this issue. It seems to be simply necessary to not explicitly define the operator=(tuple const&).

The natural result for tuples containing references is very strange. (Is it desirable?)

#include<utility>
#include<type_traits>

template<class T1, class... Ts>
struct tpl{
   T1 head_;
   tpl<Ts...> rest_;
/*   tpl& operator=(tpl const& o){
       head_ = o.head_;
       rest_ = o.rest_;
       return *this;
   }*/
   template<class Other> tpl& operator=(Other&& o){
       head_ = std::forward<Other>(o).head_;
       rest_ = std::forward<Other>(o).rest_;
       return *this;
   }
};
template<class T>
struct tpl<T>{
   T t_;
/*   tpl& operator=(tpl const& o){
       t_ = o.t_;
   }*/
   template<class Other> tpl& operator=(Other&& o){
       t_ = std::forward<Other>(o).t_;
       return *this;
   }
};
int main()
{
    static_assert(std::is_trivially_copyable<tpl<int, double>>{});
    tpl<int, double> t1 = {};
    tpl<int, double> t2 = {};
    t2 = t1;
    int i1 = {};
    double d1 = {};
    tpl<int&, double&> tr1{i1, d1};
    tpl<int&, double&> tr2{i1, d1};
    tr1 = t1; // fails unless the specializations in line 15
    tr2 = tr1;
    t2 = tr1;
   // this combination of results is strange
    static_assert(!std::is_trivially_copyable<tpl<int&, double&>>{});
    static_assert(std::is_trivially_copyable<tpl<int, double&>>{}); 
    static_assert(std::is_trivially_copyable<tpl<int&, double>>{});
}

@Flast
Copy link
Collaborator

Flast commented May 5, 2018

What do you mean that is "incompatible"? The way I see it is that unfortunately vector<int&> and vector would have to be implemented differently because they will have different implementations of operator=.

I meant, vector<int&> requires user-defined special functions to be not trivially copyable, but vector<int> should not have any user-defined special functions to be trivially copyable.
It can't be solved using SFINAE, because compiler generated special functions (i.e. non-templated) is viable than templated user-defined special functions (n4687 16.3.3 [over.match.best] /1 (1-6)).

IF the language didn't have the quirk it has one would be able to implement a tuple that is trivially copyable very easily by just saying. template<class T1, class... Ts> tuple { T1 t1; tuple<Ts...> rest; }, and operator= will work as expected for tuple<T&, ...> and it would be trivially copiable if T1 is trivially copyable and recursively tuple<Ts...> is trivially copyable.

I'm against this way, recursive may be costlier than trivially copyable.

I've got it! It might be solved using helper base class, like this.

struct non_trivial
{
    non_trivial& operator=(non_trivial const&) { return *this; }
};

struct trivial
{
};

template <typename T, typename U>
struct tpl : std::conditional_t<std::is_trivially_copyable<T>{} && std::is_trivially_copyable<U>{}, trivial, non_trivial>

https://wandbox.org/permlink/k7xnlZDDAubWSI16

It may not break current optimal (non recursive) implementation.

OFF TOPIC:

   // this combination of results is strange
    static_assert(!std::is_trivially_copyable<tpl<int&, double&>>{});
    static_assert(std::is_trivially_copyable<tpl<int, double&>>{}); 
    static_assert(std::is_trivially_copyable<tpl<int&, double>>{});

GCC passes all, but Clang fails on second. I think Clang is correct.
Compiler generating assign of tpl<T> is implicitly deleted (same reason as struct), and then compiler generating assign of tpl<T1, Ts...> instantiates user defined assign (i.e. breaks trivially copyable requirements).

@alfC
Copy link
Contributor Author

alfC commented May 5, 2018

Yes, I agree, although there maybe more natural ways to obtain this result. The other problem is that for the non-trivial case, something needs to be done to operator=(tpl const& other). Not sure if you are looking for a solution in the space of C++11, but I found in comment #173 (comment) that it is possilble to have a very compact implementation that respects the trivially copyable requirement.

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

No branches or pull requests

4 participants