-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++] Fix no-op shrink_to_fit for vector<bool> #120495
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
89af9e2
to
98c17a2
Compare
@llvm/pr-subscribers-libcxx Author: Peng Liu (winner245) ChangesThe current implementation of if (__external_cap_to_internal(size()) > __cap_) {
try {
do_shrink();
} catch (...) {
}
} However, this condition is always false (with one exception), as the number of used internal words if (empty())
deallocate(); which avoids the try/catch clause and becomes much cleaner and simpler. While the current implementation technically conforms to the standard—since the shrink-to-fit request is non-binding—it appears to be a logical error to me, particularly since a similar logical error was found in the This PR addresses the issue by modifying Full diff: https://github.com/llvm/llvm-project/pull/120495.diff 1 Files Affected:
diff --git a/libcxx/include/__vector/vector_bool.h b/libcxx/include/__vector/vector_bool.h
index 36eb7f350ac406..190ea2585b7821 100644
--- a/libcxx/include/__vector/vector_bool.h
+++ b/libcxx/include/__vector/vector_bool.h
@@ -841,11 +841,13 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::reserve(size_type _
template <class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT {
- if (__external_cap_to_internal(size()) > __cap_) {
+ if (__external_cap_to_internal(size()) < __cap_) {
#if _LIBCPP_HAS_EXCEPTIONS
try {
#endif // _LIBCPP_HAS_EXCEPTIONS
- vector(*this, allocator_type(__alloc_)).swap(*this);
+ vector __v(*this, allocator_type(__alloc_));
+ if (__v.__cap_ < __cap_)
+ __v.swap(*this);
#if _LIBCPP_HAS_EXCEPTIONS
} catch (...) {
}
|
98c17a2
to
3fb8884
Compare
f2e48f4
to
a3d32ec
Compare
a3d32ec
to
9d23aa3
Compare
Thanks for the fix. This fixes some google code broken by #120577. |
@asmok-g Thanks for your feedback! The previous change #120577 was an internal fix. I'm curious about how it caused issues with Google code, and how my current PR fixes that. Could you provide more details or context about the specific problems it created? This will help me understand the impact and ensure a comprehensive fix. |
We got |
I suspect that the issues we've seen may be caused by the same effect as described in #122502. If we find a distinct problem, we'll let you know. |
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 with a few comments, thanks a lot for fixing this!
libcxx/test/std/containers/sequences/vector.bool/shrink_to_fit.pass.cpp
Outdated
Show resolved
Hide resolved
9d23aa3
to
1108295
Compare
This PR addresses an issue where the
shrink_to_fit
function invector<bool>
is effectively a no-op, meaning it will never shrink the capacity.Fixes #122502.