-
Notifications
You must be signed in to change notification settings - Fork 12k
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++] shrink_to_fit
can increase capacity if allocate_at_least
returns a bigger allocation
#95161
Comments
Yeah, |
I had a look and there are 4 usages of
|
Is there a use-case where this is actually a problem and not just happens to make a bug more visible? AFAICT our previous implementation was conforming, but didn't solve the problem you describe in any way. The only thing it would have done is throw away usable space. |
I noticed it because I was more vigilant about the capacity when migrating to This might be an LWG issue: libc++ should be allowed to increase the capacity if the new allocation happens to be larger, which it currently isn't. |
This assures shrink_to_fit does not increase the allocated size. Partly addresses llvm#95161
What do you mean with "AFAICT our previous implementation was conforming"? The provided test code shows the code is not conforming. The example is a bit far-fetched, however @MitalAshok mentioned it happened with an arena allocator. So this sounds like a real-world issue to me. |
Before we used |
I see what you mean. Of course in the past with |
This assures shrink_to_fit does not increase the allocated size. Partly addresses llvm#95161
This assures shrink_to_fit does not increase the allocated size. Partly addresses llvm#95161
vector<bool>'s shrink_to_fit implementation is using the "swap-to-free-container-resources-trick" which only shrinks when the input vector is empty. Since the request to shrink_to_fit is non-binding, this is a valid implementation. It is not a high-quality implementation. Since vector<bool> is not a very popular container the implementation has not been changed and only a test to validate the non-growing property has been added. This was discovered while investigating llvm#95161
vector<bool>'s shrink_to_fit implementation is using the "swap-to-free-container-resources-trick" which only shrinks when the input vector is empty. Since the request to shrink_to_fit is non-binding, this is a valid implementation. It is not a high-quality implementation. Since vector<bool> is not a very popular container the implementation has not been changed and only a test to validate the non-growing property has been added. This was discovered while investigating llvm#95161
This uses the C++98 containter-swap-with-empty-container idom which only shrinks when the container is empty. This will never grow the current container's capacity. This could be improved, but it's conforming. |
This assures shrink_to_fit does not increase the allocated size. Partly addresses llvm#95161
vector<bool>'s shrink_to_fit implementation is using the "swap-to-free-container-resources-trick" which only shrinks when the input vector is empty. Since the request to shrink_to_fit is non-binding, this is a valid implementation. It is not a high-quality implementation. Since vector<bool> is not a very popular container the implementation has not been changed and only a test to validate the non-growing property has been added. This was discovered while investigating llvm#95161
This assures shrink_to_fit does not increase the allocated size. Partly addresses #95161 --------- Co-authored-by: Mital Ashok <mital.vaja@googlemail.com>
`vector<bool>`'s shrink_to_fit implementation is using the "swap-to-free-container-resources-trick" which only shrinks when the input vector is empty. Since the request to shrink_to_fit is non-binding, this is a valid implementation. It is not a high-quality implementation. Since `vector<bool>` is not a very popular container the implementation has not been changed and only a test to validate the non-growing property has been added. This was discovered while investigating #95161.
`vector<bool>`'s shrink_to_fit implementation is using the "swap-to-free-container-resources-trick" which only shrinks when the input vector is empty. Since the request to shrink_to_fit is non-binding, this is a valid implementation. It is not a high-quality implementation. Since `vector<bool>` is not a very popular container the implementation has not been changed and only a test to validate the non-growing property has been added. This was discovered while investigating llvm#95161. (cherry picked from commit c2e4386)
This ensures that shrink_to_fit does not increase the allocated size. Partly addresses #95161
This ensures that shrink_to_fit does not increase the allocated size. Partly addresses llvm#95161 (cherry picked from commit d0ca9f2)
This assures shrink_to_fit does not increase the allocated size. Partly addresses llvm#95161 --------- Co-authored-by: Mital Ashok <mital.vaja@googlemail.com>
`vector<bool>`'s shrink_to_fit implementation is using the "swap-to-free-container-resources-trick" which only shrinks when the input vector is empty. Since the request to shrink_to_fit is non-binding, this is a valid implementation. It is not a high-quality implementation. Since `vector<bool>` is not a very popular container the implementation has not been changed and only a test to validate the non-growing property has been added. This was discovered while investigating llvm#95161.
This ensures that shrink_to_fit does not increase the allocated size. Partly addresses llvm#95161
`vector<bool>`'s shrink_to_fit implementation is using the "swap-to-free-container-resources-trick" which only shrinks when the input vector is empty. Since the request to shrink_to_fit is non-binding, this is a valid implementation. It is not a high-quality implementation. Since `vector<bool>` is not a very popular container the implementation has not been changed and only a test to validate the non-growing property has been added. This was discovered while investigating llvm#95161. (cherry picked from commit c2e4386)
This ensures that shrink_to_fit does not increase the allocated size. Partly addresses llvm#95161 (cherry picked from commit d0ca9f2)
Summary: This assures shrink_to_fit does not increase the allocated size. Partly addresses #95161 --------- Co-authored-by: Mital Ashok <mital.vaja@googlemail.com> Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251479
Summary: `vector<bool>`'s shrink_to_fit implementation is using the "swap-to-free-container-resources-trick" which only shrinks when the input vector is empty. Since the request to shrink_to_fit is non-binding, this is a valid implementation. It is not a high-quality implementation. Since `vector<bool>` is not a very popular container the implementation has not been changed and only a test to validate the non-growing property has been added. This was discovered while investigating #95161. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251031
Summary: This ensures that shrink_to_fit does not increase the allocated size. Partly addresses #95161 Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251164
https://eel.is/c++draft/vector.capacity#9.sentence-3
https://godbolt.org/z/hP63rn661
The new capacity isn't checked before the elements are moved to the new allocation:
llvm-project/libcxx/include/vector
Lines 1438 to 1440 in a13bc97
This came up when using an arena allocator and the smaller arenas filled up and
shrink_to_fit
didn't helpThe text was updated successfully, but these errors were encountered: