Skip to content

Commit

Permalink
Unconditionally lower std::string's alignment requirement from 16 to 8.
Browse files Browse the repository at this point in the history
As requested in llvm#68807
  • Loading branch information
EricWF committed Oct 12, 2023
1 parent 64d78d8 commit efe2fa3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions libcxx/docs/ReleaseNotes/18.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ ABI Affecting Changes
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.

- The internal alignment requirements for heap allocations inside std::string has decreased from 16 to 8.
This save memory since string requests fewer additional bytes than it did previously. However, this
also changes the return value of std::string::max_length and can cause code compiled against older
libc++ versions but linked at runtime to a new version to thrown a different exception
when attempting allocations that are too large (std::bad_alloc vs std::length_error).

Build System Changes
--------------------

Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/string
Original file line number Diff line number Diff line change
Expand Up @@ -1851,7 +1851,7 @@ private:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
size_type __align_it(size_type __s) _NOEXCEPT
{return (__s + (__a-1)) & ~(__a-1);}
enum {__alignment = 16};
enum { __alignment = 8 };
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
size_type __recommend(size_type __s) _NOEXCEPT
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// <string>

// This test demonstrates the smaller allocation sizes when the alignment
// requirements of std::string are dropped from 16 to 8.

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <string>

#include "test_macros.h"

int main(int, char**) {
std::string input_string;
input_string.resize(64, 'a');

// Call a constructor which selects its size using __recommend.
std::string test_string(input_string.data());
constexpr std::size_t expected_align8_size = 71;
// Previously, when the alignment used to be 16 bytes, the expected
// capacity was 79.
assert(test_string.capacity() == expected_align8_size);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#include "test_macros.h"

// alignment of the string heap buffer is hardcoded to 16
static const std::size_t alignment = 16;

static const std::size_t alignment = 8;

template <class = int>
TEST_CONSTEXPR_CXX20 void full_size() {
Expand Down

0 comments on commit efe2fa3

Please sign in to comment.