diff options
| -rw-r--r-- | lld/include/lld/Core/Parallel.h | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/lld/include/lld/Core/Parallel.h b/lld/include/lld/Core/Parallel.h index eaf13907201..0b467caa469 100644 --- a/lld/include/lld/Core/Parallel.h +++ b/lld/include/lld/Core/Parallel.h @@ -283,9 +283,20 @@ void parallel_for_each(Iterator begin, Iterator end, Func func) { #else template <class Iterator, class Func> void parallel_for_each(Iterator begin, Iterator end, Func func) { + // TaskGroup has a relatively high overhead, so we want to reduce + // the number of spawn() calls. We'll create up to 1024 tasks here. + // (Note that 1024 is an arbitrary number. This code probably needs + // improving to take the number of available cores into account.) + ptrdiff_t taskSize = std::distance(begin, end) / 1024; + if (taskSize == 0) + taskSize = 1; + TaskGroup tg; - for (; begin != end; ++begin) - tg.spawn([=, &func] { func(*begin); }); + while (taskSize <= std::distance(begin, end)) { + tg.spawn([=, &func] { std::for_each(begin, begin + taskSize, func); }); + begin += taskSize; + } + std::for_each(begin, end, func); } #endif } // end namespace lld |

