mirror of
https://github.com/servo/servo.git
synced 2025-08-04 13:10:20 +01:00
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`.
This commit is contained in:
parent
e4f241389e
commit
d2be5239f5
12 changed files with 356 additions and 303 deletions
|
@ -11,7 +11,7 @@ use cssparser::{DeclarationListParser, DeclarationParser, parse_one_rule};
|
|||
use error_reporting::NullReporter;
|
||||
use parser::{PARSING_MODE_DEFAULT, ParserContext, log_css_error};
|
||||
use properties::{Importance, PropertyDeclaration, PropertyDeclarationBlock, PropertyId};
|
||||
use properties::{PropertyDeclarationId, LonghandId, ParsedDeclaration};
|
||||
use properties::{PropertyDeclarationId, LonghandId, SourcePropertyDeclaration};
|
||||
use properties::LonghandIdSet;
|
||||
use properties::animated_properties::TransitionProperty;
|
||||
use properties::longhands::transition_timing_function::single_value::SpecifiedValue as SpecifiedTimingFunction;
|
||||
|
@ -142,9 +142,11 @@ impl Keyframe {
|
|||
parent_stylesheet.quirks_mode);
|
||||
let mut input = Parser::new(css);
|
||||
|
||||
let mut declarations = SourcePropertyDeclaration::new();
|
||||
let mut rule_parser = KeyframeListParser {
|
||||
context: &context,
|
||||
shared_lock: &parent_stylesheet.shared_lock,
|
||||
declarations: &mut declarations,
|
||||
};
|
||||
parse_one_rule(&mut input, &mut rule_parser)
|
||||
}
|
||||
|
@ -345,14 +347,17 @@ impl KeyframesAnimation {
|
|||
struct KeyframeListParser<'a> {
|
||||
context: &'a ParserContext<'a>,
|
||||
shared_lock: &'a SharedRwLock,
|
||||
declarations: &'a mut SourcePropertyDeclaration,
|
||||
}
|
||||
|
||||
/// Parses a keyframe list from CSS input.
|
||||
pub fn parse_keyframe_list(context: &ParserContext, input: &mut Parser, shared_lock: &SharedRwLock)
|
||||
-> Vec<Arc<Locked<Keyframe>>> {
|
||||
let mut declarations = SourcePropertyDeclaration::new();
|
||||
RuleListParser::new_for_nested_rule(input, KeyframeListParser {
|
||||
context: context,
|
||||
shared_lock: shared_lock,
|
||||
declarations: &mut declarations,
|
||||
}).filter_map(Result::ok).collect()
|
||||
}
|
||||
|
||||
|
@ -383,13 +388,17 @@ impl<'a> QualifiedRuleParser for KeyframeListParser<'a> {
|
|||
let context = ParserContext::new_with_rule_type(self.context, Some(CssRuleType::Keyframe));
|
||||
let parser = KeyframeDeclarationParser {
|
||||
context: &context,
|
||||
declarations: self.declarations,
|
||||
};
|
||||
let mut iter = DeclarationListParser::new(input, parser);
|
||||
let mut block = PropertyDeclarationBlock::new();
|
||||
while let Some(declaration) = iter.next() {
|
||||
match declaration {
|
||||
Ok(parsed) => parsed.expand_push_into(&mut block, Importance::Normal),
|
||||
Ok(()) => {
|
||||
block.extend(iter.parser.declarations.drain(), Importance::Normal);
|
||||
}
|
||||
Err(range) => {
|
||||
iter.parser.declarations.clear();
|
||||
let pos = range.start;
|
||||
let message = format!("Unsupported keyframe property declaration: '{}'",
|
||||
iter.input.slice(range));
|
||||
|
@ -407,27 +416,24 @@ impl<'a> QualifiedRuleParser for KeyframeListParser<'a> {
|
|||
|
||||
struct KeyframeDeclarationParser<'a, 'b: 'a> {
|
||||
context: &'a ParserContext<'b>,
|
||||
declarations: &'a mut SourcePropertyDeclaration,
|
||||
}
|
||||
|
||||
/// Default methods reject all at rules.
|
||||
impl<'a, 'b> AtRuleParser for KeyframeDeclarationParser<'a, 'b> {
|
||||
type Prelude = ();
|
||||
type AtRule = ParsedDeclaration;
|
||||
type AtRule = ();
|
||||
}
|
||||
|
||||
impl<'a, 'b> DeclarationParser for KeyframeDeclarationParser<'a, 'b> {
|
||||
type Declaration = ParsedDeclaration;
|
||||
type Declaration = ();
|
||||
|
||||
fn parse_value(&mut self, name: &str, input: &mut Parser) -> Result<ParsedDeclaration, ()> {
|
||||
fn parse_value(&mut self, name: &str, input: &mut Parser) -> Result<(), ()> {
|
||||
let id = try!(PropertyId::parse(name.into()));
|
||||
match ParsedDeclaration::parse(id, self.context, input) {
|
||||
Ok(parsed) => {
|
||||
match PropertyDeclaration::parse_into(self.declarations, id, self.context, input) {
|
||||
Ok(()) => {
|
||||
// In case there is still unparsed text in the declaration, we should roll back.
|
||||
if !input.is_exhausted() {
|
||||
Err(())
|
||||
} else {
|
||||
Ok(parsed)
|
||||
}
|
||||
input.expect_exhausted()
|
||||
}
|
||||
Err(_) => Err(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue