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

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 18 12:20:46 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)) {
----------------
ldionne wrote:

This is an interesting idea! This will only trigger for indices that are large and known at compile-time, though -- do you have a use case where that actually makes a meaningful difference? You'd have to generate a large number of index accesses at compile-time in order for this to matter in practice?

Or I guess if you have a loop that processes many strings but always accesses them at a constant-but-large index, then this might make sense. Is that your use-case?

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


More information about the libcxx-commits mailing list