Skip to content

Conversation

@winner245
Copy link
Contributor

@winner245 winner245 commented Oct 21, 2024

This PR refactors the std::vector's initializer_list constructors to reduce code duplication.
The constructors now call __init_with_size directly, reducing code duplication and improving
readability and maintainability.

@winner245 winner245 requested a review from a team as a code owner October 21, 2024 17:16
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Oct 21, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 21, 2024

@llvm/pr-subscribers-libcxx

Author: None (winner245)

Changes

This PR refactors the std::vector's initializer_list constructors vector(initializer_list<value_type> __il, const allocator_type& __a) and vector(initializer_list<value_type> __il) to reduce code duplication. The constructors now call __init_with_size directly, reducing code duplication and improving readability and maintainability.


Full diff: https://github.com/llvm/llvm-project/pull/113193.diff

1 Files Affected:

  • (modified) libcxx/include/vector (+2-12)
diff --git a/libcxx/include/vector b/libcxx/include/vector
index dc31f31838264c..5ee2ca52752e23 100644
--- a/libcxx/include/vector
+++ b/libcxx/include/vector
@@ -1292,24 +1292,14 @@ vector<_Tp, _Allocator>::vector(vector&& __x, const __type_identity_t<allocator_
 template <class _Tp, class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI
 vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il) {
-  auto __guard = std::__make_exception_guard(__destroy_vector(*this));
-  if (__il.size() > 0) {
-    __vallocate(__il.size());
-    __construct_at_end(__il.begin(), __il.end(), __il.size());
-  }
-  __guard.__complete();
+  __init_with_size(__il.begin(), __il.end(), __il.size());
 }
 
 template <class _Tp, class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI
 vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
     : __alloc_(__a) {
-  auto __guard = std::__make_exception_guard(__destroy_vector(*this));
-  if (__il.size() > 0) {
-    __vallocate(__il.size());
-    __construct_at_end(__il.begin(), __il.end(), __il.size());
-  }
-  __guard.__complete();
+  __init_with_size(__il.begin(), __il.end(), __il.size());;
 }
 
 #endif // _LIBCPP_CXX03_LANG

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with green CI. The code previously in vector(initializer_list) was an exact duplicate of __init_with_size().

@github-actions
Copy link

github-actions bot commented Oct 21, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@winner245 winner245 requested a review from ldionne October 21, 2024 18:38
@winner245
Copy link
Contributor Author

Hi team,

It looks like the checks were cancelled due to my recent push to fix clang-format issues. I apologize for the inconvenience. This is my very first contribution to the LLVM project. Could someone please help me to re-trigger the checks? I appreciate all your help.

Thanks in advance,
Peng

@ldionne ldionne changed the title Refactor vector constructors to eliminate code duplication [libc++] Refactor vector constructors to eliminate code duplication Oct 22, 2024
@ldionne
Copy link
Member

ldionne commented Oct 22, 2024

@winner245 LLVM has a requirement that each commit is associated to a valid e-mail address. Can you please uncheck the "hide my email" option from your Github preferences?

@winner245
Copy link
Contributor Author

Hi @ldionne and team,

I have unckecked "hide my email" in my settings. Please let me know if there is anything else from my side. I would also like to take this opportunity to express my sincere gratitude for all your invaluable guidance and help throughout this entire process. Though it’s a small step, it truly means a lot to me. I’m eager to continue contributing on this great journey and look forward to learning more from you in the future.

Thank you so much for your help!

Best regards,
Peng

@ldionne ldionne merged commit b263a71 into llvm:main Oct 22, 2024
63 of 67 checks passed
@ldionne
Copy link
Member

ldionne commented Oct 22, 2024

Thanks for your contribution!

@github-actions
Copy link

@winner245 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@winner245 winner245 deleted the winner245/refactor-vector-initlist-ctors branch October 29, 2024 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants