Open tcbrindle opened 4 years ago
Could we potentially also use memcmp
is an optimisation in find
, and memset
as an optimisation in fill
?
memcmp
can be used for equal
. memset
can be used for fill
, but only for single-byte value types. Libstdc++ doesn't use memcmp
or memchr
or anything in find
.
You can also use memcmp
for lexicographical_compare
but only for unsigned types, and not for types larger than a byte on little-endian processors.
Thanks for the info Jonathan!
FWIW, we do use memchr
for find
when the needle has integer type and the haystack elements are char
s and !is_constant_evaluated()
:
(Yes, I have an action item to teach this about contiguous_iterator
s.)
When using the mem*
functions from an algorithm used with raw pointers as iterators, be sure to check the pointers for null before calling the mem*
functions. IIRC, they don't appreciate being called with null.
IIRC, they don't appreciate being called with null.
Doing so is undefined behaviour.
When using contiguous iterators with trivially copyable/movable value types, we should be able to optimise
copy
/move
and their backwards versions to usestd::memmove
. This would have knock-on benefits for several other algorithms which end up copying or moving elements in their implementations.Unfortunately
memmove
is notconstexpr
, so we'd need to use C++20std::is_constant_evaluated
to detect whether we're being called at compile-time (and should therefore just use the normal implementation) or at run-time. The preprocessor define__cpp_lib_is_constant_evaluated
is supported in GCC and Clang to find out whetheris_constant_evaluated
is available, but I'm not sure about MSVC.