Reduce the size of top_down_dom stackframes.

The idea is to put the memmoving of the ThreadLocalContext out of line from the point of view of top_down_dom so we don't need to allocate a large chunk of stack space for it.
This commit is contained in:
Boris Zbarsky 2017-06-08 12:35:06 -04:00
parent 3616b8f0c3
commit 93271e199b
2 changed files with 24 additions and 4 deletions

View file

@ -9,6 +9,7 @@
use rayon;
use std::cell::{Ref, RefCell, RefMut};
use std::ops::DerefMut;
/// A scoped TLS set, that is alive during the `'scope` lifetime.
///
@ -52,11 +53,16 @@ impl<'scope, T: Send> ScopedTLS<'scope, T> {
}
/// Ensure that the current data this thread owns is initialized, or
/// initialize it using `f`.
pub fn ensure<F: FnOnce() -> T>(&self, f: F) -> RefMut<T> {
/// initialize it using `f`. We want ensure() to be fast and inline, and we
/// want to inline the memmove that initializes the Option<T>. But we don't
/// want to inline space for the entire large T struct in our stack frame.
/// That's why we hand `f` a mutable borrow to write to instead of just
/// having it return a T.
#[inline(always)]
pub fn ensure<F: FnOnce(&mut Option<T>)>(&self, f: F) -> RefMut<T> {
let mut opt = self.borrow_mut();
if opt.is_none() {
*opt = Some(f());
f(opt.deref_mut());
}
RefMut::map(opt, |x| x.as_mut().unwrap())