diff options
author | Simon Sapin <simon.sapin@exyr.org> | 2017-05-19 17:46:34 +0200 |
---|---|---|
committer | Simon Sapin <simon.sapin@exyr.org> | 2017-05-19 18:53:25 +0200 |
commit | d2be5239f59a051cf25d267266cbecdf8aca09d6 (patch) | |
tree | 624066d9c42b13a74989bd8405291993f40e76f7 /components/script/dom | |
parent | e4f241389e3ef0f397acdeed5bf0b2ec358e5213 (diff) | |
download | servo-d2be5239f59a051cf25d267266cbecdf8aca09d6.tar.gz servo-d2be5239f59a051cf25d267266cbecdf8aca09d6.zip |
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`.
Diffstat (limited to 'components/script/dom')
-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(()) }) |