Skip to content

Commit 5ce62a8

Browse files
authored
Update our usage of workerpools (#2995)
In `SimulationRunner`, we initialize a worker pool but we never actually use it so it's pure overhead. In `ServerPrivate` we only use the worker pool if there are multiple simulation runners, so we can optimize for the most common use case of one runner. Signed-off-by: Addisu Z. Taddese <[email protected]>
1 parent 0172411 commit 5ce62a8

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

src/ServerPrivate.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,22 @@ bool ServerPrivate::Run(const uint64_t _iterations,
198198
}
199199
else
200200
{
201+
if (!this->workerPool.has_value())
202+
{
203+
// Initialize the workerpool if we do have multiple simulation runners and
204+
// it hasn't been initialized before
205+
this->workerPool.emplace(2);
206+
}
201207
for (std::unique_ptr<SimulationRunner> &runner : this->simRunners)
202208
{
203-
this->workerPool.AddWork([&runner, &_iterations] ()
209+
this->workerPool->AddWork([&runner, &_iterations] ()
204210
{
205211
runner->Run(_iterations);
206212
});
207213
}
208214

209215
// Wait for the runner to complete.
210-
result = this->workerPool.WaitForResults();
216+
result = this->workerPool->WaitForResults();
211217
}
212218

213219
// See comments ServerPrivate::Stop() for why we lock this mutex here.

src/ServerPrivate.hh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ namespace gz
154154
const gz::msgs::ServerControl &_req, msgs::Boolean &_res);
155155

156156
/// \brief A pool of worker threads.
157-
public: common::WorkerPool workerPool{2};
157+
/// \note We use optional here since most of the time, there will be a
158+
/// single simulation runner and a workerpool is not needed. We will
159+
/// initialize the workerpool as necessary later on.
160+
public: std::optional<common::WorkerPool> workerPool;
158161

159162
/// \brief All the simulation runners.
160163
public: std::vector<std::unique_ptr<SimulationRunner>> simRunners;

src/SimulationRunner.hh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#include <sdf/World.hh>
4242

4343
#include <gz/common/Event.hh>
44-
#include <gz/common/WorkerPool.hh>
4544
#include <gz/math/Stopwatch.hh>
4645
#include <gz/transport/Node.hh>
4746

@@ -433,9 +432,6 @@ namespace gz
433432
/// \brief Manager of distributing/receiving network work.
434433
private: std::unique_ptr<NetworkManager> networkMgr{nullptr};
435434

436-
/// \brief A pool of worker threads.
437-
private: common::WorkerPool workerPool{2};
438-
439435
/// \brief Wall time of the previous update.
440436
private: std::chrono::steady_clock::time_point prevUpdateRealTime;
441437

0 commit comments

Comments
 (0)