diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2017-05-19 18:37:14 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-19 18:37:14 -0500 |
commit | 60682cf81fe19a82c73dd98ba4c1eebc1dbbfcac (patch) | |
tree | 9004384638efc214b00b9024c26cfa454e764c46 /components/script | |
parent | d1e31f7aa46b3b230194de805fff38afcaf9eb68 (diff) | |
parent | 341fc54313aa35980119868cb90854a91c1c6e63 (diff) | |
download | servo-60682cf81fe19a82c73dd98ba4c1eebc1dbbfcac.tar.gz servo-60682cf81fe19a82c73dd98ba4c1eebc1dbbfcac.zip |
Auto merge of #16954 - servo:arrayvec, r=emilio
Avoid returning / passing around a huge ParsedDeclaration type
This enum type used to contain the result of parsing one CSS source declaration (`name: value;`) and expanding shorthands. Enum types are as big as the biggest of their variant (plus discriminant), which was quite big because some shorthands expand to many longhand properties. This type was returned through many functions and methods, wrapped and rewrapped in `Result` with different error types. This presumably caused significant `memmove` traffic.
Instead, we now allocate an `ArrayVec` on the stack and pass `&mut` references to it for various functions to push into it. This type is also very big, but we never move it.
We still use an intermediate data structure because we sometimes decide after shorthand expansion that a declaration is invalid after all and that we’re gonna drop it. Only later do we push to a `PropertyDeclarationBlock`, with an entire `ArrayVec` or nothing.
In future work we can try to avoid a large stack-allocated array, and instead writing directly to the heap allocation of the `Vec` inside `PropertyDeclarationBlock`. However this is tricky: we need to preserve this "all or nothing" aspect of parsing one source declaration, and at the same time we want to make it as little error-prone as possible for the various call sites. `PropertyDeclarationBlock` curently does property deduplication incrementally: as each `PropertyDeclaration` is pushed, we check if an existing declaration of the same property exists and if so overwrite it. To get rid of the stack allocated array we’d need to somehow deduplicate separately after pushing multiple `PropertyDeclaration`.
<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/16954)
<!-- Reviewable:end -->
Diffstat (limited to 'components/script')
-rw-r--r-- | components/script/dom/cssstyledeclaration.rs | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/components/script/dom/cssstyledeclaration.rs b/components/script/dom/cssstyledeclaration.rs index 4d7d8feaac5..ecf4b35e61c 100644 --- a/components/script/dom/cssstyledeclaration.rs +++ b/components/script/dom/cssstyledeclaration.rs @@ -19,7 +19,7 @@ use std::ascii::AsciiExt; use style::attr::AttrValue; use style::parser::PARSING_MODE_DEFAULT; use style::properties::{Importance, PropertyDeclarationBlock, PropertyId, LonghandId, ShorthandId}; -use style::properties::{parse_one_declaration, parse_style_attribute}; +use style::properties::{parse_one_declaration_into, parse_style_attribute, SourcePropertyDeclaration}; use style::selector_parser::PseudoElement; use style::shared_lock::Locked; use style::stylearc::Arc; @@ -239,12 +239,12 @@ impl CSSStyleDeclaration { self.owner.mutate_associated_block(|ref mut pdb, mut changed| { if value.is_empty() { - // Step 4 + // Step 3 *changed = pdb.remove_property(&id); return Ok(()); } - // Step 5 + // Step 4 let importance = match &*priority { "" => Importance::Normal, p if p.eq_ignore_ascii_case("important") => Importance::Important, @@ -254,27 +254,26 @@ impl CSSStyleDeclaration { } }; - // Step 6 + // Step 5 let window = self.owner.window(); let quirks_mode = window.Document().quirks_mode(); - let result = - parse_one_declaration(id, &value, &self.owner.base_url(), - window.css_error_reporter(), - PARSING_MODE_DEFAULT, - quirks_mode); + let mut declarations = SourcePropertyDeclaration::new(); + let result = parse_one_declaration_into( + &mut declarations, id, &value, &self.owner.base_url(), + window.css_error_reporter(), PARSING_MODE_DEFAULT, quirks_mode); - // Step 7 - let parsed = match result { - Ok(parsed) => parsed, + // Step 6 + match result { + Ok(()) => {}, Err(_) => { *changed = false; return Ok(()); } - }; + } + // Step 7 // Step 8 - // Step 9 - *changed = parsed.expand_set_into(pdb, importance); + *changed = pdb.extend_reset(declarations.drain(), importance); Ok(()) }) |