Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds "bench_from_slice(_vec)(_small)" benches, renaming the previous versions to "bench_from_iter*" (because they were using `From<&[T]>`, which calls `FromIterator` internally) and optimizes the `from_slice` constructor considerably. Before: ``` test bench_from_slice ... bench: 42 ns/iter (+/- 0) test bench_from_slice_small ... bench: 12 ns/iter (+/- 0) ``` After: ``` test bench_from_slice ... bench: 27 ns/iter (+/- 0) test bench_from_slice_small ... bench: 8 ns/iter (+/- 0) ```
- Loading branch information
Showing
2 changed files
with
41 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -946,9 +946,25 @@ impl<A: Array> SmallVec<A> where A::Item: Copy { | |
/// | ||
/// For slices of `Copy` types, this is more efficient than `SmallVec::from(slice)`. | ||
pub fn from_slice(slice: &[A::Item]) -> Self { | ||
let mut vec = Self::new(); | ||
vec.extend_from_slice(slice); | ||
vec | ||
let len = slice.len(); | ||
if len <= A::size() { | ||
SmallVec { | ||
capacity: len, | ||
data: SmallVecData::from_inline(unsafe { | ||
let mut data: A = mem::uninitialized(); | ||
ptr::copy_nonoverlapping(slice.as_ptr(), data.ptr_mut(), len); | ||
data | ||
}) | ||
} | ||
} else { | ||
let mut b = slice.to_vec(); | ||
let ptr = b.as_mut_ptr(); | ||
mem::forget(b); | ||
SmallVec { | ||
capacity: len, | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
llogiq
Author
Contributor
|
||
data: SmallVecData::from_heap(ptr, len), | ||
} | ||
} | ||
} | ||
|
||
/// Copy elements from a slice into the vector at position `index`, shifting any following | ||
|
Should this be
b.capacity()
? I'm gessingto_vec
may over allocate.