[🍒] Unconditionally lower std::string's alignment requirement from 16 to 8 (#68925) (#79480)

This change saves 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.

The ABI break isn't one of layout or "class size", but rather the value
of "max_size()" changes, which in turn changes whether `std::bad_alloc`
or `std::length_error` is thrown for large allocations.

This change is the child of PR #68807, which enabled the change behind
an ABI flag.
This commit is contained in:
Eric 2024-02-06 19:01:38 -06:00 committed by GitHub
parent 7fb11860b0
commit 5de1bcb70d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 17 additions and 32 deletions

View File

@ -275,11 +275,10 @@ ABI Affecting Changes
results in an ABI break, however in practice we expect uses of ``std::projected`` in ABI-sensitive places to be results in an ABI break, however in practice we expect uses of ``std::projected`` in ABI-sensitive places to be
extremely rare. Any error resulting from this change should result in a link-time error. extremely rare. Any error resulting from this change should result in a link-time error.
- Under the unstable ABI, the internal alignment requirements for heap allocations - The internal alignment requirements for heap allocations inside ``std::string`` has decreased from 16 to 8. This
inside ``std::string`` has decreased from 16 to 8. This saves memory since string requests fewer additional saves memory since string requests fewer additional bytes than it did previously. However, this also changes the
bytes than it did previously. However, this also changes the return value of ``std::string::max_size`` return value of ``std::string::max_size`` and can cause code compiled against older libc++ versions but linked at
and can cause code compiled against older libc++ versions but linked at runtime to a new version runtime to a new version to throw a different exception when attempting allocations that are too large
to throw a different exception when attempting allocations that are too large
(``std::bad_alloc`` vs ``std::length_error``). (``std::bad_alloc`` vs ``std::length_error``).
- The layout of some range adaptors that use the ``movable-box`` exposition-only type as an implementation - The layout of some range adaptors that use the ``movable-box`` exposition-only type as an implementation

View File

@ -174,11 +174,6 @@
// The implementation moved to the header, but we still export the symbols from // The implementation moved to the header, but we still export the symbols from
// the dylib for backwards compatibility. // the dylib for backwards compatibility.
# define _LIBCPP_ABI_DO_NOT_EXPORT_TO_CHARS_BASE_10 # 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 # elif _LIBCPP_ABI_VERSION == 1
# if !(defined(_LIBCPP_OBJECT_FORMAT_COFF) || defined(_LIBCPP_OBJECT_FORMAT_XCOFF)) # if !(defined(_LIBCPP_OBJECT_FORMAT_COFF) || defined(_LIBCPP_OBJECT_FORMAT_XCOFF))
// Enable compiling copies of now inline methods into the dylib to support // Enable compiling copies of now inline methods into the dylib to support

View File

@ -1937,12 +1937,7 @@ private:
return (__s + (__a - 1)) & ~(__a - 1); return (__s + (__a - 1)) & ~(__a - 1);
} }
enum { enum {
__alignment = __alignment = 8
#ifdef _LIBCPP_ABI_STRING_8_BYTE_ALIGNMENT
8
#else
16
#endif
}; };
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __recommend(size_type __s) _NOEXCEPT { static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __recommend(size_type __s) _NOEXCEPT {
if (__s < __min_cap) { if (__s < __min_cap) {

View File

@ -6,6 +6,8 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// XFAIL: stdlib=apple-libc++ && target={{.+}}-apple-macosx{{10.13|10.15|11.0}}
// <string> // <string>
// This test demonstrates the smaller allocation sizes when the alignment // This test demonstrates the smaller allocation sizes when the alignment
@ -17,14 +19,8 @@
#include "test_macros.h" #include "test_macros.h"
// alignment of the string heap buffer is hardcoded to either 16 or 8 // alignment of the string heap buffer is hardcoded to 8
const std::size_t alignment = 8;
const std::size_t alignment =
#ifdef _LIBCPP_ABI_STRING_8_BYTE_ALIGNMENT
8;
#else
16;
#endif
int main(int, char**) { int main(int, char**) {
std::string input_string; std::string input_string;

View File

@ -17,14 +17,8 @@
#include "test_macros.h" #include "test_macros.h"
// alignment of the string heap buffer is hardcoded to 16 // alignment of the string heap buffer is hardcoded to 8
static const std::size_t alignment = 8;
static const std::size_t alignment =
#ifdef _LIBCPP_ABI_STRING_8_BYTE_ALIGNMENT
8;
#else
16;
#endif
template <class = int> template <class = int>
TEST_CONSTEXPR_CXX20 void full_size() { TEST_CONSTEXPR_CXX20 void full_size() {

View File

@ -7,6 +7,12 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// UNSUPPORTED: no-exceptions // UNSUPPORTED: no-exceptions
// After changing the alignment of the allocated pointer from 16 to 8, the exception thrown is no longer `bad_alloc`
// but instead length_error on systems using new headers but older dylibs.
//
// XFAIL: stdlib=apple-libc++ && target={{.+}}-apple-macosx{{10.13|10.15|11.0}}
// <string> // <string>
// size_type max_size() const; // constexpr since C++20 // size_type max_size() const; // constexpr since C++20