rust/library/alloc/tests
bors 6cae28165f Auto merge of #76682 - richkadel:vec-take, r=Mark-Simulacrum
Optimize behavior of vec.split_off(0) (take all)

Optimization improvement to `split_off()` so the performance meets the
intuitively expected behavior when `at == 0`, avoiding the current behavior
of copying the entire vector.

The change honors documented behavior that the original vector's
"previous capacity unchanged".

This improvement better supports the pattern for building and flushing a
buffer of elements, such as the following:

```rust
    let mut vec = Vec::new();
    loop {
        vec.push(something);
        if condition_is_met {
            process(vec.split_off(0));
        }
    }
```

`Option` wrapping is the first alternative I thought of, but is much
less obvious and more verbose:

```rust
    let mut capacity = 1;
    let mut vec: Option<Vec<Stuff>> = None;
    loop {
        vec.get_or_insert_with(|| Vec::with_capacity(capacity)).push(something);
        if condition_is_met {
            capacity = vec.capacity();
            process(vec.take().unwrap());
        }
    }
```

Directly using `mem::replace()` (instead of  calling`split_off()`) could work,
but `mem::replace()` is a more advanced tool for Rust developers, and in
this case, I believe developers would assume the standard library should
be sufficient for the purpose described here.

The benefit of the approach to this change is it does not change the
existing API contract, but improves the peformance of `split_off(0)` for
`Vec`, `String` (which delegates `split_off()` to `Vec`), and any other
existing use cases.

This change adds tests to validate the behavior of `split_off()` with
regard to capacity, as originally documented, and confirm that behavior
still holds, when `at == 0`.

The change is an implementation detail, and does not require a
documentation change, but documenting the new behavior as part of its
API contract may benefit future users.

(Let me know if I should make that documentation update.)

Note, for future consideration:

I think it would be helpful to introduce an additional method to `Vec`
(if not also to `String`):

```
    pub fn take_all(&mut self) -> Self {
        self.split_off(0)
    }
```

This would make it more clear how `Vec` supports the pattern, and make
it easier to find, since the behavior is similar to other `take()`
methods in the Rust standard library.

r? `@wesleywiser`
FYI: `@tmandry`
2020-09-15 05:01:17 +00:00
..
arc.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
binary_heap.rs in-place collect for Vec. Box<[]> and BinaryHeap IntoIter and some adapters 2020-09-03 20:59:03 +02:00
borrow.rs Move various ui const tests to library 2020-09-04 02:35:27 +02:00
boxed.rs fix typos 2020-08-02 23:20:00 +08:00
btree_set_hash.rs Move btree unit test to their native, privileged location 2020-08-14 17:54:09 +02:00
cow_str.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
fmt.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
heap.rs Replace Memoryblock with NonNull<[u8]> 2020-08-04 18:03:34 +02:00
lib.rs Move various ui const tests to library 2020-09-04 02:35:27 +02:00
linked_list.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
rc.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
slice.rs in-place collect for Vec. Box<[]> and BinaryHeap IntoIter and some adapters 2020-09-03 20:59:03 +02:00
str.rs Move test-cases in string.rs 2020-09-06 09:23:40 +05:30
string.rs Optimize behavior of vec.split_off(0) (take all) 2020-09-13 14:32:29 -07:00
vec_deque.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
vec.rs Auto merge of #76682 - richkadel:vec-take, r=Mark-Simulacrum 2020-09-15 05:01:17 +00:00