From 0c5681140a5b44ae5481faf2d7353c4e7a272d50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sun, 30 Apr 2017 12:26:37 +0200 Subject: [PATCH] style: Use SmallVec in the computed value representation of most stuff. --- Cargo.lock | 19 ++++---- components/style/Cargo.toml | 4 +- components/style/parser.rs | 2 - components/style/properties/helpers.mako.rs | 28 ++++++----- .../helpers/animated_properties.mako.rs | 14 ++++-- .../style/properties/longhand/border.mako.rs | 48 +++++++++---------- .../longhand/inherited_text.mako.rs | 6 ++- .../properties/shorthand/background.mako.rs | 2 +- components/style/values/computed/mod.rs | 2 +- 9 files changed, 68 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5e49cf1ea4a..51a2eafe707 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -987,7 +987,7 @@ dependencies = [ "servo_geometry 0.0.1", "servo_url 0.0.1", "simd 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "style 0.0.1", "style_traits 0.0.1", "time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1385,7 +1385,7 @@ dependencies = [ "servo_config 0.0.1", "servo_geometry 0.0.1", "servo_url 0.0.1", - "smallvec 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "style 0.0.1", "style_traits 0.0.1", "unicode-bidi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1966,7 +1966,7 @@ dependencies = [ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2293,7 +2293,7 @@ dependencies = [ "servo_geometry 0.0.1", "servo_rand 0.0.1", "servo_url 0.0.1", - "smallvec 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "style 0.0.1", "style_traits 0.0.1", "time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2389,7 +2389,7 @@ dependencies = [ "fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "precomputed-hash 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2685,8 +2685,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "smallvec" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "heapsize 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "string_cache" @@ -2762,7 +2765,7 @@ dependencies = [ "servo_atoms 0.0.1", "servo_config 0.0.1", "servo_url 0.0.1", - "smallvec 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "style_traits 0.0.1", "time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3520,7 +3523,7 @@ dependencies = [ "checksum simd 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a94d14a2ae1f1f110937de5fb69e494372560181c7e1739a097fcc2cee37ba0" "checksum siphasher 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0df90a788073e8d0235a67e50441d47db7c8ad9debd91cbf43736a2a92d36537" "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" -"checksum smallvec 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dca03f2f42500a9ef8ac0d16183dff8bed40e3dcf98f9d4147928548d5c4236e" +"checksum smallvec 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4f8266519bc1d17d0b5b16f6c21295625d562841c708f6376f49028a43e9c11e" "checksum string_cache 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f55fba06c5e294108f22e8512eb598cb13388a117991e411a8df8f41a1219a75" "checksum string_cache_codegen 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "479cde50c3539481f33906a387f2bd17c8e87cb848c35b6021d41fb81ff9b4d7" "checksum string_cache_shared 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b1884d1bc09741d466d9b14e6d37ac89d6909cbcac41dd9ae982d4d063bbedfc" diff --git a/components/style/Cargo.toml b/components/style/Cargo.toml index 54f476b0e73..3f1aa94f750 100644 --- a/components/style/Cargo.toml +++ b/components/style/Cargo.toml @@ -17,7 +17,7 @@ gecko = ["nsstring_vendor", "rayon/unstable", "num_cpus"] use_bindgen = ["bindgen", "regex"] servo = ["serde/unstable", "serde", "serde_derive", "heapsize", "heapsize_derive", "style_traits/servo", "servo_atoms", "servo_config", "html5ever-atoms", - "cssparser/heapsize", "cssparser/serde", "encoding", + "cssparser/heapsize", "cssparser/serde", "encoding", "smallvec/heapsizeof", "rayon/unstable", "servo_url"] testing = [] gecko_debug = ["nsstring_vendor/gecko_debug"] @@ -53,7 +53,7 @@ serde = {version = "0.9", optional = true} serde_derive = {version = "0.9", optional = true} servo_atoms = {path = "../atoms", optional = true} servo_config = {path = "../config", optional = true} -smallvec = "0.3" +smallvec = "0.3.3" style_traits = {path = "../style_traits"} servo_url = {path = "../url", optional = true} time = "0.1" diff --git a/components/style/parser.rs b/components/style/parser.rs index bac1252228a..f437d8ba22c 100644 --- a/components/style/parser.rs +++ b/components/style/parser.rs @@ -4,8 +4,6 @@ //! The context within which CSS code is parsed. -#![deny(missing_docs)] - use context::QuirksMode; use cssparser::{Parser, SourcePosition, UnicodeRange}; use error_reporting::ParseErrorReporter; diff --git a/components/style/properties/helpers.mako.rs b/components/style/properties/helpers.mako.rs index 478e1be659b..472bf1c0a93 100644 --- a/components/style/properties/helpers.mako.rs +++ b/components/style/properties/helpers.mako.rs @@ -15,6 +15,7 @@ use app_units::Au; use cssparser::{Color as CSSParserColor, RGBA}; use values::specified::AllowQuirks; + use smallvec::SmallVec; pub use values::specified::${type} as SpecifiedValue; pub mod computed_value { % if computed_type: @@ -75,6 +76,7 @@ delegate_animate=False, space_separated_allowed=False, **kwargs)"> <%call expr="longhand(name, **kwargs)"> % if not gecko_only: + use smallvec::SmallVec; use std::fmt; use values::HasViewportPercentage; use style_traits::ToCss; @@ -100,10 +102,11 @@ pub mod computed_value { pub use super::single_value::computed_value as single_value; pub use self::single_value::T as SingleComputedValue; + use smallvec::SmallVec; /// The computed value, effectively a list of single values. #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] - pub struct T(pub Vec); + pub struct T(pub SmallVec<[single_value::T; 1]>); % if delegate_animate: use properties::animated_properties::Interpolate; @@ -179,9 +182,11 @@ pub fn get_initial_value() -> computed_value::T { % if allow_empty: - computed_value::T(vec![]) + computed_value::T(SmallVec::new()) % else: - computed_value::T(vec![single_value::get_initial_value()]) + let mut v = SmallVec::new(); + v.push(single_value::get_initial_value()); + computed_value::T(v) % endif } @@ -193,19 +198,16 @@ if space_separated_allowed: parse_func = "parse_space_or_comma_separated" %> + % if allow_empty: if input.try(|input| input.expect_ident_matching("none")).is_ok() { - Ok(SpecifiedValue(Vec::new())) - } else { - ${parse_func}(input, |parser| { - single_value::parse(context, parser) - }).map(SpecifiedValue) + return Ok(SpecifiedValue(Vec::new())) } - % else: - ${parse_func}(input, |parser| { - single_value::parse(context, parser) - }).map(SpecifiedValue) % endif + + ${parse_func}(input, |parser| { + single_value::parse(context, parser) + }).map(SpecifiedValue) } pub use self::single_value::SpecifiedValue as SingleSpecifiedValue; @@ -220,7 +222,7 @@ #[inline] fn from_computed_value(computed: &computed_value::T) -> Self { SpecifiedValue(computed.0.iter() - .map(|x| ToComputedValue::from_computed_value(x)) + .map(ToComputedValue::from_computed_value) .collect()) } } diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index d11f9bd271e..f2da326e048 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -27,6 +27,7 @@ use properties::longhands::vertical_align::computed_value::T as VerticalAlign; use properties::longhands::visibility::computed_value::T as Visibility; #[cfg(feature = "gecko")] use properties::{PropertyDeclarationId, LonghandId}; #[cfg(feature = "servo")] use servo_atoms::Atom; +use smallvec::SmallVec; use std::cmp; #[cfg(feature = "gecko")] use std::collections::HashMap; use std::fmt; @@ -612,7 +613,7 @@ pub trait Interpolate: Sized { /// https://drafts.csswg.org/css-transitions/#animtype-repeatable-list pub trait RepeatableListInterpolate: Interpolate {} -impl Interpolate for Vec { +impl Interpolate for SmallVec<[T; 1]> { fn interpolate(&self, other: &Self, progress: f64) -> Result { use num_integer::lcm; let len = lcm(self.len(), other.len()); @@ -1065,7 +1066,12 @@ impl Interpolate for ClipRect { }; let max_len = cmp::max(self.0.len(), other.0.len()); - let mut result = Vec::with_capacity(max_len); + + let mut result = if max_len > 1 { + SmallVec::from_vec(Vec::with_capacity(max_len)) + } else { + SmallVec::new() + }; for i in 0..max_len { let shadow = match (self.0.get(i), other.0.get(i)) { @@ -2192,7 +2198,7 @@ pub trait ComputeDistance: Sized { } } -impl ComputeDistance for Vec { +impl ComputeDistance for SmallVec<[T; 1]> { #[inline] fn compute_distance(&self, other: &Self) -> Result { self.compute_squared_distance(other).map(|sd| sd.sqrt()) @@ -2848,7 +2854,7 @@ impl <'a> From<<&'a IntermediateColor> for CSSParserColor { #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[allow(missing_docs)] /// Intermediate type for box-shadow list and text-shadow list. - pub struct Intermediate${type}ShadowList(pub Vec); + pub struct Intermediate${type}ShadowList(pub SmallVec<[Intermediate${type}Shadow; 1]>); impl <'a> From<<&'a Intermediate${type}ShadowList> for ${type}ShadowList { fn from(shadow_list: &Intermediate${type}ShadowList) -> ${type}ShadowList { diff --git a/components/style/properties/longhand/border.mako.rs b/components/style/properties/longhand/border.mako.rs index a540b515a82..b43363edf48 100644 --- a/components/style/properties/longhand/border.mako.rs +++ b/components/style/properties/longhand/border.mako.rs @@ -145,7 +145,7 @@ ${helpers.gecko_keyword_conversion(Keyword('border-style', match *computed { computed_value::T(Some(ref vec)) => { SpecifiedValue::Colors(vec.iter() - .map(|c| ToComputedValue::from_computed_value((c))) + .map(ToComputedValue::from_computed_value) .collect()) }, computed_value::T(None) => { @@ -675,7 +675,7 @@ ${helpers.single_keyword("-moz-float-edge", "content-box margin-box", #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub struct T { - pub corners: Vec, + pub corners: [NumberOrPercentage; 4], pub fill: bool, } } @@ -721,10 +721,10 @@ ${helpers.single_keyword("-moz-float-edge", "content-box margin-box", #[inline] pub fn get_initial_value() -> computed_value::T { computed_value::T { - corners: vec![ComputedNumberOrPercentage::Percentage(Percentage(1.0)), - ComputedNumberOrPercentage::Percentage(Percentage(1.0)), - ComputedNumberOrPercentage::Percentage(Percentage(1.0)), - ComputedNumberOrPercentage::Percentage(Percentage(1.0))], + corners: [ComputedNumberOrPercentage::Percentage(Percentage(1.0)), + ComputedNumberOrPercentage::Percentage(Percentage(1.0)), + ComputedNumberOrPercentage::Percentage(Percentage(1.0)), + ComputedNumberOrPercentage::Percentage(Percentage(1.0))], fill: false, } } @@ -744,22 +744,22 @@ ${helpers.single_keyword("-moz-float-edge", "content-box margin-box", fn to_computed_value(&self, context: &Context) -> computed_value::T { let length = self.corners.len(); let corners = match length { - 4 => vec![self.corners[0].to_computed_value(context), - self.corners[1].to_computed_value(context), - self.corners[2].to_computed_value(context), - self.corners[3].to_computed_value(context)], - 3 => vec![self.corners[0].to_computed_value(context), - self.corners[1].to_computed_value(context), - self.corners[2].to_computed_value(context), - self.corners[1].to_computed_value(context)], - 2 => vec![self.corners[0].to_computed_value(context), - self.corners[1].to_computed_value(context), - self.corners[0].to_computed_value(context), - self.corners[1].to_computed_value(context)], - 1 => vec![self.corners[0].to_computed_value(context), - self.corners[0].to_computed_value(context), - self.corners[0].to_computed_value(context), - self.corners[0].to_computed_value(context)], + 4 => [self.corners[0].to_computed_value(context), + self.corners[1].to_computed_value(context), + self.corners[2].to_computed_value(context), + self.corners[3].to_computed_value(context)], + 3 => [self.corners[0].to_computed_value(context), + self.corners[1].to_computed_value(context), + self.corners[2].to_computed_value(context), + self.corners[1].to_computed_value(context)], + 2 => [self.corners[0].to_computed_value(context), + self.corners[1].to_computed_value(context), + self.corners[0].to_computed_value(context), + self.corners[1].to_computed_value(context)], + 1 => [self.corners[0].to_computed_value(context), + self.corners[0].to_computed_value(context), + self.corners[0].to_computed_value(context), + self.corners[0].to_computed_value(context)], _ => unreachable!(), }; computed_value::T { @@ -791,11 +791,11 @@ ${helpers.single_keyword("-moz-float-edge", "content-box margin-box", } } - if fill == false { + if !fill { fill = input.try(|input| input.expect_ident_matching("fill")).is_ok(); } - if values.len() > 0 { + if !values.is_empty() { Ok(SpecifiedValue { corners: values, fill: fill diff --git a/components/style/properties/longhand/inherited_text.mako.rs b/components/style/properties/longhand/inherited_text.mako.rs index c2e85e43b4b..c6757a2df53 100644 --- a/components/style/properties/longhand/inherited_text.mako.rs +++ b/components/style/properties/longhand/inherited_text.mako.rs @@ -753,10 +753,11 @@ ${helpers.single_keyword("text-align-last", pub mod computed_value { use app_units::Au; use cssparser::Color; + use smallvec::SmallVec; #[derive(Clone, PartialEq, Debug)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] - pub struct T(pub Vec); + pub struct T(pub SmallVec<[TextShadow; 1]>); #[derive(Clone, PartialEq, Debug)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] @@ -833,7 +834,8 @@ ${helpers.single_keyword("text-align-last", #[inline] pub fn get_initial_value() -> computed_value::T { - computed_value::T(Vec::new()) + use smallvec::SmallVec; + computed_value::T(SmallVec::new()) } pub fn parse(context: &ParserContext, input: &mut Parser) -> Result { diff --git a/components/style/properties/shorthand/background.mako.rs b/components/style/properties/shorthand/background.mako.rs index ab19507702f..21a75db2693 100644 --- a/components/style/properties/shorthand/background.mako.rs +++ b/components/style/properties/shorthand/background.mako.rs @@ -214,7 +214,7 @@ } Ok(()) })); - if any == false { + if !any { return Err(()); } diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index 7cfdfa77716..bf3685f7eca 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -332,7 +332,7 @@ pub struct Shadow { /// A `` value. pub type Number = CSSFloat; -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[allow(missing_docs)] pub enum NumberOrPercentage {