-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++] Refactor vector move constructor with allocator #116449
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
Conversation
672e7cf
to
85b1169
Compare
@llvm/pr-subscribers-libcxx Author: Peng Liu (winner245) ChangesThis PR simplifies the implementation of Full diff: https://github.com/llvm/llvm-project/pull/116449.diff 1 Files Affected:
diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index d2d707d8c913c0..ac07750d8ece2f 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -976,9 +976,7 @@ vector<_Tp, _Allocator>::vector(vector&& __x, const __type_identity_t<allocator_
__x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
} else {
typedef move_iterator<iterator> _Ip;
- auto __guard = std::__make_exception_guard(__destroy_vector(*this));
- assign(_Ip(__x.begin()), _Ip(__x.end()));
- __guard.__complete();
+ __init_with_size(_Ip(__x.begin()), _Ip(__x.end()), __x.size());
}
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM assuming the CI is happy.
85b1169
to
44c0814
Compare
Does this patch add an exception guard that wasn't there before? If so, are we fixing an exception safety issue that we were not handling before? If that's the case, this needs a test. |
No, we're simply re-using a different one, which allows us to remove one. |
The exception guard was there before, but the guard was applied around an |
86a65d0
to
4c8fe79
Compare
4c8fe79
to
e356689
Compare
The existing exception test for this constructor is problematic as it did not throw any exceptions. I have added a new test for this constructor. Additionally, I found other tests in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I pulled the patch locally and can confirm that the test before the patch doesn't actually test anything. So this both refactors the code and also makes sure that the test is working properly.
Since this is a small change, let's land this before the other vector refactorings.
This PR simplifies the implementation of
std::vector
move constructor with alternative allocator by invoking__init_with_size
, instead of callingassign
which ultimately calls__assign_with_size
. The advantage of using__init_with_size
lies in its internal use of exception guard, thus simplifying the code. Furthermore, from a semantic standpoint, it is more intuitive for a constructor to call an initialization function rather than an assignment function.