Bug 1466645: Avoid useless allocations in custom property name serialization. r?xidorn draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Tue, 05 Jun 2018 15:53:43 +0200
changeset 804321 8dc732da7da4a5fcdd2786376c979e548acc02a3
parent 804278 434eb5596464495f5732d8aa0a6f4a218711e257
child 804322 11efb6ce849c9dd0d38b38e7d8a06742041e724c
push id112345
push userbmo:emilio@crisal.io
push dateTue, 05 Jun 2018 19:10:58 +0000
reviewersxidorn
bugs1466645
milestone62.0a1
Bug 1466645: Avoid useless allocations in custom property name serialization. r?xidorn And make transition-property more correct by serializing --0 unescaped instead of escaped. MozReview-Commit-ID: CCBSe5Frd0d
servo/components/style/properties/properties.mako.rs
servo/components/style/values/mod.rs
servo/components/style/values/specified/box.rs
--- a/servo/components/style/properties/properties.mako.rs
+++ b/servo/components/style/properties/properties.mako.rs
@@ -44,16 +44,17 @@ use selectors::parser::SelectorParseErro
 use shared_lock::StylesheetGuards;
 use style_traits::{CssWriter, KeywordsCollectFn, ParseError, ParsingMode};
 use style_traits::{SpecifiedValueInfo, StyleParseErrorKind, ToCss};
 use stylesheets::{CssRuleType, Origin, UrlExtraData};
 #[cfg(feature = "servo")] use values::Either;
 use values::generics::text::LineHeight;
 use values::computed;
 use values::computed::NonNegativeLength;
+use values::serialize_atom_name;
 use rule_tree::{CascadeLevel, StrongRuleNode};
 use self::computed_value_flags::*;
 use str::{CssString, CssStringBorrow, CssStringWriter};
 use style_adjuster::StyleAdjuster;
 
 pub use self::declaration_block::*;
 
 <%!
@@ -1501,18 +1502,19 @@ pub enum PropertyDeclarationId<'a> {
 
 impl<'a> ToCss for PropertyDeclarationId<'a> {
     fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
     where
         W: Write,
     {
         match *self {
             PropertyDeclarationId::Longhand(id) => dest.write_str(id.name()),
-            PropertyDeclarationId::Custom(_) => {
-                serialize_identifier(&self.name(), dest)
+            PropertyDeclarationId::Custom(ref name) => {
+                dest.write_str("--")?;
+                serialize_atom_name(name, dest)
             }
         }
     }
 }
 
 impl<'a> PropertyDeclarationId<'a> {
     /// Whether a given declaration id is either the same as `other`, or a
     /// longhand of it.
@@ -1582,18 +1584,19 @@ impl ToCss for PropertyId {
     where
         W: Write,
     {
         match *self {
             PropertyId::Longhand(id) => dest.write_str(id.name()),
             PropertyId::Shorthand(id) => dest.write_str(id.name()),
             PropertyId::LonghandAlias(id, _) => dest.write_str(id.name()),
             PropertyId::ShorthandAlias(id, _) => dest.write_str(id.name()),
-            PropertyId::Custom(_) => {
-                serialize_identifier(&self.name(), dest)
+            PropertyId::Custom(ref name) => {
+                dest.write_str("--")?;
+                serialize_atom_name(name, dest)
             }
         }
     }
 }
 
 impl PropertyId {
     /// Return the longhand id that this property id represents.
     #[inline]
--- a/servo/components/style/values/mod.rs
+++ b/servo/components/style/values/mod.rs
@@ -4,17 +4,17 @@
 
 //! Common [values][values] used in CSS.
 //!
 //! [values]: https://drafts.csswg.org/css-values/
 
 #![deny(missing_docs)]
 
 use Atom;
-pub use cssparser::{serialize_identifier, CowRcStr, Parser, SourceLocation, Token, RGBA};
+pub use cssparser::{serialize_identifier, serialize_name, CowRcStr, Parser, SourceLocation, Token, RGBA};
 use parser::{Parse, ParserContext};
 use selectors::parser::SelectorParseErrorKind;
 use std::fmt::{self, Debug, Write};
 use std::hash;
 use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss};
 use values::distance::{ComputeSquaredDistance, SquaredDistance};
 
 #[cfg(feature = "servo")]
@@ -55,16 +55,38 @@ pub fn serialize_atom_identifier<Static,
 ) -> fmt::Result
 where
     Static: ::string_cache::StaticAtomSet,
     W: Write,
 {
     serialize_identifier(&ident, dest)
 }
 
+/// Serialize a name which is represented as an Atom.
+#[cfg(feature = "gecko")]
+pub fn serialize_atom_name<W>(ident: &Atom, dest: &mut W) -> fmt::Result
+where
+    W: Write,
+{
+    ident.with_str(|s| serialize_name(s, dest))
+}
+
+/// Serialize a name which is represented as an Atom.
+#[cfg(feature = "servo")]
+pub fn serialize_atom_name<Static, W>(
+    ident: &::string_cache::Atom<Static>,
+    dest: &mut W,
+) -> fmt::Result
+where
+    Static: ::string_cache::StaticAtomSet,
+    W: Write,
+{
+    serialize_name(&ident, dest)
+}
+
 /// Serialize a normalized value into percentage.
 pub fn serialize_percentage<W>(value: CSSFloat, dest: &mut CssWriter<W>) -> fmt::Result
 where
     W: Write,
 {
     (value * 100.).to_css(dest)?;
     dest.write_str("%")
 }
--- a/servo/components/style/values/specified/box.rs
+++ b/servo/components/style/values/specified/box.rs
@@ -751,23 +751,23 @@ pub enum TransitionProperty {
     Unsupported(CustomIdent),
 }
 
 impl ToCss for TransitionProperty {
     fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
     where
         W: Write,
     {
-        use values::serialize_atom_identifier;
+        use values::serialize_atom_name;
         match *self {
             TransitionProperty::Shorthand(ref s) => s.to_css(dest),
             TransitionProperty::Longhand(ref l) => l.to_css(dest),
             TransitionProperty::Custom(ref name) => {
                 dest.write_str("--")?;
-                serialize_atom_identifier(name, dest)
+                serialize_atom_name(name, dest)
             }
             TransitionProperty::Unsupported(ref i) => i.to_css(dest),
         }
     }
 }
 
 impl Parse for TransitionProperty {
     fn parse<'i, 't>(