From 85cead23374ff9ca01f5132fc178626f286c7064 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Fri, 23 Sep 2016 22:03:23 +0200 Subject: [PATCH] [mtouch] Don't use nested Parallel.ForEach loops. (#888) The nested Parallel.ForEach loops don't take into account the outer MaxDegreeOfParallelism value, causing us to spawn more concurrent tasks than we want to. So refactor the code to have one static list of tasks, which each subtask adds to. --- tools/mtouch/Application.cs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/tools/mtouch/Application.cs b/tools/mtouch/Application.cs index a5f98e5dd9..20f4bbc0a6 100644 --- a/tools/mtouch/Application.cs +++ b/tools/mtouch/Application.cs @@ -1694,19 +1694,30 @@ namespace Xamarin.Bundler { public class BuildTasks : List { - static void Execute (BuildTask v) + static void Execute (List added, BuildTask v) { var next = v.Execute (); - if (next != null) - Parallel.ForEach (next, new ParallelOptions () { MaxDegreeOfParallelism = Driver.Concurrency }, Execute); + if (next != null) { + lock (added) + added.AddRange (next); + } } public void ExecuteInParallel () { if (Count == 0) return; - - Parallel.ForEach (this, new ParallelOptions () { MaxDegreeOfParallelism = Driver.Concurrency }, Execute); + + var build_list = new List (this); + var added = new List (); + while (build_list.Count > 0) { + added.Clear (); + Parallel.ForEach (build_list, new ParallelOptions () { MaxDegreeOfParallelism = Driver.Concurrency }, (v) => { + Execute (added, v); + }); + build_list.Clear (); + build_list.AddRange (added); + } Clear (); }