Skip to content

Commit cdd1a76

Browse files
committed
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]> (cherry picked from commit 5ce62a8)
1 parent aab29bc commit cdd1a76

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
@@ -200,16 +200,22 @@ bool ServerPrivate::Run(const uint64_t _iterations,
200200
}
201201
else
202202
{
203+
if (!this->workerPool.has_value())
204+
{
205+
// Initialize the workerpool if we do have multiple simulation runners and
206+
// it hasn't been initialized before
207+
this->workerPool.emplace(2);
208+
}
203209
for (std::unique_ptr<SimulationRunner> &runner : this->simRunners)
204210
{
205-
this->workerPool.AddWork([&runner, &_iterations] ()
211+
this->workerPool->AddWork([&runner, &_iterations] ()
206212
{
207213
runner->Run(_iterations);
208214
});
209215
}
210216

211217
// Wait for the runner to complete.
212-
result = this->workerPool.WaitForResults();
218+
result = this->workerPool->WaitForResults();
213219
}
214220

215221
// 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

@@ -427,9 +426,6 @@ namespace gz
427426
/// \brief Manager of distributing/receiving network work.
428427
private: std::unique_ptr<NetworkManager> networkMgr{nullptr};
429428

430-
/// \brief A pool of worker threads.
431-
private: common::WorkerPool workerPool{2};
432-
433429
/// \brief Wall time of the previous update.
434430
private: std::chrono::steady_clock::time_point prevUpdateRealTime;
435431

0 commit comments

Comments
 (0)