[PATCH] D46207: Avoid some memory allocations in the ThreadPool
Rafael Avila de Espindola via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 27 12:49:39 PDT 2018
espindola created this revision.
espindola added a reviewer: dblaikie.
I was surprised to see std::function related allocations showing up it a profile of lld. In the case of lld the allocations are tiny and don't really impact performance.
I am not sure if we should be trying to improve this code as it probably needs a full rewrite for better performance, but I was curious how hard it would be to remove some of the allocations.
Note that this only updated one of the executors so far.
https://reviews.llvm.org/D46207
Files:
lib/Support/Parallel.cpp
Index: lib/Support/Parallel.cpp
===================================================================
--- lib/Support/Parallel.cpp
+++ lib/Support/Parallel.cpp
@@ -23,7 +23,7 @@
class Executor {
public:
virtual ~Executor() = default;
- virtual void add(std::function<void()> func) = 0;
+ virtual void add(std::function<void()> F, parallel::detail::Latch *L) = 0;
static Executor *getDefaultExecutor();
};
@@ -91,9 +91,9 @@
// Wait for ~Latch.
}
- void add(std::function<void()> F) override {
+ void add(std::function<void()> F, parallel::detail::Latch *L) override {
std::unique_lock<std::mutex> Lock(Mutex);
- WorkStack.push(F);
+ WorkStack.push({std::move(F), L});
Lock.unlock();
Cond.notify_one();
}
@@ -105,16 +105,18 @@
Cond.wait(Lock, [&] { return Stop || !WorkStack.empty(); });
if (Stop)
break;
- auto Task = WorkStack.top();
+ auto Task = std::move(WorkStack.top());
WorkStack.pop();
Lock.unlock();
- Task();
+ Task.first();
+ Task.second->dec();
}
Done.dec();
}
std::atomic<bool> Stop{false};
- std::stack<std::function<void()>> WorkStack;
+ std::stack<std::pair<std::function<void()>, parallel::detail::Latch *>>
+ WorkStack;
std::mutex Mutex;
std::condition_variable Cond;
parallel::detail::Latch Done;
@@ -130,9 +132,6 @@
#if LLVM_ENABLE_THREADS
void parallel::detail::TaskGroup::spawn(std::function<void()> F) {
L.inc();
- Executor::getDefaultExecutor()->add([&, F] {
- F();
- L.dec();
- });
+ Executor::getDefaultExecutor()->add(std::move(F), &L);
}
#endif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46207.144386.patch
Type: text/x-patch
Size: 1631 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180427/0526e232/attachment.bin>
More information about the llvm-commits
mailing list