From a121fb7d8ffbd8bed00ed13f746cfa800c74c066 Mon Sep 17 00:00:00 2001 From: SCG82 Date: Tue, 7 Jan 2020 07:12:15 -0800 Subject: [PATCH] parallelize using libdispatch (Grand Central Dispatch) --- CMakeLists.txt | 1 + src/Dependency.h | 2 -- src/DylibBundler.cpp | 9 +++++---- src/ParallelForEach.h | 23 +++++++++++++++++++++++ 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 src/ParallelForEach.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4460b7e..3cf07a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,4 +24,5 @@ add_executable(dylibbundler src/Settings.h src/Utils.cpp src/Utils.h + src/ParallelForEach.h ) diff --git a/src/Dependency.h b/src/Dependency.h index 1954cae..fb5a165 100644 --- a/src/Dependency.h +++ b/src/Dependency.h @@ -6,8 +6,6 @@ #include #include -//#include "DylibBundler.h" - class DylibBundler; class Dependency { diff --git a/src/DylibBundler.cpp b/src/DylibBundler.cpp index 94b41a7..850201c 100644 --- a/src/DylibBundler.cpp +++ b/src/DylibBundler.cpp @@ -12,6 +12,7 @@ #include #endif +#include "ParallelForEach.h" #include "Dependency.h" #include "Utils.h" @@ -161,18 +162,18 @@ void DylibBundler::bundleDependencies() // copy & fix up dependencies if (bundleLibs()) { createDestDir(); - for (const auto& dep : deps) { + parallel_for_each(deps.begin(), deps.end(), [&](Dependency* dep) { dep->CopyToBundle(); changeLibPathsOnFile(dep->InstallPath()); fixRpathsOnFile(dep->OriginalPath(), dep->InstallPath()); - } + }); } // fix up input files const auto files = filesToFix(); - for (const auto& file : files) { + parallel_for_each(files.begin(), files.end(), [&](const std::string& file) { changeLibPathsOnFile(file); fixRpathsOnFile(file, file); - } + }); } void DylibBundler::bundleQtPlugins() diff --git a/src/ParallelForEach.h b/src/ParallelForEach.h new file mode 100644 index 0000000..5b66f9e --- /dev/null +++ b/src/ParallelForEach.h @@ -0,0 +1,23 @@ +#ifndef DYLIBBUNDLER_PARALLELFOREACH_H +#define DYLIBBUNDLER_PARALLELFOREACH_H + +#include +#include + +template +inline void parallel_for_each(It a, It b, F&& f) +{ + size_t count = std::distance(a,b); + using data_t = std::pair; + data_t helper = data_t(a, std::forward(f)); + dispatch_apply_f(count, + dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), + &helper, + [](void* ctx, size_t cnt) { + auto* d = static_cast(ctx); + auto elem_it = std::next(d->first, cnt); + (*d).second(*(elem_it)); + }); +} + +#endif //DYLIBBUNDLER_PARALLELFOREACH_H