[libcxx-commits] [libcxx] [libc++] Base string's alignment on __STDCPP_DEFAULT_NEW_ALIGNMENT__ (PR #171785)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Thu Dec 11 01:19:43 PST 2025


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/171785

This allows users to influence how much we overalign `string`s allocations and tune it to the new/delete implementation. If we don't have `__STDCPP_DEFAULT_NEW_ALGINMENT__` or we're not using `std::allocator`, we default to eight byte alignment.


>From 557a42079ba8da0bce2ce667b2033ba672b37e46 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Thu, 11 Dec 2025 10:14:52 +0100
Subject: [PATCH] [libc++] Base string's alignment on
 __STDCPP_DEFAULT_NEW_ALIGNMENT__

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

diff --git a/libcxx/include/string b/libcxx/include/string
index 2b3ba6d2d9b62..d55bcf4db7760 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -2366,7 +2366,12 @@ private:
   static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __align_it(size_type __s) _NOEXCEPT {
     return (__s + (__a - 1)) & ~(__a - 1);
   }
-  enum { __alignment = 8 };
+
+#  ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
+  static inline const size_t __alignment = __is_std_allocator_v<allocator_type> ? __STDCPP_DEFAULT_NEW_ALIGNMENT__ : 8;
+#  else
+  static inline const size_t __alignment = 8;
+#  endif
 
   // This makes sure that we're using a capacity with some extra alignment, since allocators almost always over-align
   // the allocations anyways, improving memory usage. More importantly, this ensures that the lowest bit is never set



More information about the libcxx-commits mailing list