layout: Small vector optimize CSS selector matching

This commit is contained in:
Patrick Walton 2014-01-26 18:19:44 -08:00
parent beb9b9bd92
commit 61b030552e
3 changed files with 36 additions and 25 deletions

View file

@ -10,6 +10,7 @@ use layout::util::LayoutDataAccess;
use layout::wrapper::LayoutNode; use layout::wrapper::LayoutNode;
use extra::arc::Arc; use extra::arc::Arc;
use servo_util::smallvec::SmallVec;
use style::{TNode, Stylist, cascade}; use style::{TNode, Stylist, cascade};
use style::{Before, After}; use style::{Before, After};
@ -33,12 +34,22 @@ impl<'ln> MatchMethods for LayoutNode<'ln> {
let mut layout_data_ref = self.mutate_layout_data(); let mut layout_data_ref = self.mutate_layout_data();
match *layout_data_ref.get() { match *layout_data_ref.get() {
Some(ref mut layout_data) => { Some(ref mut layout_data) => {
layout_data.data.applicable_declarations = stylist.get_applicable_declarations( stylist.get_applicable_declarations(self,
self, style_attribute, None); style_attribute,
layout_data.data.before_applicable_declarations = stylist.get_applicable_declarations( None,
self, None, Some(Before)); &mut layout_data.data.applicable_declarations);
layout_data.data.after_applicable_declarations = stylist.get_applicable_declarations( stylist.get_applicable_declarations(self,
self, None, Some(After)); None,
Some(Before),
&mut layout_data
.data
.before_applicable_declarations);
stylist.get_applicable_declarations(self,
None,
Some(After),
&mut layout_data
.data
.after_applicable_declarations);
} }
None => fail!("no layout data") None => fail!("no layout data")
} }
@ -79,7 +90,8 @@ impl<'ln> MatchMethods for LayoutNode<'ln> {
let computed_values = { let computed_values = {
let layout_data_ref = self.borrow_layout_data(); let layout_data_ref = self.borrow_layout_data();
let layout_data = layout_data_ref.get().as_ref().unwrap(); let layout_data = layout_data_ref.get().as_ref().unwrap();
Arc::new(cascade(layout_data.data.$applicable_declarations, parent_style)) Arc::new(cascade(layout_data.data.$applicable_declarations.as_slice(),
parent_style))
}; };
let mut layout_data_ref = self.mutate_layout_data(); let mut layout_data_ref = self.mutate_layout_data();

View file

@ -11,6 +11,7 @@ use script::dom::bindings::utils::Reflectable;
use script::dom::node::AbstractNode; use script::dom::node::AbstractNode;
use script::layout_interface::{LayoutChan, UntrustedNodeAddress}; use script::layout_interface::{LayoutChan, UntrustedNodeAddress};
use servo_util::range::Range; use servo_util::range::Range;
use servo_util::smallvec::{SmallVec0, SmallVec16};
use std::cast; use std::cast;
use std::cell::{Ref, RefMut}; use std::cell::{Ref, RefMut};
use std::iter::Enumerate; use std::iter::Enumerate;
@ -129,11 +130,11 @@ impl ElementMapping {
/// Data that layout associates with a node. /// Data that layout associates with a node.
pub struct PrivateLayoutData { pub struct PrivateLayoutData {
/// The results of CSS matching for this node. /// The results of CSS matching for this node.
before_applicable_declarations: ~[Arc<~[PropertyDeclaration]>], applicable_declarations: SmallVec16<Arc<~[PropertyDeclaration]>>,
applicable_declarations: ~[Arc<~[PropertyDeclaration]>], before_applicable_declarations: SmallVec0<Arc<~[PropertyDeclaration]>>,
after_applicable_declarations: ~[Arc<~[PropertyDeclaration]>], after_applicable_declarations: SmallVec0<Arc<~[PropertyDeclaration]>>,
/// The results of CSS styling for this node. /// The results of CSS styling for this node.
before_style: Option<Arc<ComputedValues>>, before_style: Option<Arc<ComputedValues>>,
@ -154,9 +155,9 @@ impl PrivateLayoutData {
/// Creates new layout data. /// Creates new layout data.
pub fn new() -> PrivateLayoutData { pub fn new() -> PrivateLayoutData {
PrivateLayoutData { PrivateLayoutData {
applicable_declarations: ~[], applicable_declarations: SmallVec16::new(),
before_applicable_declarations: ~[], before_applicable_declarations: SmallVec0::new(),
after_applicable_declarations: ~[], after_applicable_declarations: SmallVec0::new(),
before_style: None, before_style: None,
style: None, style: None,
after_style: None, after_style: None,

View file

@ -9,6 +9,7 @@ use std::str;
use std::to_bytes; use std::to_bytes;
use servo_util::namespace; use servo_util::namespace;
use servo_util::smallvec::{SmallVec, SmallVec16};
use servo_util::sort; use servo_util::sort;
use media_queries::{Device, Screen}; use media_queries::{Device, Screen};
@ -106,7 +107,7 @@ impl SelectorMap {
N:TNode<E>>( N:TNode<E>>(
&self, &self,
node: &N, node: &N,
matching_rules_list: &mut ~[Rule]) { matching_rules_list: &mut SmallVec16<Rule>) {
if self.empty { if self.empty {
return return
} }
@ -156,7 +157,7 @@ impl SelectorMap {
node: &N, node: &N,
hash: &HashMap<~str,~[Rule]>, hash: &HashMap<~str,~[Rule]>,
key: &str, key: &str,
matching_rules: &mut ~[Rule]) { matching_rules: &mut SmallVec16<Rule>) {
match hash.find_equiv(&key) { match hash.find_equiv(&key) {
Some(rules) => { Some(rules) => {
SelectorMap::get_matching_rules(node, *rules, matching_rules) SelectorMap::get_matching_rules(node, *rules, matching_rules)
@ -170,7 +171,7 @@ impl SelectorMap {
node: &N, node: &N,
hash: &HashMap<~str,~[Rule]>, hash: &HashMap<~str,~[Rule]>,
key: &str, key: &str,
matching_rules: &mut ~[Rule]) { matching_rules: &mut SmallVec16<Rule>) {
match hash.find_equiv(&LowercaseAsciiString(key)) { match hash.find_equiv(&LowercaseAsciiString(key)) {
Some(rules) => { Some(rules) => {
SelectorMap::get_matching_rules(node, *rules, matching_rules) SelectorMap::get_matching_rules(node, *rules, matching_rules)
@ -184,7 +185,7 @@ impl SelectorMap {
N:TNode<E>>( N:TNode<E>>(
node: &N, node: &N,
rules: &[Rule], rules: &[Rule],
matching_rules: &mut ~[Rule]) { matching_rules: &mut SmallVec16<Rule>) {
for rule in rules.iter() { for rule in rules.iter() {
if matches_compound_selector(rule.selector.get(), node) { if matches_compound_selector(rule.selector.get(), node) {
// TODO(pradeep): Is the cloning inefficient? // TODO(pradeep): Is the cloning inefficient?
@ -358,12 +359,13 @@ impl Stylist {
/// Returns the applicable CSS declarations for the given element. This corresponds to /// Returns the applicable CSS declarations for the given element. This corresponds to
/// `ElementRuleCollector` in WebKit. /// `ElementRuleCollector` in WebKit.
pub fn get_applicable_declarations<E:TElement, pub fn get_applicable_declarations<E:TElement,
N:TNode<E>>( N:TNode<E>,
V:SmallVec<Arc<~[PropertyDeclaration]>>>(
&self, &self,
element: &N, element: &N,
style_attribute: Option<&PropertyDeclarationBlock>, style_attribute: Option<&PropertyDeclarationBlock>,
pseudo_element: Option<PseudoElement>) pseudo_element: Option<PseudoElement>,
-> ~[Arc<~[PropertyDeclaration]>] { applicable_declarations: &mut V) {
assert!(element.is_element()); assert!(element.is_element());
assert!(style_attribute.is_none() || pseudo_element.is_none(), assert!(style_attribute.is_none() || pseudo_element.is_none(),
"Style attributes do not apply to pseudo-elements"); "Style attributes do not apply to pseudo-elements");
@ -387,8 +389,7 @@ impl Stylist {
// we have the indices straight at the end. // we have the indices straight at the end.
let mut rule_map_indices = [ 0, ..6 ]; let mut rule_map_indices = [ 0, ..6 ];
// TODO(pcwalton): Small vector optimization. let mut matching_rules_list = SmallVec16::new();
let mut matching_rules_list = ~[];
for (i, rule_map) in rule_map_list.iter().enumerate() { for (i, rule_map) in rule_map_list.iter().enumerate() {
rule_map_indices[i] = matching_rules_list.len(); rule_map_indices[i] = matching_rules_list.len();
@ -406,7 +407,6 @@ impl Stylist {
}); });
// Gather up all rules. // Gather up all rules.
let mut applicable_declarations = ~[];
let mut i = 0; let mut i = 0;
// Step 1: Normal rules. // Step 1: Normal rules.
@ -432,8 +432,6 @@ impl Stylist {
applicable_declarations.push(declaration_iter.next().unwrap()); applicable_declarations.push(declaration_iter.next().unwrap());
i += 1 i += 1
} }
applicable_declarations
} }
} }