[libcxx-commits] [libcxx] [libc++][string] Improve constexpr efficiency (PR #184724)

Vitaly Buka via libcxx-commits libcxx-commits at lists.llvm.org
Sat Oct 3 21:11:30 PDT 2026


https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/184724

>From 2b202256ebe1b844f673fd036eb2234b3e00810d Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Wed, 4 Mar 2026 18:33:17 -0800
Subject: [PATCH] [libc++][string] Improve constexpr performance

Adding `if (__libcpp_is_constant_evaluated()) return` allows
significantly increase complexity of extression.

In case of Asan it changed from 1000 to 6000.

Pull Request: https://github.com/llvm/llvm-project/pull/184724
---
 libcxx/include/string | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/libcxx/include/string b/libcxx/include/string
index a27f9344916d1b6..13feae3e0dc8e4d 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -2243,18 +2243,26 @@ private:
   }
 
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_new(size_type __current_size) const _NOEXCEPT {
+    if (__libcpp_is_constant_evaluated())
+      return;
     __annotate_contiguous_container(data() + capacity() + 1, data() + __current_size + 1);
   }
 
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_delete() const _NOEXCEPT {
+    if (__libcpp_is_constant_evaluated())
+      return;
     __annotate_contiguous_container(data() + size() + 1, data() + capacity() + 1);
   }
 
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_increase(size_type __n) const _NOEXCEPT {
+    if (__libcpp_is_constant_evaluated())
+      return;
     __annotate_contiguous_container(data() + size() + 1, data() + size() + 1 + __n);
   }
 
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_shrink(size_type __old_size) const _NOEXCEPT {
+    if (__libcpp_is_constant_evaluated())
+      return;
     __annotate_contiguous_container(data() + __old_size + 1, data() + size() + 1);
   }
 



More information about the libcxx-commits mailing list