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

@ -83,6 +83,7 @@
delegate_animate=False, separator='Comma', **kwargs)">
<%call expr="longhand(name, vector=True, **kwargs)">
% if not gecko_only:
#[allow(unused_imports)]
use smallvec::SmallVec;
use std::fmt;
#[allow(unused_imports)]
@ -113,13 +114,23 @@
pub mod computed_value {
pub use super::single_value::computed_value as single_value;
pub use self::single_value::T as SingleComputedValue;
% if allow_empty and allow_empty != "NotInitial":
use std::vec::IntoIter;
% else:
use smallvec::{IntoIter, SmallVec};
% endif
use values::computed::ComputedVecIter;
/// The computed value, effectively a list of single values.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct T(pub SmallVec<[single_value::T; 1]>);
pub struct T(
% if allow_empty and allow_empty != "NotInitial":
pub Vec<single_value::T>,
% else:
pub SmallVec<[single_value::T; 1]>,
% endif
);
% if delegate_animate:
use properties::animated_properties::Animatable;
@ -149,7 +160,11 @@
impl IntoIterator for T {
type Item = single_value::T;
% if allow_empty and allow_empty != "NotInitial":
type IntoIter = IntoIter<single_value::T>;
% else:
type IntoIter = IntoIter<[single_value::T; 1]>;
% endif
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
@ -207,7 +222,7 @@
pub fn get_initial_value() -> computed_value::T {
% if allow_empty and allow_empty != "NotInitial":
computed_value::T(SmallVec::new())
computed_value::T(vec![])
% else:
let mut v = SmallVec::new();
v.push(single_value::get_initial_value());

View file

@ -808,7 +808,9 @@ pub trait RepeatableListAnimatable: Animatable {}
impl RepeatableListAnimatable for LengthOrPercentage {}
impl RepeatableListAnimatable for Either<f32, LengthOrPercentage> {}
impl<T: RepeatableListAnimatable> Animatable for SmallVec<[T; 1]> {
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;
@ -829,10 +831,14 @@ impl<T: RepeatableListAnimatable> Animatable for SmallVec<[T; 1]> {
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())
}).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());