[Bug libstdc++/107367] All standard library algorithms should optimize to pointers internally when they are contiguous iterators after C++20

unlvsur at live dot com gcc-bugzilla@gcc.gnu.org
Tue Oct 25 03:36:22 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107367

--- Comment #2 from cqwrteur <unlvsur at live dot com> ---
(In reply to cqwrteur from comment #1)
> This optimization will prevent duplications of templates over iterators and
> pointers. (vector<int>::iterator and int* duplications for example)
> 
> For example:
> 
> https://godbolt.org/z/9zEajxxa8
> vs
> https://godbolt.org/z/n61vEddj1
> 
> 579 vs 879
For debugging. You can do something like this

template<typename ForwardIterator>
concept can_optimize_to_pointer_impl = 
#ifdef _GLIBCXX_DEBUG
false;
#else
std::contiguous_iterator<ForwardIterator>&&!std::is_pointer_v<ForwardIterator>;
#endif

template<typename ForwardIterator>
constexpr void my_sort(ForwardIterator first,ForwardIterator last)
{
    if constexpr(can_optimize_to_pointer_impl<ForwardIterator>)
    {
        std::sort(std::to_address(first),std::to_address(last));
    }
    else
    {
        std::sort(first,last);
    }
}

https://godbolt.org/z/jj38MoWen


More information about the Gcc-bugs mailing list