script: Use a 16-element SmallVec for the root collection.

In my tests the size of the root collection never exceeded 7, so 16
seems like a nice conservative number.
This commit is contained in:
Patrick Walton 2014-10-28 11:12:09 -07:00
parent 541077286c
commit 5e9127e2b2

View file

@ -53,7 +53,8 @@ use js::jsapi::JSObject;
use layout_interface::TrustedNodeAddress; use layout_interface::TrustedNodeAddress;
use script_task::StackRoots; use script_task::StackRoots;
use std::cell::{Cell, RefCell}; use servo_util::smallvec::{SmallVec, SmallVec16};
use std::cell::{Cell, UnsafeCell};
use std::default::Default; use std::default::Default;
use std::kinds::marker::ContravariantLifetime; use std::kinds::marker::ContravariantLifetime;
use std::mem; use std::mem;
@ -419,14 +420,14 @@ impl<T: Assignable<U>, U: Reflectable> TemporaryPushable<T> for Vec<JS<U>> {
/// An opaque, LIFO rooting mechanism. /// An opaque, LIFO rooting mechanism.
pub struct RootCollection { pub struct RootCollection {
roots: RefCell<Vec<*mut JSObject>>, roots: UnsafeCell<SmallVec16<*mut JSObject>>,
} }
impl RootCollection { impl RootCollection {
/// Create an empty collection of roots /// Create an empty collection of roots
pub fn new() -> RootCollection { pub fn new() -> RootCollection {
RootCollection { RootCollection {
roots: RefCell::new(vec!()), roots: UnsafeCell::new(SmallVec16::new()),
} }
} }
@ -438,17 +439,23 @@ impl RootCollection {
/// Track a stack-based root to ensure LIFO root ordering /// Track a stack-based root to ensure LIFO root ordering
fn root<'a, 'b, T: Reflectable>(&self, untracked: &Root<'a, 'b, T>) { fn root<'a, 'b, T: Reflectable>(&self, untracked: &Root<'a, 'b, T>) {
let mut roots = self.roots.borrow_mut(); unsafe {
roots.push(untracked.js_ptr); let roots = self.roots.get();
(*roots).push(untracked.js_ptr);
debug!(" rooting {:?}", untracked.js_ptr); debug!(" rooting {:?}", untracked.js_ptr);
} }
}
/// Stop tracking a stack-based root, asserting if LIFO root ordering has been violated /// Stop tracking a stack-based root, asserting if LIFO root ordering has been violated
fn unroot<'a, 'b, T: Reflectable>(&self, rooted: &Root<'a, 'b, T>) { fn unroot<'a, 'b, T: Reflectable>(&self, rooted: &Root<'a, 'b, T>) {
let mut roots = self.roots.borrow_mut(); unsafe {
debug!("unrooting {:?} (expecting {:?}", roots.last().unwrap(), rooted.js_ptr); let roots = self.roots.get();
assert!(*roots.last().unwrap() == rooted.js_ptr); debug!("unrooting {:?} (expecting {:?}",
roots.pop().unwrap(); (*roots).as_slice().last().unwrap(),
rooted.js_ptr);
assert!(*(*roots).as_slice().last().unwrap() == rooted.js_ptr);
(*roots).pop().unwrap();
}
} }
} }