Skip to content

Commit df00624

Browse files
committed
Use parentEntity available in systems instead of storing it for each interface
Signed-off-by: Addisu Z. Taddese <addisu@openrobotics.org>
1 parent 00a6e47 commit df00624

File tree

3 files changed

+57
-114
lines changed

3 files changed

+57
-114
lines changed

src/SimulationRunner.cc

+3-6
Original file line numberDiff line numberDiff line change
@@ -571,12 +571,10 @@ void SimulationRunner::ProcessSystemQueue()
571571
this->postUpdateStartBarrier->Wait();
572572
if (this->postUpdateThreadsRunning)
573573
{
574-
system->PostUpdate(this->currentInfo,
575-
this->entityCompMgr);
574+
system->PostUpdate(this->currentInfo, this->entityCompMgr);
576575
}
577576
this->postUpdateStopBarrier->Wait();
578577
}
579-
580578
gzdbg << "Exiting postupdate worker thread ("
581579
<< id << ")" << std::endl;
582580
}));
@@ -604,13 +602,13 @@ void SimulationRunner::UpdateSystems()
604602
{
605603
GZ_PROFILE("PreUpdate");
606604
for (auto& system : this->systemMgr->SystemsPreUpdate())
607-
system.system->PreUpdate(this->currentInfo, this->entityCompMgr);
605+
system->PreUpdate(this->currentInfo, this->entityCompMgr);
608606
}
609607

610608
{
611609
GZ_PROFILE("Update");
612610
for (auto& system : this->systemMgr->SystemsUpdate())
613-
system.system->Update(this->currentInfo, this->entityCompMgr);
611+
system->Update(this->currentInfo, this->entityCompMgr);
614612
}
615613

616614
{
@@ -895,7 +893,6 @@ void SimulationRunner::Step(const UpdateInfo &_info)
895893
// Update all the systems.
896894
this->UpdateSystems();
897895

898-
899896
if (!this->Paused() && this->requestedRunToSimTime &&
900897
this->requestedRunToSimTime.value() > this->simTimeEpoch &&
901898
this->currentInfo.simTime >= this->requestedRunToSimTime.value())

src/SystemManager.cc

+42-68
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <list>
1919
#include <mutex>
2020
#include <set>
21-
#include <unordered_set>
2221

2322
#include <gz/common/StringUtils.hh>
2423

@@ -110,31 +109,23 @@ size_t SystemManager::ActivatePendingSystems()
110109
this->systems.push_back(system);
111110

112111
if (system.configure)
113-
this->systemsConfigure.emplace_back(
114-
system.parentEntity,
115-
system.configure);
112+
this->systemsConfigure.push_back(system.configure);
116113

117114
if (system.configureParameters)
118-
this->systemsConfigureParameters.emplace_back(
119-
system.parentEntity,
120-
system.configureParameters);
115+
this->systemsConfigureParameters.push_back(system.configureParameters);
121116

122117
if (system.reset)
123-
this->systemsReset.emplace_back(system.parentEntity,
124-
system.reset);
118+
this->systemsReset.push_back(system.reset);
125119

126120
if (system.preupdate)
127-
this->systemsPreupdate.emplace_back(system.parentEntity,
128-
system.preupdate);
121+
this->systemsPreupdate.push_back(system.preupdate);
129122

130123
if (system.update)
131-
this->systemsUpdate.emplace_back(system.parentEntity,
132-
system.update);
124+
this->systemsUpdate.push_back(system.update);
133125

134126
if (system.postupdate)
135127
{
136128
this->systemsPostupdate.push_back(system.postupdate);
137-
this->postUpdateParents.push_back(system.parentEntity);
138129
}
139130
}
140131

@@ -169,7 +160,6 @@ void SystemManager::Reset(const UpdateInfo &_info, EntityComponentManager &_ecm)
169160
this->systemsPreupdate.clear();
170161
this->systemsUpdate.clear();
171162
this->systemsPostupdate.clear();
172-
this->postUpdateParents.clear();
173163

174164
std::vector<PluginInfo> pluginsToBeLoaded;
175165

@@ -291,42 +281,37 @@ void SystemManager::AddSystemImpl(
291281
}
292282

293283
//////////////////////////////////////////////////
294-
const std::vector<SystemIfaceWithParent<ISystemConfigure>>&
295-
SystemManager::SystemsConfigure()
284+
const std::vector<ISystemConfigure *>& SystemManager::SystemsConfigure()
296285
{
297286
return this->systemsConfigure;
298287
}
299288

300-
const std::vector<SystemIfaceWithParent<ISystemConfigureParameters>>&
289+
const std::vector<ISystemConfigureParameters *>&
301290
SystemManager::SystemsConfigureParameters()
302291
{
303292
return this->systemsConfigureParameters;
304293
}
305294

306295
//////////////////////////////////////////////////
307-
const std::vector<SystemIfaceWithParent<ISystemReset>>&
308-
SystemManager::SystemsReset()
296+
const std::vector<ISystemReset *> &SystemManager::SystemsReset()
309297
{
310298
return this->systemsReset;
311299
}
312300

313301
//////////////////////////////////////////////////
314-
const std::vector<SystemIfaceWithParent<ISystemPreUpdate>>&
315-
SystemManager::SystemsPreUpdate()
302+
const std::vector<ISystemPreUpdate *>& SystemManager::SystemsPreUpdate()
316303
{
317304
return this->systemsPreupdate;
318305
}
319306

320307
//////////////////////////////////////////////////
321-
const std::vector<SystemIfaceWithParent<ISystemUpdate>>&
322-
SystemManager::SystemsUpdate()
308+
const std::vector<ISystemUpdate *>& SystemManager::SystemsUpdate()
323309
{
324310
return this->systemsUpdate;
325311
}
326312

327313
//////////////////////////////////////////////////
328-
const std::vector<ISystemPostUpdate *>&
329-
SystemManager::SystemsPostUpdate()
314+
const std::vector<ISystemPostUpdate *>& SystemManager::SystemsPostUpdate()
330315
{
331316
return this->systemsPostupdate;
332317
}
@@ -445,6 +430,13 @@ void RemoveFromVectorIf(std::vector<Tp>& vec,
445430
{
446431
vec.erase(std::remove_if(vec.begin(), vec.end(), pred), vec.end());
447432
}
433+
template <typename Tp>
434+
void removeFromVector(std::vector<Tp> &_vec, const Tp &_value)
435+
{
436+
_vec.erase(std::remove_if(_vec.begin(), _vec.end(),
437+
[&](const auto &_arg) { return _arg == _value; }),
438+
_vec.end());
439+
}
448440

449441
//////////////////////////////////////////////////
450442
void SystemManager::ProcessRemovedEntities(
@@ -460,58 +452,40 @@ void SystemManager::ProcessRemovedEntities(
460452
return;
461453
}
462454

463-
RemoveFromVectorIf(this->systemsReset,
464-
[&](const SystemIfaceWithParent<ISystemReset>& system) {
465-
return _ecm.IsMarkedForRemoval(system.parent);
466-
});
467-
RemoveFromVectorIf(this->systemsPreupdate,
468-
[&](const SystemIfaceWithParent<ISystemPreUpdate>& system) {
469-
return _ecm.IsMarkedForRemoval(system.parent);
470-
});
471-
RemoveFromVectorIf(this->systemsUpdate,
472-
[&](const SystemIfaceWithParent<ISystemUpdate>& system) {
473-
return _ecm.IsMarkedForRemoval(system.parent);
474-
});
455+
std::vector<SystemInternal> markedForRemoval;
456+
for (const auto &system: this->systems)
457+
{
458+
if (_ecm.IsMarkedForRemoval(system.parentEntity))
459+
{
460+
markedForRemoval.push_back(system);
461+
}
462+
}
475463

476-
std::unordered_set<ISystemPostUpdate *> markedForRemoval;
477-
for (std::size_t i = 0; i < this->systemsPostupdate.size(); i++)
464+
for (const auto &system: markedForRemoval)
478465
{
479-
if(_ecm.IsMarkedForRemoval(postUpdateParents[i]))
466+
removeFromVector(this->systemsReset, system.reset);
467+
removeFromVector(this->systemsPreupdate, system.preupdate);
468+
removeFromVector(this->systemsUpdate, system.update);
469+
470+
if (system.postupdate)
480471
{
481472
// If system with a PostUpdate is marked for removal
482473
// mark all worker threads for removal.
483474
_needsCleanUp = true;
484-
markedForRemoval.insert(this->systemsPostupdate[i]);
485475
}
486-
}
487476

488-
RemoveFromVectorIf(this->systemsPostupdate,
489-
[&](const auto system) {
490-
if (markedForRemoval.count(system)) {
491-
return true;
492-
}
493-
return false;
494-
});
477+
removeFromVector(this->systemsPostupdate, system.postupdate);
478+
removeFromVector(this->systemsConfigure, system.configure);
479+
removeFromVector(this->systemsConfigureParameters, system.configureParameters);
480+
RemoveFromVectorIf(this->systems,
481+
[&](const SystemInternal& _arg) {
482+
return _arg.parentEntity == system.parentEntity;
483+
});
484+
}
495485

496-
RemoveFromVectorIf(this->postUpdateParents,
497-
[&](const Entity& entity){
498-
return _ecm.IsMarkedForRemoval(entity);
499-
}
500-
);
501-
RemoveFromVectorIf(this->systemsConfigure,
502-
[&](const SystemIfaceWithParent<ISystemConfigure>& system) {
503-
return _ecm.IsMarkedForRemoval(system.parent);
504-
});
505-
RemoveFromVectorIf(this->systemsConfigureParameters,
506-
[&](const SystemIfaceWithParent<ISystemConfigureParameters>& system) {
507-
return _ecm.IsMarkedForRemoval(system.parent);
508-
});
509-
RemoveFromVectorIf(this->systems, [&](const SystemInternal& system) {
510-
return _ecm.IsMarkedForRemoval(system.parentEntity);
511-
});
512486
std::lock_guard lock(this->pendingSystemsMutex);
513487
RemoveFromVectorIf(this->pendingSystems,
514-
[&](const SystemInternal& system) {
515-
return _ecm.IsMarkedForRemoval(system.parentEntity);
488+
[&](const SystemInternal& _system) {
489+
return _ecm.IsMarkedForRemoval(_system.parentEntity);
516490
});
517491
}

src/SystemManager.hh

+12-40
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
#include <memory>
2323
#include <string>
24-
#include <unordered_map>
25-
#include <unordered_set>
2624
#include <vector>
2725

2826
#include <sdf/Plugin.hh>
@@ -43,21 +41,6 @@ namespace gz
4341
// Inline bracket to help doxygen filtering.
4442
inline namespace GZ_SIM_VERSION_NAMESPACE {
4543

46-
/// \brief Helper container to keep track of
47-
/// system interfaces and their parents
48-
template<typename IFace>
49-
struct SystemIfaceWithParent {
50-
/// Parent entity of system
51-
Entity parent;
52-
53-
/// Interface pointer
54-
IFace* system;
55-
56-
/// \brief constructor
57-
SystemIfaceWithParent(Entity _parent, IFace* _iface):
58-
parent(_parent), system(_iface) {}
59-
};
60-
6144
/// \brief Used to load / unload sysetms as well as iterate over them.
6245
class GZ_SIM_VISIBLE SystemManager
6346
{
@@ -131,35 +114,29 @@ namespace gz
131114

132115
/// \brief Get a vector of all systems implementing "Configure"
133116
/// \return Vector of systems' configure interfaces.
134-
public: const std::vector<SystemIfaceWithParent<ISystemConfigure>>&
135-
SystemsConfigure();
117+
public: const std::vector<ISystemConfigure *>& SystemsConfigure();
136118

137119
/// \brief Get an vector of all active systems implementing
138120
/// "ConfigureParameters"
139121
/// \return Vector of systems's configure interfaces.
140-
public: const std::vector<
141-
SystemIfaceWithParent<ISystemConfigureParameters>>&
142-
SystemsConfigureParameters();
122+
public: const std::vector<ISystemConfigureParameters *>&
123+
SystemsConfigureParameters();
143124

144125
/// \brief Get an vector of all active systems implementing "Reset"
145126
/// \return Vector of systems' reset interfaces.
146-
public: const std::vector<SystemIfaceWithParent<ISystemReset>>&
147-
SystemsReset();
127+
public: const std::vector<ISystemReset *>& SystemsReset();
148128

149129
/// \brief Get an vector of all active systems implementing "PreUpdate"
150130
/// \return Vector of systems's pre-update interfaces.
151-
public: const std::vector<SystemIfaceWithParent<ISystemPreUpdate>>&
152-
SystemsPreUpdate();
131+
public: const std::vector<ISystemPreUpdate *>& SystemsPreUpdate();
153132

154133
/// \brief Get an vector of all active systems implementing "Update"
155134
/// \return Vector of systems's update interfaces.
156-
public: const std::vector<SystemIfaceWithParent<ISystemUpdate>>&
157-
SystemsUpdate();
135+
public: const std::vector<ISystemUpdate *>& SystemsUpdate();
158136

159137
/// \brief Get an vector of all active systems implementing "PostUpdate"
160138
/// \return Vector of systems's post-update interfaces.
161-
public: const std::vector<ISystemPostUpdate *>&
162-
SystemsPostUpdate();
139+
public: const std::vector<ISystemPostUpdate *>& SystemsPostUpdate();
163140

164141
/// \brief Get an vector of all systems attached to a given entity.
165142
/// \return Vector of systems.
@@ -218,29 +195,24 @@ namespace gz
218195
private: mutable std::mutex pendingSystemsMutex;
219196

220197
/// \brief Systems implementing Configure
221-
private: std::vector<SystemIfaceWithParent<ISystemConfigure>>
222-
systemsConfigure;
198+
private: std::vector<ISystemConfigure *> systemsConfigure;
223199

224200
/// \brief Systems implementing ConfigureParameters
225-
private: std::vector<SystemIfaceWithParent<ISystemConfigureParameters>>
201+
private: std::vector<ISystemConfigureParameters *>
226202
systemsConfigureParameters;
227203

228204
/// \brief Systems implementing Reset
229-
private: std::vector<SystemIfaceWithParent<ISystemReset>> systemsReset;
205+
private: std::vector<ISystemReset *> systemsReset;
230206

231207
/// \brief Systems implementing PreUpdate
232-
private: std::vector<SystemIfaceWithParent<ISystemPreUpdate>>
233-
systemsPreupdate;
208+
private: std::vector<ISystemPreUpdate *> systemsPreupdate;
234209

235210
/// \brief Systems implementing Update
236-
private: std::vector<SystemIfaceWithParent<ISystemUpdate>> systemsUpdate;
211+
private: std::vector<ISystemUpdate *> systemsUpdate;
237212

238213
/// \brief Systems implementing PostUpdate
239214
private: std::vector<ISystemPostUpdate *> systemsPostupdate;
240215

241-
/// \brief Parents of post update systems.
242-
private: std::vector<Entity> postUpdateParents;
243-
244216
/// \brief System loader, for loading system plugins.
245217
private: SystemLoaderPtr systemLoader;
246218

0 commit comments

Comments
 (0)