-
Notifications
You must be signed in to change notification settings - Fork 13.8k
[libc++] Optimize vector growing of trivially relocatable types #76657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
philnik777
merged 1 commit into
llvm:main
from
philnik777:uninitialized_allocator_relocate
Feb 2, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _LIBCPP___TYPE_TRAITS_IS_TRIVIALLY_RELOCATABLE_H | ||
#define _LIBCPP___TYPE_TRAITS_IS_TRIVIALLY_RELOCATABLE_H | ||
|
||
#include <__config> | ||
#include <__type_traits/enable_if.h> | ||
#include <__type_traits/integral_constant.h> | ||
#include <__type_traits/is_same.h> | ||
#include <__type_traits/is_trivially_copyable.h> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
#endif | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
// A type is trivially relocatable if a move construct + destroy of the original object is equivalent to | ||
// `memcpy(dst, src, sizeof(T))`. | ||
|
||
#if __has_builtin(__is_trivially_relocatable) | ||
template <class _Tp, class = void> | ||
struct __libcpp_is_trivially_relocatable : integral_constant<bool, __is_trivially_relocatable(_Tp)> {}; | ||
#else | ||
template <class _Tp, class = void> | ||
struct __libcpp_is_trivially_relocatable : is_trivially_copyable<_Tp> {}; | ||
#endif | ||
|
||
template <class _Tp> | ||
struct __libcpp_is_trivially_relocatable<_Tp, | ||
philnik777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
__enable_if_t<is_same<_Tp, typename _Tp::__trivially_relocatable>::value> > | ||
: true_type {}; | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
#endif // _LIBCPP___TYPE_TRAITS_IS_TRIVIALLY_RELOCATABLE_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -982,37 +982,54 @@ template <ranges::input_range _Range, | |
vector(from_range_t, _Range&&, _Alloc = _Alloc()) -> vector<ranges::range_value_t<_Range>, _Alloc>; | ||
#endif | ||
|
||
// __swap_out_circular_buffer relocates the objects in [__begin_, __end_) into the front of __v and swaps the buffers of | ||
// *this and __v. It is assumed that __v provides space for exactly (__end_ - __begin_) objects in the front. This | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a followup NFC, we absolutely need to change the name of |
||
// function has a strong exception guarantee. | ||
template <class _Tp, class _Allocator> | ||
_LIBCPP_CONSTEXPR_SINCE_CXX20 void | ||
vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v) { | ||
__annotate_delete(); | ||
using _RevIter = std::reverse_iterator<pointer>; | ||
__v.__begin_ = std::__uninitialized_allocator_move_if_noexcept( | ||
__alloc(), _RevIter(__end_), _RevIter(__begin_), _RevIter(__v.__begin_)) | ||
.base(); | ||
auto __new_begin = __v.__begin_ - (__end_ - __begin_); | ||
std::__uninitialized_allocator_relocate( | ||
__alloc(), std::__to_address(__begin_), std::__to_address(__end_), std::__to_address(__new_begin)); | ||
__v.__begin_ = __new_begin; | ||
__end_ = __begin_; // All the objects have been destroyed by relocating them. | ||
ldionne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::swap(this->__begin_, __v.__begin_); | ||
std::swap(this->__end_, __v.__end_); | ||
std::swap(this->__end_cap(), __v.__end_cap()); | ||
__v.__first_ = __v.__begin_; | ||
__annotate_new(size()); | ||
} | ||
|
||
// __swap_out_circular_buffer relocates the objects in [__begin_, __p) into the front of __v, the objects in | ||
// [__p, __end_) into the back of __v and swaps the buffers of *this and __v. It is assumed that __v provides space for | ||
// exactly (__p - __begin_) objects in the front and space for at least (__end_ - __p) objects in the back. This | ||
// function has a strong exception guarantee if __begin_ == __p || __end_ == __p. | ||
template <class _Tp, class _Allocator> | ||
_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer | ||
vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p) { | ||
__annotate_delete(); | ||
pointer __r = __v.__begin_; | ||
using _RevIter = std::reverse_iterator<pointer>; | ||
__v.__begin_ = std::__uninitialized_allocator_move_if_noexcept( | ||
__alloc(), _RevIter(__p), _RevIter(__begin_), _RevIter(__v.__begin_)) | ||
.base(); | ||
__v.__end_ = std::__uninitialized_allocator_move_if_noexcept(__alloc(), __p, __end_, __v.__end_); | ||
pointer __ret = __v.__begin_; | ||
|
||
// Relocate [__p, __end_) first to avoid having a hole in [__begin_, __end_) | ||
// in case something in [__begin_, __p) throws. | ||
std::__uninitialized_allocator_relocate( | ||
philnik777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
__alloc(), std::__to_address(__p), std::__to_address(__end_), std::__to_address(__v.__end_)); | ||
__v.__end_ += (__end_ - __p); | ||
__end_ = __p; // The objects in [__p, __end_) have been destroyed by relocating them. | ||
auto __new_begin = __v.__begin_ - (__p - __begin_); | ||
|
||
std::__uninitialized_allocator_relocate( | ||
__alloc(), std::__to_address(__begin_), std::__to_address(__p), std::__to_address(__new_begin)); | ||
__v.__begin_ = __new_begin; | ||
__end_ = __begin_; // All the objects have been destroyed by relocating them. | ||
|
||
std::swap(this->__begin_, __v.__begin_); | ||
std::swap(this->__end_, __v.__end_); | ||
std::swap(this->__end_cap(), __v.__end_cap()); | ||
__v.__first_ = __v.__begin_; | ||
__annotate_new(size()); | ||
return __r; | ||
return __ret; | ||
} | ||
|
||
template <class _Tp, class _Allocator> | ||
philnik777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.