[libcxx-commits] [libcxx] [libc++] Optimize string operator[] for known large inputs (PR #69500)

Ilya Tocar via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 18 12:44:55 PDT 2023


================
@@ -1198,11 +1198,17 @@ public:
 
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference operator[](size_type __pos) const _NOEXCEPT {
     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos <= size(), "string index out of bounds");
+    if (__builtin_constant_p(__pos) && !__fits_in_sso(__pos)) {
----------------
TocarIP wrote:

The original use case was a hash function specific to a data set that skipped most of the string and hashed only specific bytes (e.g s[32]*37+s[64]). This wasn't a major performance change, but removing branches seems like a good idea in general.

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


More information about the libcxx-commits mailing list