Skip to content

Commit 79aa9b1

Browse files
committed
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 method leaves 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 applying `mem::replace()` could work, but `mem::` functions are typically a last resort, when a developer is actively seeking better performance than the standard library provides, for example. 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.
1 parent a1894e4 commit 79aa9b1

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

library/alloc/src/vec.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,11 @@ impl<T> Vec<T> {
14101410
assert_failed(at, self.len());
14111411
}
14121412

1413+
if at == 0 {
1414+
// the new vector can take over the original buffer and avoid the copy
1415+
return mem::replace(self, Vec::with_capacity(self.capacity()));
1416+
}
1417+
14131418
let other_len = self.len - at;
14141419
let mut other = Vec::with_capacity(other_len);
14151420

library/alloc/tests/string.rs

+4
Original file line numberDiff line numberDiff line change
@@ -278,17 +278,21 @@ fn test_split_off_mid_char() {
278278
#[test]
279279
fn test_split_off_ascii() {
280280
let mut ab = String::from("ABCD");
281+
let orig_capacity = ab.capacity();
281282
let cd = ab.split_off(2);
282283
assert_eq!(ab, "AB");
283284
assert_eq!(cd, "CD");
285+
assert_eq!(ab.capacity(), orig_capacity);
284286
}
285287

286288
#[test]
287289
fn test_split_off_unicode() {
288290
let mut nihon = String::from("日本語");
291+
let orig_capacity = nihon.capacity();
289292
let go = nihon.split_off("日本".len());
290293
assert_eq!(nihon, "日本");
291294
assert_eq!(go, "語");
295+
assert_eq!(nihon.capacity(), orig_capacity);
292296
}
293297

294298
#[test]

library/alloc/tests/vec.rs

+14
Original file line numberDiff line numberDiff line change
@@ -772,9 +772,23 @@ fn test_append() {
772772
#[test]
773773
fn test_split_off() {
774774
let mut vec = vec![1, 2, 3, 4, 5, 6];
775+
let orig_capacity = vec.capacity();
775776
let vec2 = vec.split_off(4);
776777
assert_eq!(vec, [1, 2, 3, 4]);
777778
assert_eq!(vec2, [5, 6]);
779+
assert_eq!(vec.capacity(), orig_capacity);
780+
}
781+
782+
#[test]
783+
fn test_split_off_take_all() {
784+
let mut vec = vec![1, 2, 3, 4, 5, 6];
785+
let orig_ptr = vec.as_ptr();
786+
let orig_capacity = vec.capacity();
787+
let vec2 = vec.split_off(0);
788+
assert_eq!(vec, []);
789+
assert_eq!(vec2, [1, 2, 3, 4, 5, 6]);
790+
assert_eq!(vec.capacity(), orig_capacity);
791+
assert_eq!(vec2.as_ptr(), orig_ptr);
778792
}
779793

780794
#[test]

0 commit comments

Comments
 (0)