-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
By using `repeat_n` instead of `ExtendElement`, we can delete a bunch more `unsafe` code from `Vec. All the codegen tests are passing (and I made one stricter to help be sure), so let's see if perf is also happy with it.
- Loading branch information
Showing
3 changed files
with
37 additions
and
104 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 |
---|---|---|
@@ -1,61 +1,35 @@ | ||
use core::ptr; | ||
use core::iter; | ||
|
||
use crate::alloc::Allocator; | ||
use crate::raw_vec::RawVec; | ||
|
||
use super::{ExtendElement, IsZero, Vec}; | ||
use super::{IsZero, Vec}; | ||
|
||
// Specialization trait used for Vec::from_elem | ||
pub(super) trait SpecFromElem: Sized { | ||
fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A>; | ||
} | ||
|
||
#[inline] | ||
fn basic_from_elem<T: Clone, A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> { | ||
let mut v = Vec::with_capacity_in(n, alloc); | ||
v.extend(iter::repeat_n(elem, n)); | ||
v | ||
} | ||
|
||
impl<T: Clone> SpecFromElem for T { | ||
#[inline] | ||
default fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A> { | ||
let mut v = Vec::with_capacity_in(n, alloc); | ||
v.extend_with(n, ExtendElement(elem)); | ||
v | ||
basic_from_elem(elem, n, alloc) | ||
} | ||
} | ||
|
||
impl<T: Clone + IsZero> SpecFromElem for T { | ||
#[inline] | ||
default fn from_elem<A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> { | ||
fn from_elem<A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> { | ||
if elem.is_zero() { | ||
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; | ||
} | ||
let mut v = Vec::with_capacity_in(n, alloc); | ||
v.extend_with(n, ExtendElement(elem)); | ||
v | ||
} | ||
} | ||
|
||
impl SpecFromElem for i8 { | ||
#[inline] | ||
fn from_elem<A: Allocator>(elem: i8, n: usize, alloc: A) -> Vec<i8, A> { | ||
if elem == 0 { | ||
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; | ||
} | ||
unsafe { | ||
let mut v = Vec::with_capacity_in(n, alloc); | ||
ptr::write_bytes(v.as_mut_ptr(), elem as u8, n); | ||
v.set_len(n); | ||
v | ||
} | ||
} | ||
} | ||
|
||
impl SpecFromElem for u8 { | ||
#[inline] | ||
fn from_elem<A: Allocator>(elem: u8, n: usize, alloc: A) -> Vec<u8, A> { | ||
if elem == 0 { | ||
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; | ||
} | ||
unsafe { | ||
let mut v = Vec::with_capacity_in(n, alloc); | ||
ptr::write_bytes(v.as_mut_ptr(), elem, n); | ||
v.set_len(n); | ||
v | ||
} | ||
basic_from_elem(elem, n, alloc) | ||
} | ||
} |
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