[libcxx-commits] [libcxx] [libc++] Optimize string copy construction (PR #222021)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 9 05:23:16 PDT 2026


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

>From 6f496bbd3db7442feb5306ff883244228ac1874f Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Tue, 8 Sep 2026 16:56:52 +0200
Subject: [PATCH] [libc++] Optimize string copy construction

---
 libcxx/include/string | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/libcxx/include/string b/libcxx/include/string
index 8b0b648d2c655..ff2059c44389b 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -2596,6 +2596,12 @@ basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_ty
 template <class _CharT, class _Traits, class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void
 basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(const value_type* __s, size_type __sz) {
+#ifndef _LIBCPP_CXX03_LANG
+  if constexpr (__alloc_traits::is_always_equal::value) {
+    [[__maybe_unused__]] auto __val = __sz <= max_size();
+    _LIBCPP_ASSUME(__val);
+  }
+#endif
   pointer __p = __init_internal_buffer(__sz);
   traits_type::copy(std::__to_address(__p), __s, __sz + 1);
 }



More information about the libcxx-commits mailing list