<algorithm>
: count()
is missing vectorization opportunities in C++14/17
#3245
Labels
<algorithm>
: count()
is missing vectorization opportunities in C++14/17
#3245
While investigating #3244, I found something curious -
count()
was affected by that correctness bug only in C++20/23 mode whenvector
iterators are used (see https://godbolt.org/z/dcTscbeTn ). This is because there's a performance bug in C++14/17 mode.There are 4 uses of
_Within_limits
in the STL (affected by said correctness bug, which is irrelevant for the purposes of this performance bug). Each use is preceded by checking_Vector_alg_in_find_is_safe
.ranges::_Count_fn::_Count_unchecked
:STL/stl/inc/algorithm
Lines 442 to 453 in e28f956
std::_Find_unchecked
:STL/stl/inc/xutility
Lines 5603 to 5612 in e28f956
ranges::_Find_unchecked
:STL/stl/inc/xutility
Lines 5674 to 5681 in e28f956
std::count
:STL/stl/inc/xutility
Lines 5779 to 5795 in e28f956
The performance bug 🐞 is that
std::count
is templated on a wrapped iterator_InIt
(in my example, avector
iterator), and we check_Vector_alg_in_find_is_safe<_InIt, _Ty>
with that wrapped iterator. However, the following code correctly uses the unwrapped iterator_UFirst
(with both_Within_limits
and__std_count_trivial
).The 14/17 versus 20/23 dependency is because
_Vector_alg_in_find_is_safe
uses_Iterator_is_contiguous
:STL/stl/inc/xutility
Lines 5591 to 5593 in e28f956
In C++20/23 mode, it's powered by concepts and can detect arbitrary contiguous iterators, compensating for the perf bug:
STL/stl/inc/xutility
Lines 4226 to 4229 in e28f956
But in C++14/17 mode, it can only detect pointers, so the perf bug is revealed:
STL/stl/inc/xutility
Lines 4236 to 4239 in e28f956
As the comment says: "(Iterators should be unwrapped before using this.)"
The text was updated successfully, but these errors were encountered: