[libcxx-commits] [libcxx] [libc++] fix minor performance issue in `basic_string<C>::append()` (PR #210078)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jul 20 06:24:33 PDT 2026


================
@@ -1359,13 +1359,13 @@ public:
   template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
   append(_ForwardIterator __first, _ForwardIterator __last) {
-    size_type __sz  = size();
-    size_type __cap = capacity();
-    size_type __n   = static_cast<size_type>(std::distance(__first, __last));
----------------
ldionne wrote:

I think this actually makes a lot of sense. It seems very realistic to me that computing the distance between two iterators could be more expensive than checking whether the iterator is at the end. Hence, checking `first == last` explicitly instead of inferring it from a potentially much more complex computation `distance(first, last) == 0` makes sense to me.

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


More information about the libcxx-commits mailing list