[libcxx-commits] [libcxx] [libc++] Optimize string::pop_back() (PR #223729)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Sep 22 01:37:08 PDT 2026


https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/223729

>From e3bac0269418874f94f662ce2119960ea7820273 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Tue, 15 Sep 2026 16:54:06 +0200
Subject: [PATCH] [libc++] Optimize string::pop_back()

---
 .../benchmarks/containers/string.bench.cpp    | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/libcxx/test/benchmarks/containers/string.bench.cpp b/libcxx/test/benchmarks/containers/string.bench.cpp
index b795654e84281d..54ffaf4281b059 100644
--- a/libcxx/test/benchmarks/containers/string.bench.cpp
+++ b/libcxx/test/benchmarks/containers/string.bench.cpp
@@ -263,6 +263,33 @@ int main(int argc, char** argv) {
           [](auto bm) { bm->Arg(small_size)->Arg(large_size); });
   }
 
+  {
+    static auto bench_impl =
+        []<bool opaque, class CharT>(std::bool_constant<opaque>, std::type_identity<CharT>, benchmark::State& state) {
+          std::basic_string<CharT> strings[4096];
+
+          size_t size = state.range();
+          size_t pos  = size / 2;
+          while (state.KeepRunningBatch(std::size(strings))) {
+            state.PauseTiming();
+            for (auto& string : strings)
+              string.resize(size, 'a');
+            state.ResumeTiming();
+            for (auto& string : strings) {
+              if constexpr (opaque)
+                benchmark::DoNotOptimize(pos);
+              string.pop_back();
+            }
+          }
+        };
+    bench("std::basic_string::pop_back() (opaque)", std::bind_front(bench_impl, std::true_type{}), [](auto bm) {
+      bm->Arg(small_size)->Arg(large_size);
+    });
+    bench("std::basic_string::pop_back() (transparent)", std::bind_front(bench_impl, std::false_type{}), [](auto bm) {
+      bm->Arg(small_size)->Arg(large_size);
+    });
+  }
+
   // [string.ops]
   bench("std::basic_string::data()", []<class CharT>(std::type_identity<CharT>, benchmark::State& state) {
     std::basic_string<CharT> str;



More information about the libcxx-commits mailing list