From 49c43d0fa4df549f290f1fea86447dd85b485f07 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Mon, 27 Aug 2018 08:34:24 +0200 Subject: [PATCH] First specialization 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. --- Cargo.toml | 1 + lib.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 133b7e2..3bf75ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ documentation = "http://doc.servo.org/smallvec/" std = [] union = [] default = ["std"] +specialization = [] [lib] name = "smallvec" diff --git a/lib.rs b/lib.rs index bb8457c..959bbb1 100644 --- a/lib.rs +++ b/lib.rs @@ -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)] @@ -1156,11 +1157,40 @@ where A::Item: Deserialize<'de>, } } + +#[cfg(feature = "specialization")] +trait SpecFrom { + fn spec_from(slice: S) -> SmallVec; +} + +#[cfg(feature = "specialization")] +impl<'a, A: Array> SpecFrom for SmallVec where A::Item: Clone { + #[inline] + default fn spec_from(slice: &'a [A::Item]) -> SmallVec { + slice.into_iter().cloned().collect() + } +} + +#[cfg(feature = "specialization")] +impl<'a, A: Array> SpecFrom for SmallVec where A::Item: Copy { + #[inline] + fn spec_from(slice: &'a [A::Item]) -> SmallVec { + SmallVec::from_slice(slice) + } +} + impl<'a, A: Array> From<&'a [A::Item]> for SmallVec where A::Item: Clone { + #[cfg(not(feature = "specialization"))] #[inline] fn from(slice: &'a [A::Item]) -> SmallVec { slice.into_iter().cloned().collect() } + + #[cfg(feature = "specialization")] + #[inline] + fn from(slice: &'a [A::Item]) -> SmallVec { + SmallVec::spec_from(slice) + } } impl From> for SmallVec {