-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
[libc++] Refactor vector::push_back to use vector::emplace #113481
Merged
philnik777
merged 1 commit into
llvm:main
from
ldionne:review/refactor-vector-push-back
Oct 24, 2024
Merged
[libc++] Refactor vector::push_back to use vector::emplace #113481
philnik777
merged 1 commit into
llvm:main
from
ldionne:review/refactor-vector-push-back
Oct 24, 2024
Conversation
This file contains 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 removes some duplicate code. I suspect this was originally written that way because vector::emplace didn't exist in C++03 mode, which stopped being relevant when Clang implemented rvalue references in C++03.
CC @philnik777 |
llvmbot
added
the
libc++
libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
label
Oct 23, 2024
@llvm/pr-subscribers-libcxx Author: Louis Dionne (ldionne) ChangesThis removes some duplicate code. I suspect this was originally written that way because vector::emplace didn't exist in C++03 mode, which stopped being relevant when Clang implemented rvalue references in C++03. Full diff: https://github.com/llvm/llvm-project/pull/113481.diff 1 Files Affected:
diff --git a/libcxx/include/vector b/libcxx/include/vector
index bfbf1ea1cfc9f0..ce412a829ea8f7 100644
--- a/libcxx/include/vector
+++ b/libcxx/include/vector
@@ -689,9 +689,9 @@ public:
return std::__to_address(this->__begin_);
}
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x);
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x) { emplace_back(__x); }
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x) { emplace_back(std::move(__x)); }
template <class... _Args>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
@@ -898,9 +898,6 @@ private:
__annotate_shrink(__old_size);
}
- template <class _Up>
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI inline pointer __push_back_slow_path(_Up&& __x);
-
template <class... _Args>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI inline pointer __emplace_back_slow_path(_Args&&... __args);
@@ -1491,44 +1488,6 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::shrink_to_fit() _NOE
}
}
-template <class _Tp, class _Allocator>
-template <class _Up>
-_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer
-vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x) {
- allocator_type& __a = this->__alloc();
- __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
- // __v.push_back(std::forward<_Up>(__x));
- __alloc_traits::construct(__a, std::__to_address(__v.__end_), std::forward<_Up>(__x));
- __v.__end_++;
- __swap_out_circular_buffer(__v);
- return this->__end_;
-}
-
-template <class _Tp, class _Allocator>
-_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
-vector<_Tp, _Allocator>::push_back(const_reference __x) {
- pointer __end = this->__end_;
- if (__end < this->__end_cap()) {
- __construct_one_at_end(__x);
- ++__end;
- } else {
- __end = __push_back_slow_path(__x);
- }
- this->__end_ = __end;
-}
-
-template <class _Tp, class _Allocator>
-_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void vector<_Tp, _Allocator>::push_back(value_type&& __x) {
- pointer __end = this->__end_;
- if (__end < this->__end_cap()) {
- __construct_one_at_end(std::move(__x));
- ++__end;
- } else {
- __end = __push_back_slow_path(std::move(__x));
- }
- this->__end_ = __end;
-}
-
template <class _Tp, class _Allocator>
template <class... _Args>
_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer
|
philnik777
approved these changes
Oct 23, 2024
Closed
NoumanAmir657
pushed a commit
to NoumanAmir657/llvm-project
that referenced
this pull request
Nov 4, 2024
This removes some duplicate code. I suspect this was originally written that way because vector::emplace didn't exist in C++03 mode, which stopped being relevant when Clang implemented rvalue references in C++03.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This removes some duplicate code. I suspect this was originally written that way because vector::emplace didn't exist in C++03 mode, which stopped being relevant when Clang implemented rvalue references in C++03.