Skip to content

Commit

Permalink
Add liballoc impl SpecFromElem for i8
Browse files Browse the repository at this point in the history
Speedup vec![1_i8; N] for non-zero element.

Before
test do_bench_from_elem_i8        ... bench:         130 ns/iter (+/- 7) = 61 MB/s
test do_bench_from_elem_u8        ... bench:         121 ns/iter (+/- 4) = 66 MB/s
After
test do_bench_from_elem_i8        ... bench:         123 ns/iter (+/- 7) = 65 MB/s
test do_bench_from_elem_u8        ... bench:         121 ns/iter (+/- 5) = 66 MB/s

No speed difference if element is already zero.

    #[bench]
    fn do_bench_from_elem_i8(b: &mut Bencher) {
        b.bytes = 8 as u64;
        b.iter(|| {
            let dst = ve::vec![10_i8; 100];
            assert_eq!(dst.len(), 100);
            assert!(dst.iter().all(|x| *x == 10));
        })
    }

As suggested by @cuviper
https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers
  • Loading branch information
pickfire committed Jun 20, 2020
1 parent 2d8bd9b commit f66bcc5
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,21 @@ impl<T: Clone> SpecFromElem for T {
}
}

impl SpecFromElem for i8 {
#[inline]
fn from_elem(elem: i8, n: usize) -> Vec<i8> {
if elem == 0 {
return Vec { buf: RawVec::with_capacity_zeroed(n), len: n };
}
unsafe {
let mut v = Vec::with_capacity(n);
ptr::write_bytes(v.as_mut_ptr(), elem as u8, n);
v.set_len(n);
v
}
}
}

impl SpecFromElem for u8 {
#[inline]
fn from_elem(elem: u8, n: usize) -> Vec<u8> {
Expand Down Expand Up @@ -1847,7 +1862,6 @@ macro_rules! impl_is_zero {
};
}

impl_is_zero!(i8, |x| x == 0);
impl_is_zero!(i16, |x| x == 0);
impl_is_zero!(i32, |x| x == 0);
impl_is_zero!(i64, |x| x == 0);
Expand Down

0 comments on commit f66bcc5

Please sign in to comment.