[libcxx-commits] [libcxx] [libc++] Optimize std::find if types are integral (PR #70345)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu Oct 26 09:37:38 PDT 2023


================
@@ -73,6 +73,19 @@ __find_impl(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) {
 }
 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
 
+template <class _Tp,
+          class _Up,
+          class _Proj,
+          __enable_if_t<__is_identity<_Proj>::value && !__libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&
+                            is_integral<_Tp>::value && is_integral<_Up>::value,
+                        int> = 0>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*
+__find_impl(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) {
+  if (__value < numeric_limits<_Tp>::min() || __value > numeric_limits<_Tp>::max())
+    return __last;
----------------
ldionne wrote:

I like this optimization in principle. However, I am a bit scared of getting it wrong, since we basically have to be bug-for-bug compatible with whatever the compiler would do (in terms of integral promotions when doing comparisons, etc).

We would definitely need tests where we have `find(int8_t*, int8_t*, long long)` for example, where we will be basically narrowing `_Up` to `_Tp`. And we should have tests for the other way around although it is a bit less concerning.

https://github.com/llvm/llvm-project/pull/70345


More information about the libcxx-commits mailing list