Small optimization of vector (or other container comparisons)

Theodore Papadopoulo Theodore.Papadopoulo@inria.fr
Mon Nov 16 19:08:35 GMT 2020


     Hi,

     Sorry if this is a naive question...

I wonder whether it will be legal and/or interesting to modify vector 
comparison so that it returns early when the vectors have the same address
ie replace

template<typename _Tp, typename _Alloc>
inline bool
     operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, 
_Alloc>& __y)
     { return (__x.size() == __y.size()
           && std::equal(__x.begin(), __x.end(), __y.begin())); }

by

template<typename _Tp, typename _Alloc>
inline bool
     operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, 
_Alloc>& __y)
     { return (&__x==&__y) || (__x.size() == __y.size()
           && std::equal(__x.begin(), __x.end(), __y.begin()))); }

Of course, the exact gain depends on the ratio of same vector vs 
different vector comparisons and of the actual size of
the vectors, but it seems to add little extra cost and the address test 
may even also sometimes be removed by the compiler.

Obviously, it is always possible for a user to do it by itself, but 
integrating it is probably easier for the average user.....

     Thanks

         Theo.





More information about the Libstdc++ mailing list