auto merge of #1564 : pcwalton/servo/small-vector, r=larsbergstrom

12% improvement on style recalc for the rainbow page.

r? @larsbergstrom
This commit is contained in:
bors-servo 2014-01-30 10:13:32 -08:00
commit 3256cfb78c
5 changed files with 536 additions and 25 deletions

View file

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

View file

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

View file

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

View file

@ -0,0 +1,498 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Small vectors in various sizes. These store a certain number of elements inline and fall back
//! to the heap for larger allocations.
use i = std::unstable::intrinsics::init;
use std::cast;
use std::libc::c_char;
use std::mem;
use std::num;
use std::ptr;
use std::rt::global_heap;
use std::rt::local_heap;
use std::unstable::intrinsics;
use std::unstable::raw::Slice;
use std::util;
// Generic code for all small vectors
trait SmallVecPrivate<T> {
unsafe fn set_len(&mut self, new_len: uint);
unsafe fn set_cap(&mut self, new_cap: uint);
fn data(&self, index: uint) -> *T;
fn mut_data(&mut self, index: uint) -> *mut T;
unsafe fn ptr(&self) -> *T;
unsafe fn mut_ptr(&mut self) -> *mut T;
unsafe fn set_ptr(&mut self, new_ptr: *mut T);
}
pub trait SmallVec<T> : SmallVecPrivate<T> {
fn inline_size(&self) -> uint;
fn len(&self) -> uint;
fn cap(&self) -> uint;
fn spilled(&self) -> bool {
self.cap() > self.inline_size()
}
fn begin(&self) -> *T {
unsafe {
if self.spilled() {
self.ptr()
} else {
self.data(0)
}
}
}
fn end(&self) -> *T {
unsafe {
self.begin().offset(self.len() as int)
}
}
fn iter<'a>(&'a self) -> SmallVecIterator<'a,T> {
SmallVecIterator {
ptr: self.begin(),
end: self.end(),
lifetime: None,
}
}
/// NB: For efficiency reasons (avoiding making a second copy of the inline elements), this
/// actually clears out the original array instead of moving it.
fn move_iter<'a>(&'a mut self) -> SmallVecMoveIterator<'a,T> {
unsafe {
let iter = cast::transmute(self.iter());
let ptr_opt = if self.spilled() {
Some(cast::transmute(self.ptr()))
} else {
None
};
let inline_size = self.inline_size();
self.set_cap(inline_size);
self.set_len(0);
SmallVecMoveIterator {
allocation: ptr_opt,
iter: iter,
lifetime: None,
}
}
}
fn push(&mut self, value: T) {
let cap = self.cap();
if self.len() == cap {
self.grow(num::max(cap * 2, 1))
}
unsafe {
let end: &mut T = cast::transmute(self.end());
intrinsics::move_val_init(end, value);
let len = self.len();
self.set_len(len + 1)
}
}
fn grow(&mut self, new_cap: uint) {
unsafe {
let new_alloc: *mut T = cast::transmute(global_heap::malloc_raw(mem::size_of::<T>() *
new_cap));
ptr::copy_nonoverlapping_memory(new_alloc, self.begin(), self.len());
if self.spilled() {
if intrinsics::owns_managed::<T>() {
local_heap::local_free(self.ptr() as *u8 as *c_char)
} else {
global_heap::exchange_free(self.ptr() as *u8 as *c_char)
}
} else {
let mut_begin: *mut T = cast::transmute(self.begin());
intrinsics::set_memory(mut_begin, 0, self.len())
}
self.set_ptr(new_alloc);
self.set_cap(new_cap)
}
}
fn get<'a>(&'a self, index: uint) -> &'a T {
if index >= self.len() {
self.fail_bounds_check(index)
}
unsafe {
cast::transmute(self.begin().offset(index as int))
}
}
fn get_mut<'a>(&'a mut self, index: uint) -> &'a mut T {
if index >= self.len() {
self.fail_bounds_check(index)
}
unsafe {
cast::transmute(self.begin().offset(index as int))
}
}
fn slice<'a>(&'a self, start: uint, end: uint) -> &'a [T] {
assert!(start <= end);
assert!(end <= self.len());
unsafe {
cast::transmute(Slice {
data: self.begin().offset(start as int),
len: (end - start)
})
}
}
fn as_slice<'a>(&'a self) -> &'a [T] {
self.slice(0, self.len())
}
fn mut_slice<'a>(&'a mut self, start: uint, end: uint) -> &'a mut [T] {
assert!(start <= end);
assert!(end <= self.len());
unsafe {
cast::transmute(Slice {
data: self.begin().offset(start as int),
len: (end - start)
})
}
}
fn mut_slice_from<'a>(&'a mut self, start: uint) -> &'a mut [T] {
let len = self.len();
self.mut_slice(start, len)
}
fn fail_bounds_check(&self, index: uint) {
fail!("index {} beyond length ({})", index, self.len())
}
}
pub struct SmallVecIterator<'a,T> {
priv ptr: *T,
priv end: *T,
priv lifetime: Option<&'a T>
}
impl<'a,T> Iterator<&'a T> for SmallVecIterator<'a,T> {
#[inline]
fn next(&mut self) -> Option<&'a T> {
unsafe {
if self.ptr == self.end {
return None
}
let old = self.ptr;
self.ptr = if mem::size_of::<T>() == 0 {
cast::transmute(self.ptr as uint + 1)
} else {
self.ptr.offset(1)
};
Some(cast::transmute(old))
}
}
}
pub struct SmallVecMoveIterator<'a,T> {
priv allocation: Option<*mut u8>,
priv iter: SmallVecIterator<'static,T>,
priv lifetime: Option<&'a T>,
}
impl<'a,T> Iterator<T> for SmallVecMoveIterator<'a,T> {
#[inline]
fn next(&mut self) -> Option<T> {
unsafe {
match self.iter.next() {
None => None,
Some(reference) => {
// Zero out the values as we go so they don't get double-freed.
let reference: &mut T = cast::transmute(reference);
Some(util::replace(reference, intrinsics::init()))
}
}
}
}
}
#[unsafe_destructor]
impl<'a,T> Drop for SmallVecMoveIterator<'a,T> {
fn drop(&mut self) {
// Destroy the remaining elements.
for _ in *self {}
match self.allocation {
None => {}
Some(allocation) => {
unsafe {
if intrinsics::owns_managed::<T>() {
local_heap::local_free(allocation as *u8 as *c_char)
} else {
global_heap::exchange_free(allocation as *u8 as *c_char)
}
}
}
}
}
}
// Concrete implementations
macro_rules! def_small_vector(
($name:ident, $size:expr) => (
pub struct $name<T> {
len: uint,
cap: uint,
ptr: *T,
data: [T, ..$size],
}
)
)
macro_rules! def_small_vector_private_trait_impl(
($name:ident, $size:expr) => (
impl<T> SmallVecPrivate<T> for $name<T> {
unsafe fn set_len(&mut self, new_len: uint) {
self.len = new_len
}
unsafe fn set_cap(&mut self, new_cap: uint) {
self.cap = new_cap
}
fn data(&self, index: uint) -> *T {
let ptr: *T = &self.data[index];
ptr
}
fn mut_data(&mut self, index: uint) -> *mut T {
let ptr: *mut T = &mut self.data[index];
ptr
}
unsafe fn ptr(&self) -> *T {
self.ptr
}
unsafe fn mut_ptr(&mut self) -> *mut T {
cast::transmute(self.ptr)
}
unsafe fn set_ptr(&mut self, new_ptr: *mut T) {
self.ptr = cast::transmute(new_ptr)
}
}
)
)
macro_rules! def_small_vector_trait_impl(
($name:ident, $size:expr) => (
impl<T> SmallVec<T> for $name<T> {
fn inline_size(&self) -> uint {
$size
}
fn len(&self) -> uint {
self.len
}
fn cap(&self) -> uint {
self.cap
}
}
)
)
macro_rules! def_small_vector_drop_impl(
($name:ident, $size:expr) => (
#[unsafe_destructor]
impl<T> Drop for $name<T> {
fn drop(&mut self) {
if !self.spilled() {
return
}
unsafe {
let ptr = self.mut_ptr();
for i in range(0, self.len()) {
*ptr.offset(i as int) = intrinsics::uninit();
}
if intrinsics::owns_managed::<T>() {
local_heap::local_free(self.ptr() as *u8 as *c_char)
} else {
global_heap::exchange_free(self.ptr() as *u8 as *c_char)
}
}
}
}
)
)
macro_rules! def_small_vector_impl(
($name:ident, $size:expr) => (
impl<T> $name<T> {
#[inline]
pub fn new() -> $name<T> {
unsafe {
$name {
len: 0,
cap: $size,
ptr: ptr::null(),
data: intrinsics::init(),
}
}
}
}
)
)
/// TODO(pcwalton): Remove in favor of `vec_ng` after a Rust upgrade.
pub struct SmallVec0<T> {
len: uint,
cap: uint,
ptr: *mut T,
}
impl<T> SmallVecPrivate<T> for SmallVec0<T> {
unsafe fn set_len(&mut self, new_len: uint) {
self.len = new_len
}
unsafe fn set_cap(&mut self, new_cap: uint) {
self.cap = new_cap
}
fn data(&self, _: uint) -> *T {
ptr::null()
}
fn mut_data(&mut self, _: uint) -> *mut T {
ptr::mut_null()
}
unsafe fn ptr(&self) -> *T {
cast::transmute(self.ptr)
}
unsafe fn mut_ptr(&mut self) -> *mut T {
self.ptr
}
unsafe fn set_ptr(&mut self, new_ptr: *mut T) {
self.ptr = new_ptr
}
}
impl<T> SmallVec<T> for SmallVec0<T> {
fn inline_size(&self) -> uint {
0
}
fn len(&self) -> uint {
self.len
}
fn cap(&self) -> uint {
self.cap
}
}
impl<T> SmallVec0<T> {
pub fn new() -> SmallVec0<T> {
SmallVec0 {
len: 0,
cap: 0,
ptr: ptr::mut_null(),
}
}
}
def_small_vector_drop_impl!(SmallVec0, 0)
def_small_vector!(SmallVec1, 1)
def_small_vector_private_trait_impl!(SmallVec1, 1)
def_small_vector_trait_impl!(SmallVec1, 1)
def_small_vector_drop_impl!(SmallVec1, 1)
def_small_vector_impl!(SmallVec1, 1)
def_small_vector!(SmallVec2, 2)
def_small_vector_private_trait_impl!(SmallVec2, 2)
def_small_vector_trait_impl!(SmallVec2, 2)
def_small_vector_drop_impl!(SmallVec2, 2)
def_small_vector_impl!(SmallVec2, 2)
def_small_vector!(SmallVec4, 4)
def_small_vector_private_trait_impl!(SmallVec4, 4)
def_small_vector_trait_impl!(SmallVec4, 4)
def_small_vector_drop_impl!(SmallVec4, 4)
def_small_vector_impl!(SmallVec4, 4)
def_small_vector!(SmallVec8, 8)
def_small_vector_private_trait_impl!(SmallVec8, 8)
def_small_vector_trait_impl!(SmallVec8, 8)
def_small_vector_drop_impl!(SmallVec8, 8)
def_small_vector_impl!(SmallVec8, 8)
def_small_vector!(SmallVec16, 16)
def_small_vector_private_trait_impl!(SmallVec16, 16)
def_small_vector_trait_impl!(SmallVec16, 16)
def_small_vector_drop_impl!(SmallVec16, 16)
def_small_vector_impl!(SmallVec16, 16)
def_small_vector!(SmallVec24, 24)
def_small_vector_private_trait_impl!(SmallVec24, 24)
def_small_vector_trait_impl!(SmallVec24, 24)
def_small_vector_drop_impl!(SmallVec24, 24)
def_small_vector_impl!(SmallVec24, 24)
def_small_vector!(SmallVec32, 32)
def_small_vector_private_trait_impl!(SmallVec32, 32)
def_small_vector_trait_impl!(SmallVec32, 32)
def_small_vector_drop_impl!(SmallVec32, 32)
def_small_vector_impl!(SmallVec32, 32)
#[cfg(test)]
pub mod tests {
use smallvec::{SmallVec, SmallVec0, SmallVec2, SmallVec16};
// We heap allocate all these strings so that double frees will show up under valgrind.
#[test]
pub fn test_inline() {
let mut v = SmallVec16::new();
v.push(~"hello");
v.push(~"there");
assert_eq!(v.as_slice(), &[~"hello", ~"there"]);
}
#[test]
pub fn test_spill() {
let mut v = SmallVec2::new();
v.push(~"hello");
v.push(~"there");
v.push(~"burma");
v.push(~"shave");
assert_eq!(v.as_slice(), &[~"hello", ~"there", ~"burma", ~"shave"]);
}
#[test]
pub fn test_double_spill() {
let mut v = SmallVec2::new();
v.push(~"hello");
v.push(~"there");
v.push(~"burma");
v.push(~"shave");
v.push(~"hello");
v.push(~"there");
v.push(~"burma");
v.push(~"shave");
assert_eq!(v.as_slice(), &[
~"hello", ~"there", ~"burma", ~"shave", ~"hello", ~"there", ~"burma", ~"shave",
]);
}
#[test]
pub fn test_smallvec0() {
let mut v = SmallVec0::new();
v.push(~"hello");
v.push(~"there");
v.push(~"burma");
v.push(~"shave");
v.push(~"hello");
v.push(~"there");
v.push(~"burma");
v.push(~"shave");
assert_eq!(v.as_slice(), &[
~"hello", ~"there", ~"burma", ~"shave", ~"hello", ~"there", ~"burma", ~"shave",
]);
}
}

View file

@ -23,3 +23,5 @@ pub mod task;
pub mod workqueue;
pub mod namespace;
pub mod sort;
pub mod smallvec;