From d1c13faf4be3307d7e2ba55f1b1046d230610c47 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 27 Mar 2015 18:59:18 -0700 Subject: [PATCH] script: Optimize JS rooting to not move the entire `Root` struct from the stack to the return out-pointer. This was showing up very high in instruction-level profiling. --- components/script/dom/bindings/js.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/components/script/dom/bindings/js.rs b/components/script/dom/bindings/js.rs index 84644a8428f..ef5fb2155df 100644 --- a/components/script/dom/bindings/js.rs +++ b/components/script/dom/bindings/js.rs @@ -626,12 +626,12 @@ impl RootCollection { } } - /// Track a stack-based root to ensure LIFO root ordering - fn root<'b, T: Reflectable>(&self, untracked: &Root) { + /// Track a stack-based root as a pointer to ensure LIFO root ordering. + fn root<'b>(&self, untracked_js_ptr: *mut JSObject) { unsafe { let roots = self.roots.get(); - (*roots).push(untracked.js_ptr); - debug!(" rooting {:?}", untracked.js_ptr); + (*roots).push(untracked_js_ptr); + debug!(" rooting {:?}", untracked_js_ptr); assert!(!(*roots).spilled()); } } @@ -668,15 +668,18 @@ impl Root { /// Create a new stack-bounded root for the provided JS-owned value. /// It cannot not outlive its associated `RootCollection`, and it contains /// a `JSRef` which cannot outlive this new `Root`. + #[inline] fn new(roots: &'static RootCollection, unrooted: NonZero<*const T>) -> Root { - let root = Root { + let js_ptr = unsafe { + (**unrooted).reflector().get_jsobject() + }; + roots.root(js_ptr); + Root { root_list: roots, ptr: unrooted, - js_ptr: unsafe { (**unrooted).reflector().get_jsobject() }, - }; - roots.root(&root); - root + js_ptr: js_ptr, + } } /// Obtain a safe reference to the wrapped JS owned-value that cannot