script: Use atom comparison in more places, especially for attributes.

75% improvement in style recalc for Guardians of the Galaxy.
This commit is contained in:
Patrick Walton 2014-09-12 13:28:37 -07:00
parent 61642d64b5
commit 874db26104
32 changed files with 300 additions and 147 deletions

View file

@ -39,3 +39,12 @@ git = "https://github.com/servo/rust-geom"
[dependencies.url]
git = "https://github.com/servo/rust-url"
[dependencies.string_cache]
git = "https://github.com/servo/string-cache"
branch = "pre-rustup"
[dependencies.string_cache_macros]
git = "https://github.com/servo/string-cache"
branch = "pre-rustup"

View file

@ -57,6 +57,7 @@ use script::dom::node::{CommentNodeTypeId, DoctypeNodeTypeId, DocumentFragmentNo
use script::dom::node::{DocumentNodeTypeId, ElementNodeTypeId, ProcessingInstructionNodeTypeId};
use script::dom::node::{TextNodeTypeId};
use script::dom::htmlobjectelement::is_image_data;
use servo_util::atom::Atom;
use servo_util::namespace;
use std::mem;
use std::sync::atomics::Relaxed;
@ -1047,7 +1048,8 @@ trait ObjectElement {
impl<'ln> ObjectElement for ThreadSafeLayoutNode<'ln> {
fn get_type_and_data(&self) -> (Option<&'static str>, Option<&'static str>) {
let elem = self.as_element();
(elem.get_attr(&namespace::Null, "type"), elem.get_attr(&namespace::Null, "data"))
(elem.get_attr(&namespace::Null, &satom!("type")),
elem.get_attr(&namespace::Null, &satom!("data")))
}
fn has_object_data(&self) -> bool {

View file

@ -223,8 +223,7 @@ impl StyleSharingCandidate {
style: style,
parent_style: parent_style,
local_name: element.get_local_name().clone(),
class: element.get_attr(&Null, "class")
.map(|string| string.to_string()),
class: element.get_attr(&Null, &satom!("class")).map(|string| string.to_string()),
})
}
@ -232,7 +231,7 @@ impl StyleSharingCandidate {
if *element.get_local_name() != self.local_name {
return false
}
match (&self.class, element.get_attr(&Null, "class")) {
match (&self.class, element.get_attr(&Null, &satom!("class"))) {
(&None, Some(_)) | (&Some(_), None) => return false,
(&Some(ref this_class), Some(element_class)) if element_class != this_class.as_slice() => {
return false
@ -454,7 +453,7 @@ impl<'ln> MatchMethods for LayoutNode<'ln> {
}
let ok = {
let element = self.as_element();
element.style_attribute().is_none() && element.get_attr(&Null, "id").is_none()
element.style_attribute().is_none() && element.get_attr(&Null, &satom!("id")).is_none()
};
if !ok {
return CannotShare(false)
@ -501,7 +500,7 @@ impl<'ln> MatchMethods for LayoutNode<'ln> {
// TODO: case-sensitivity depends on the document type and quirks mode
element
.get_attr(&Null, "class")
.get_attr(&Null, &satom!("class"))
.map(|attr| {
for c in attr.split(style::SELECTOR_WHITESPACE) {
bf.insert(&c);
@ -520,7 +519,7 @@ impl<'ln> MatchMethods for LayoutNode<'ln> {
// TODO: case-sensitivity depends on the document type and quirks mode
element
.get_attr(&Null, "class")
.get_attr(&Null, &satom!("class"))
.map(|attr| {
for c in attr.split(style::SELECTOR_WHITESPACE) {
bf.remove(&c);

View file

@ -39,6 +39,7 @@ use serialize::{Encodable, Encoder};
use servo_msg::constellation_msg::{ConstellationChan, FrameRectMsg, PipelineId, SubpageId};
use servo_net::image::holder::ImageHolder;
use servo_net::local_image_cache::LocalImageCache;
use servo_util::atom::Atom;
use servo_util::geometry::Au;
use servo_util::geometry;
use servo_util::logical_geometry::{LogicalRect, LogicalSize, LogicalMargin};
@ -174,7 +175,7 @@ impl ImageFragmentInfo {
image_url: Url,
local_image_cache: Arc<Mutex<LocalImageCache>>)
-> ImageFragmentInfo {
fn convert_length(node: &ThreadSafeLayoutNode, name: &str) -> Option<Au> {
fn convert_length(node: &ThreadSafeLayoutNode, name: &Atom) -> Option<Au> {
let element = node.as_element();
element.get_attr(&namespace::Null, name).and_then(|string| {
let n: Option<int> = FromStr::from_str(string);
@ -183,8 +184,8 @@ impl ImageFragmentInfo {
}
let is_vertical = node.style().writing_mode.is_vertical();
let dom_width = convert_length(node, "width");
let dom_height = convert_length(node, "height");
let dom_width = convert_length(node, &satom!("width"));
let dom_height = convert_length(node, &satom!("height"));
ImageFragmentInfo {
image: ImageHolder::new(image_url, local_image_cache),
computed_inline_size: None,
@ -337,7 +338,7 @@ impl TableColumnFragmentInfo {
pub fn new(node: &ThreadSafeLayoutNode) -> TableColumnFragmentInfo {
let span = {
let element = node.as_element();
element.get_attr(&namespace::Null, "span").and_then(|string| {
element.get_attr(&namespace::Null, &satom!("span")).and_then(|string| {
let n: Option<int> = FromStr::from_str(string);
n
})

View file

@ -29,6 +29,10 @@ extern crate servo_msg = "msg";
#[phase(plugin, link)]
extern crate servo_util = "util";
#[phase(plugin)]
extern crate string_cache_macros;
extern crate string_cache;
extern crate collections;
extern crate encoding;
extern crate green;

View file

@ -273,15 +273,14 @@ impl<'ln> TNode<LayoutElement<'ln>> for LayoutNode<'ln> {
fn match_attr(&self, attr: &AttrSelector, test: |&str| -> bool) -> bool {
assert!(self.is_element())
let name = if self.is_html_element_in_html_document() {
attr.lower_name.as_slice()
&attr.lower_name
} else {
attr.name.as_slice()
&attr.name
};
match attr.namespace {
SpecificNamespace(ref ns) => {
let element = self.as_element();
element.get_attr(ns, name)
.map_or(false, |attr| test(attr))
element.get_attr(ns, name).map_or(false, |attr| test(attr))
},
// FIXME: https://github.com/mozilla/servo/issues/1558
AnyNamespace => false,
@ -383,7 +382,7 @@ impl<'le> TElement for LayoutElement<'le> {
}
#[inline]
fn get_attr(&self, namespace: &Namespace, name: &str) -> Option<&'static str> {
fn get_attr(&self, namespace: &Namespace, name: &Atom) -> Option<&'static str> {
unsafe { self.element.get_attr_val_for_layout(namespace, name) }
}
@ -395,7 +394,9 @@ impl<'le> TElement for LayoutElement<'le> {
ElementNodeTypeId(HTMLAnchorElementTypeId) |
ElementNodeTypeId(HTMLAreaElementTypeId) |
ElementNodeTypeId(HTMLLinkElementTypeId) => {
unsafe { self.element.get_attr_val_for_layout(&namespace::Null, "href") }
unsafe {
self.element.get_attr_val_for_layout(&namespace::Null, &satom!("href"))
}
}
_ => None,
}
@ -409,7 +410,9 @@ impl<'le> TElement for LayoutElement<'le> {
#[inline]
fn get_id(&self) -> Option<Atom> {
unsafe { self.element.get_attr_atom_for_layout(&namespace::Null, "id") }
unsafe {
self.element.get_attr_atom_for_layout(&namespace::Null, &satom!("id"))
}
}
fn get_disabled_state(&self) -> bool {
@ -424,11 +427,24 @@ impl<'le> TElement for LayoutElement<'le> {
}
}
fn has_class(&self, name: &str) -> bool {
fn has_class(&self, name: &Atom) -> bool {
unsafe {
self.element.has_class_for_layout(name)
}
}
fn each_class(&self, callback: |&Atom|) {
unsafe {
match self.element.get_classes_for_layout() {
None => {}
Some(ref classes) => {
for class in classes.iter() {
callback(class)
}
}
}
}
}
}
fn get_content(content_list: &content::T) -> String {
@ -758,8 +774,10 @@ pub struct ThreadSafeLayoutElement<'le> {
impl<'le> ThreadSafeLayoutElement<'le> {
#[inline]
pub fn get_attr(&self, namespace: &Namespace, name: &str) -> Option<&'static str> {
unsafe { self.element.get_attr_val_for_layout(namespace, name) }
pub fn get_attr(&self, namespace: &Namespace, name: &Atom) -> Option<&'static str> {
unsafe {
self.element.get_attr_val_for_layout(namespace, name)
}
}
}