[all-commits] [llvm/llvm-project] 31824b: [libc++] Fix shrink_to_fit to swap buffer only whe...
Peng Liu via All-commits
all-commits at lists.llvm.org
Sat Feb 22 05:51:10 PST 2025
Branch: refs/heads/main
Home: https://github.com/llvm/llvm-project
Commit: 31824b2a11a8aa3e1d5c699cd0786ad666999abf
https://github.com/llvm/llvm-project/commit/31824b2a11a8aa3e1d5c699cd0786ad666999abf
Author: Peng Liu <winner245 at hotmail.com>
Date: 2025-02-22 (Sat, 22 Feb 2025)
Changed paths:
M libcxx/include/string
M libcxx/test/libcxx/strings/basic.string/string.capacity/shrink_to_fit.pass.cpp
M libcxx/test/std/strings/basic.string/string.capacity/shrink_to_fit.pass.cpp
M libcxx/test/support/increasing_allocator.h
Log Message:
-----------
[libc++] Fix shrink_to_fit to swap buffer only when capacity is strictly smaller (#127321)
The current implementation of the `shrink_to_fit()` function of
`basic_string` swaps to the newly allocated buffer when the new buffer
has the same capacity as the existing one. While this is not incorrect,
it is truly unnecessary to swap to an equally-sized buffer. With equal
capacity, we should keep using the existing buffer and simply deallocate
the new one, avoiding the extra work of copying elements.
The desired behavior was documented in the following comment within the
function:
https://github.com/llvm/llvm-project/blob/61ad08792a86e62309b982189a600f4342a38d91/libcxx/include/string#L3560-L3566
However, the existing implementation did not exactly conform to this
guideline, which is a QoI matter.
This PR modifies the `shrink_to_fit()` function to ensure that the
buffer is only swapped when the new allocation is strictly smaller than
the existing one. When the capacities are equal, the new buffer will be
discarded without copying the elements. This is achieved by including
the `==` check in the above conditional logic.
To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications
More information about the All-commits
mailing list