Lazily initialize 'leaf node' taskgroups for unlinked spawns, for an apparent 11% speedup.

This commit is contained in:
Ben Blum
2013-08-05 16:55:08 -04:00
parent 47e82c8555
commit 1f95bd7684
2 changed files with 63 additions and 45 deletions

View File

@@ -193,6 +193,10 @@ impl BlockedTask {
/// Create a blocked task, unless the task was already killed. /// Create a blocked task, unless the task was already killed.
pub fn try_block(mut task: ~Task) -> Either<~Task, BlockedTask> { pub fn try_block(mut task: ~Task) -> Either<~Task, BlockedTask> {
// NB: As an optimization, we could give a free pass to being unkillable
// to tasks whose taskgroups haven't been initialized yet, but that
// introduces complications with select() and with the test cases below,
// and it's not clear the uncommon performance boost is worth it.
if task.death.unkillable > 0 { if task.death.unkillable > 0 {
Right(Unkillable(task)) Right(Unkillable(task))
} else { } else {

View File

@@ -568,7 +568,8 @@ impl RuntimeGlue {
let me = Local::unsafe_borrow::<Task>(); let me = Local::unsafe_borrow::<Task>();
blk(match (*me).taskgroup { blk(match (*me).taskgroup {
None => { None => {
// Main task, doing first spawn ever. Lazily initialize. // First task in its (unlinked/unsupervised) taskgroup.
// Lazily initialize.
let mut members = TaskSet::new(); let mut members = TaskSet::new();
let my_handle = (*me).death.kill_handle.get_ref().clone(); let my_handle = (*me).death.kill_handle.get_ref().clone();
members.insert(NewTask(my_handle)); members.insert(NewTask(my_handle));
@@ -591,15 +592,21 @@ impl RuntimeGlue {
} }
} }
// Returns 'None' in the case where the child's TG should be lazily initialized.
fn gen_child_taskgroup(linked: bool, supervised: bool) fn gen_child_taskgroup(linked: bool, supervised: bool)
-> (TaskGroupArc, AncestorList, bool) { -> Option<(TaskGroupArc, AncestorList, bool)> {
// FIXME(#7544): Not safe to lazily initialize in the old runtime. Remove
// this context check once 'spawn_raw_oldsched' is gone.
if context() == OldTaskContext || linked || supervised {
// with_my_taskgroup will lazily initialize the parent's taskgroup if
// it doesn't yet exist. We don't want to call it in the unlinked case.
do RuntimeGlue::with_my_taskgroup |spawner_group| { do RuntimeGlue::with_my_taskgroup |spawner_group| {
let ancestors = AncestorList(spawner_group.ancestors.map(|x| x.clone())); let ancestors = AncestorList(spawner_group.ancestors.map(|x| x.clone()));
if linked { if linked {
// Child is in the same group as spawner. // Child is in the same group as spawner.
// Child's ancestors are spawner's ancestors. // Child's ancestors are spawner's ancestors.
// Propagate main-ness. // Propagate main-ness.
(spawner_group.tasks.clone(), ancestors, spawner_group.is_main) Some((spawner_group.tasks.clone(), ancestors, spawner_group.is_main))
} else { } else {
// Child is in a separate group from spawner. // Child is in a separate group from spawner.
let g = Exclusive::new(Some(TaskGroupData { let g = Exclusive::new(Some(TaskGroupData {
@@ -620,9 +627,12 @@ fn gen_child_taskgroup(linked: bool, supervised: bool)
// Child has no ancestors. // Child has no ancestors.
AncestorList(None) AncestorList(None)
}; };
(g, a, false) Some((g, a, false))
} }
} }
} else {
None
}
} }
// Set up membership in taskgroup and descendantship in all ancestor // Set up membership in taskgroup and descendantship in all ancestor
@@ -670,8 +680,11 @@ fn spawn_raw_newsched(mut opts: TaskOpts, f: ~fn()) {
let child_wrapper: ~fn() = || { let child_wrapper: ~fn() = || {
// Child task runs this code. // Child task runs this code.
let child_data = Cell::new(child_data.take()); // :(
let enlist_success = do Local::borrow::<Task, bool> |me| { // If child data is 'None', the enlist is vacuously successful.
let enlist_success = do child_data.take().map_consume_default(true) |child_data| {
let child_data = Cell::new(child_data); // :(
do Local::borrow::<Task, bool> |me| {
let (child_tg, ancestors, is_main) = child_data.take(); let (child_tg, ancestors, is_main) = child_data.take();
let mut ancestors = ancestors; let mut ancestors = ancestors;
// FIXME(#7544): Optimize out the xadd in this clone, somehow. // FIXME(#7544): Optimize out the xadd in this clone, somehow.
@@ -685,6 +698,7 @@ fn spawn_raw_newsched(mut opts: TaskOpts, f: ~fn()) {
} else { } else {
false false
} }
}
}; };
// Should be run after the local-borrowed task is returned. // Should be run after the local-borrowed task is returned.
if enlist_success { if enlist_success {
@@ -749,7 +763,7 @@ fn spawn_raw_newsched(mut opts: TaskOpts, f: ~fn()) {
let join_task = join_task_cell.take(); let join_task = join_task_cell.take();
let bootstrap_task = ~do Task::new_root(&mut new_sched.stack_pool) || { let bootstrap_task = ~do Task::new_root(&mut new_sched.stack_pool) || {
rtdebug!("boostraping a 1:1 scheduler"); rtdebug!("bootstrapping a 1:1 scheduler");
}; };
new_sched.bootstrap(bootstrap_task); new_sched.bootstrap(bootstrap_task);
@@ -793,7 +807,7 @@ fn spawn_raw_newsched(mut opts: TaskOpts, f: ~fn()) {
fn spawn_raw_oldsched(mut opts: TaskOpts, f: ~fn()) { fn spawn_raw_oldsched(mut opts: TaskOpts, f: ~fn()) {
let (child_tg, ancestors, is_main) = let (child_tg, ancestors, is_main) =
gen_child_taskgroup(opts.linked, opts.supervised); gen_child_taskgroup(opts.linked, opts.supervised).expect("old runtime needs TG");
unsafe { unsafe {
let child_data = Cell::new((child_tg, ancestors, f)); let child_data = Cell::new((child_tg, ancestors, f));