[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
Tue Jul 21 04:30:54 PDT 2026


================
@@ -482,6 +483,41 @@ int main(int argc, char** argv) {
           std::bind_front(bench_impl, std::integral_constant<size_t, large_size>{}, std::false_type{}));
   }
 
+  {
+    static auto bench_impl =
+        []<size_t size, bool opaque, class CharT>(
+            std::integral_constant<size_t, size>,
+            std::bool_constant<opaque>,
+            std::type_identity<CharT>,
+            benchmark::State& state) {
+          std::basic_string<CharT> src(size, 'a');
+          auto getIterator = [&src](size_t i) {
+            // INT_MAX because we want overhead of ThrowingIterator without actually throwing
+            return ThrowingIterator<CharT>(src.data() + i, src.data() + src.size(), INT_MAX);
+          };
+          std::basic_string<CharT> string = src;
+          for (auto _ : state) {
+            benchmark::DoNotOptimize(string);
+            string.clear();
+            if constexpr (opaque)
+              benchmark::DoNotOptimize(src);
+            benchmark::DoNotOptimize(string.append(getIterator(0), getIterator(size)));
+          }
+        };
+
+    bench("std::basic_string::append(ForwardIt, ForwardIt) (opaque)",
----------------
ldionne wrote:

The PR is https://github.com/llvm/llvm-project/pull/210795

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


More information about the libcxx-commits mailing list