Don't use SmallVec<[T; 1]> for computed values with an empty default

This commit is contained in:
Anthony Ramine 2017-06-27 12:00:40 +02:00
parent da9d2001db
commit 813883e1bd
2 changed files with 48 additions and 35 deletions

View file

@ -808,31 +808,37 @@ pub trait RepeatableListAnimatable: Animatable {}
impl RepeatableListAnimatable for LengthOrPercentage {}
impl RepeatableListAnimatable for Either<f32, LengthOrPercentage> {}
impl<T: RepeatableListAnimatable> Animatable for SmallVec<[T; 1]> {
fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64)
-> Result<Self, ()> {
use num_integer::lcm;
let len = lcm(self.len(), other.len());
self.iter().cycle().zip(other.iter().cycle()).take(len).map(|(me, you)| {
me.add_weighted(you, self_portion, other_portion)
}).collect()
}
macro_rules! repeated_vec_impl {
($($ty:ty),*) => {
$(impl<T: RepeatableListAnimatable> Animatable for $ty {
fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64)
-> Result<Self, ()> {
use num_integer::lcm;
let len = lcm(self.len(), other.len());
self.iter().cycle().zip(other.iter().cycle()).take(len).map(|(me, you)| {
me.add_weighted(you, self_portion, other_portion)
}).collect()
}
#[inline]
fn compute_distance(&self, other: &Self) -> Result<f64, ()> {
self.compute_squared_distance(other).map(|sd| sd.sqrt())
}
#[inline]
fn compute_distance(&self, other: &Self) -> Result<f64, ()> {
self.compute_squared_distance(other).map(|sd| sd.sqrt())
}
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<f64, ()> {
use num_integer::lcm;
let len = lcm(self.len(), other.len());
self.iter().cycle().zip(other.iter().cycle()).take(len).map(|(me, you)| {
me.compute_squared_distance(you)
}).collect::<Result<Vec<_>, _>>().map(|d| d.iter().sum())
}
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<f64, ()> {
use num_integer::lcm;
let len = lcm(self.len(), other.len());
self.iter().cycle().zip(other.iter().cycle()).take(len).map(|(me, you)| {
me.compute_squared_distance(you)
}).sum()
}
})*
};
}
repeated_vec_impl!(SmallVec<[T; 1]>, Vec<T>);
/// https://drafts.csswg.org/css-transitions/#animtype-number
impl Animatable for Au {
#[inline]
@ -3053,9 +3059,9 @@ pub struct IntermediateShadow {
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)]
/// Intermediate type for box-shadow list and text-shadow list.
pub struct IntermediateShadowList(pub SmallVec<[IntermediateShadow; 1]>);
pub struct IntermediateShadowList(pub Vec<IntermediateShadow>);
type ShadowList = SmallVec<[Shadow; 1]>;
type ShadowList = Vec<Shadow>;
impl From<IntermediateShadowList> for ShadowList {
fn from(shadow_list: IntermediateShadowList) -> Self {
@ -3172,11 +3178,7 @@ impl Animatable for IntermediateShadowList {
let max_len = cmp::max(self.0.len(), other.0.len());
let mut result = if max_len > 1 {
SmallVec::from_vec(Vec::with_capacity(max_len))
} else {
SmallVec::new()
};
let mut result = Vec::with_capacity(max_len);
for i in 0..max_len {
let shadow = match (self.0.get(i), other.0.get(i)) {
@ -3202,11 +3204,7 @@ impl Animatable for IntermediateShadowList {
fn add(&self, other: &Self) -> Result<Self, ()> {
let len = self.0.len() + other.0.len();
let mut result = if len > 1 {
SmallVec::from_vec(Vec::with_capacity(len))
} else {
SmallVec::new()
};
let mut result = Vec::with_capacity(len);
result.extend(self.0.iter().cloned());
result.extend(other.0.iter().cloned());