rust/library/alloc/tests
Matthias Krüger 772e80a650
Rollup merge of #119917 - Zalathar:split-off, r=cuviper
Remove special-case handling of `vec.split_off(0)`

#76682 added special handling to `Vec::split_off` for the case where `at == 0`. Instead of copying the vector's contents into a freshly-allocated vector and returning it, the special-case code steals the old vector's allocation, and replaces it with a new (empty) buffer with the same capacity.

That eliminates the need to copy the existing elements, but comes at a surprising cost, as seen in #119913. The returned vector's capacity is no longer determined by the size of its contents (as would be expected for a freshly-allocated vector), and instead uses the full capacity of the old vector.

In cases where the capacity is large but the size is small, that results in a much larger capacity than would be expected from reading the documentation of `split_off`. This is especially bad when `split_off` is called in a loop (to recycle a buffer), and the returned vectors have a wide variety of lengths.

I believe it's better to remove the special-case code, and treat `at == 0` just like any other value:
- The current documentation states that `split_off` returns a “newly allocated vector”, which is not actually true in the current implementation when `at == 0`.
- If the value of `at` could be non-zero at runtime, then the caller has already agreed to the cost of a full memcpy of the taken elements in the general case. Avoiding that copy would be nice if it were close to free, but the different handling of capacity means that it is not.
- If the caller specifically wants to avoid copying in the case where `at == 0`, they can easily implement that behaviour themselves using `mem::replace`.

Fixes #119913.
2024-01-26 14:43:30 +01:00
..
arc.rs remove redundant imports 2023-12-10 10:56:22 +08:00
autotraits.rs apply fmt 2024-01-11 15:04:48 +03:00
borrow.rs remove redundant imports 2023-12-10 10:56:22 +08:00
boxed.rs rm const traits in libcore 2023-04-16 06:49:27 +00:00
btree_set_hash.rs Include the length in BTree hashes 2021-10-01 12:29:09 -07:00
c_str.rs Remove use of reverted std::ffi::c_char 2022-04-27 14:01:04 -07:00
const_fns.rs fix library and rustdoc tests 2023-04-16 11:38:52 +00:00
cow_str.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
fmt.rs enable fuzzy_provenance_casts lint in liballoc 2022-11-20 19:12:18 +01:00
heap.rs Rename `AllocRef` to `Allocator` and `(de)alloc` to `(de)allocate` 2020-12-04 14:47:15 +01:00
lib.rs Rollup merge of #118123 - RalfJung:internal-lib-features, r=compiler-errors 2023-12-05 14:52:41 -05:00
linked_list.rs Classify BinaryHeap & LinkedList unit tests as such 2022-03-11 11:33:59 +01:00
rc.rs remove redundant imports 2023-12-10 10:56:22 +08:00
slice.rs Stabilize `slice_group_by` 2023-11-07 17:46:00 +01:00
str.rs optimize str::iter::Chars::advance_by 2023-11-27 22:06:35 +01:00
string.rs Improve test case for experimental API remove_matches in library/alloc/tests/string.rs 2023-07-26 17:54:48 -04:00
thin_box.rs Make use of `pointer::is_aligned[_to]` 2022-08-21 15:46:03 +04:00
vec.rs Rollup merge of #119917 - Zalathar:split-off, r=cuviper 2024-01-26 14:43:30 +01:00
vec_deque.rs ignore core, alloc and test tests that require unwinding on panic=abort 2023-06-13 15:53:24 +02:00