From 327de2a984171b187cd4e15972955c0ee82ad8e6 Mon Sep 17 00:00:00 2001 From: "Addisu Z. Taddese" Date: Wed, 23 Jul 2025 12:46:33 -0500 Subject: [PATCH] Update our usage of workerpools 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 --- src/ServerPrivate.cc | 10 ++++++++-- src/ServerPrivate.hh | 5 ++++- src/SimulationRunner.hh | 4 ---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/ServerPrivate.cc b/src/ServerPrivate.cc index bf5c1161b0..6d2682038a 100644 --- a/src/ServerPrivate.cc +++ b/src/ServerPrivate.cc @@ -198,16 +198,22 @@ bool ServerPrivate::Run(const uint64_t _iterations, } else { + if (!this->workerPool.has_value()) + { + // Initialize the workerpool if we do have multiple simulation runners and + // it hasn't been initialized before + this->workerPool.emplace(2); + } for (std::unique_ptr &runner : this->simRunners) { - this->workerPool.AddWork([&runner, &_iterations] () + this->workerPool->AddWork([&runner, &_iterations] () { runner->Run(_iterations); }); } // Wait for the runner to complete. - result = this->workerPool.WaitForResults(); + result = this->workerPool->WaitForResults(); } // See comments ServerPrivate::Stop() for why we lock this mutex here. diff --git a/src/ServerPrivate.hh b/src/ServerPrivate.hh index 6fe36f8b1f..8dd9930166 100644 --- a/src/ServerPrivate.hh +++ b/src/ServerPrivate.hh @@ -154,7 +154,10 @@ namespace gz const gz::msgs::ServerControl &_req, msgs::Boolean &_res); /// \brief A pool of worker threads. - public: common::WorkerPool workerPool{2}; + /// \note We use optional here since most of the time, there will be a + /// single simulation runner and a workerpool is not needed. We will + /// initialize the workerpool as necessary later on. + public: std::optional workerPool; /// \brief All the simulation runners. public: std::vector> simRunners; diff --git a/src/SimulationRunner.hh b/src/SimulationRunner.hh index 44dd1c7ff0..f97a40b7cd 100644 --- a/src/SimulationRunner.hh +++ b/src/SimulationRunner.hh @@ -41,7 +41,6 @@ #include #include -#include #include #include @@ -433,9 +432,6 @@ namespace gz /// \brief Manager of distributing/receiving network work. private: std::unique_ptr networkMgr{nullptr}; - /// \brief A pool of worker threads. - private: common::WorkerPool workerPool{2}; - /// \brief Wall time of the previous update. private: std::chrono::steady_clock::time_point prevUpdateRealTime;