Skip to content

Commit

Permalink
First specialization
Browse files Browse the repository at this point in the history
I added a new `specialization` feature that will specialize
`From<&[_: Copy]>` to use `from_slice`, which offers a nice
performance boost.

Alas, I could not get any measurable perf improvement on
`insert_many` or `extend`, so I'll leave them out for now.
  • Loading branch information
llogiq committed Aug 28, 2018
1 parent 65f72d1 commit 49c43d0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -14,6 +14,7 @@ documentation = "http://doc.servo.org/smallvec/"
std = []
union = []
default = ["std"]
specialization = []

[lib]
name = "smallvec"
Expand Down
30 changes: 30 additions & 0 deletions lib.rs
Expand Up @@ -31,6 +31,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(alloc))]
#![cfg_attr(feature = "union", feature(untagged_unions))]
#![cfg_attr(feature = "specialization", feature(specialization))]
#![deny(missing_docs)]


Expand Down Expand Up @@ -1156,11 +1157,40 @@ where A::Item: Deserialize<'de>,
}
}


#[cfg(feature = "specialization")]
trait SpecFrom<A: Array, S> {
fn spec_from(slice: S) -> SmallVec<A>;
}

#[cfg(feature = "specialization")]
impl<'a, A: Array> SpecFrom<A, &'a [A::Item]> for SmallVec<A> where A::Item: Clone {
#[inline]
default fn spec_from(slice: &'a [A::Item]) -> SmallVec<A> {
slice.into_iter().cloned().collect()
}
}

#[cfg(feature = "specialization")]
impl<'a, A: Array> SpecFrom<A, &'a [A::Item]> for SmallVec<A> where A::Item: Copy {
#[inline]
fn spec_from(slice: &'a [A::Item]) -> SmallVec<A> {
SmallVec::from_slice(slice)
}
}

impl<'a, A: Array> From<&'a [A::Item]> for SmallVec<A> where A::Item: Clone {
#[cfg(not(feature = "specialization"))]
#[inline]
fn from(slice: &'a [A::Item]) -> SmallVec<A> {
slice.into_iter().cloned().collect()
}

#[cfg(feature = "specialization")]
#[inline]
fn from(slice: &'a [A::Item]) -> SmallVec<A> {
SmallVec::spec_from(slice)
}
}

impl<A: Array> From<Vec<A::Item>> for SmallVec<A> {
Expand Down

0 comments on commit 49c43d0

Please sign in to comment.