From 7523a5612d2fbd5c8b1bead6219d2b5c25946ca1 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Mon, 22 Jan 2024 16:37:40 -0600 Subject: [PATCH] Unconditionally change std::string's alignment to 8. Save memory by providing the allocator more freedom to allocate the most efficient size class by dropping the alignment requirements for std::string's pointer from 16 to 8. This changes the output of std::string::max_size, which makes it ABI breaking. That said, the discussion concluded that we don't care about this ABI break and would like this change enabled universally --- libcxx/include/__config | 5 ----- libcxx/include/string | 7 +------ .../string.capacity/allocation_size.pass.cpp | 10 ++-------- .../basic.string/string.capacity/max_size.pass.cpp | 10 ++-------- 4 files changed, 5 insertions(+), 27 deletions(-) diff --git a/libcxx/include/__config b/libcxx/include/__config index 9a64cdb489119d..6fd44a3b915f5d 100644 --- a/libcxx/include/__config +++ b/libcxx/include/__config @@ -174,11 +174,6 @@ // The implementation moved to the header, but we still export the symbols from // the dylib for backwards compatibility. # define _LIBCPP_ABI_DO_NOT_EXPORT_TO_CHARS_BASE_10 -// Save memory by providing the allocator more freedom to allocate the most -// efficient size class by dropping the alignment requirements for std::string's -// pointer from 16 to 8. This changes the output of std::string::max_size, -// which makes it ABI breaking -# define _LIBCPP_ABI_STRING_8_BYTE_ALIGNMENT # elif _LIBCPP_ABI_VERSION == 1 # if !(defined(_LIBCPP_OBJECT_FORMAT_COFF) || defined(_LIBCPP_OBJECT_FORMAT_XCOFF)) // Enable compiling copies of now inline methods into the dylib to support diff --git a/libcxx/include/string b/libcxx/include/string index e97139206d4fa7..ba169c3dbfc9e6 100644 --- a/libcxx/include/string +++ b/libcxx/include/string @@ -1937,12 +1937,7 @@ private: return (__s + (__a - 1)) & ~(__a - 1); } enum { - __alignment = -#ifdef _LIBCPP_ABI_STRING_8_BYTE_ALIGNMENT - 8 -#else - 16 -#endif + __alignment = 8 }; static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __recommend(size_type __s) _NOEXCEPT { if (__s < __min_cap) { diff --git a/libcxx/test/libcxx/strings/basic.string/string.capacity/allocation_size.pass.cpp b/libcxx/test/libcxx/strings/basic.string/string.capacity/allocation_size.pass.cpp index c7df56c815a805..6791ada40894c7 100644 --- a/libcxx/test/libcxx/strings/basic.string/string.capacity/allocation_size.pass.cpp +++ b/libcxx/test/libcxx/strings/basic.string/string.capacity/allocation_size.pass.cpp @@ -17,14 +17,8 @@ #include "test_macros.h" -// alignment of the string heap buffer is hardcoded to either 16 or 8 - -const std::size_t alignment = -#ifdef _LIBCPP_ABI_STRING_8_BYTE_ALIGNMENT - 8; -#else - 16; -#endif +// alignment of the string heap buffer is hardcoded to 8 +const std::size_t alignment = 8; int main(int, char**) { std::string input_string; diff --git a/libcxx/test/libcxx/strings/basic.string/string.capacity/max_size.pass.cpp b/libcxx/test/libcxx/strings/basic.string/string.capacity/max_size.pass.cpp index a3cb79522f2e1e..726570beb6d1ae 100644 --- a/libcxx/test/libcxx/strings/basic.string/string.capacity/max_size.pass.cpp +++ b/libcxx/test/libcxx/strings/basic.string/string.capacity/max_size.pass.cpp @@ -17,14 +17,8 @@ #include "test_macros.h" -// alignment of the string heap buffer is hardcoded to 16 - -static const std::size_t alignment = -#ifdef _LIBCPP_ABI_STRING_8_BYTE_ALIGNMENT - 8; -#else - 16; -#endif +// alignment of the string heap buffer is hardcoded to 8 +static const std::size_t alignment = 8; template TEST_CONSTEXPR_CXX20 void full_size() {