From 2993671fa47ae6875406319773a6f6a9db3901f3 Mon Sep 17 00:00:00 2001 From: James Turner Date: Thu, 24 Mar 2022 16:06:09 +0000 Subject: [PATCH] Subsystems: only record timings if enabled Avoid some overhead in copying std::map<>, in the common case where subsystem timing reporting is not enabled. --- simgear/structure/subsystem_mgr.cxx | 29 +++++++++++++++++++++++++++-- simgear/structure/subsystem_mgr.hxx | 5 ++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/simgear/structure/subsystem_mgr.cxx b/simgear/structure/subsystem_mgr.cxx index 8c9e7624..3cfa05e6 100644 --- a/simgear/structure/subsystem_mgr.cxx +++ b/simgear/structure/subsystem_mgr.cxx @@ -416,6 +416,30 @@ SGSubsystemGroup::update (double delta_time_sec) } const bool recordTime = (reportTimingCb != nullptr); + if (recordTime) { + // recording timing adds some overhead at present, this is + // an easy way to ensure in the case where recording is not actually + // enabled, we don't pay that cost. + // Longer-term solution: add timing information to the Member struct, + // so that it can be recorded without the TimerStats strucutres being + // copied/created/cleared each update() + updateMembersWithTiming(loopCount, delta_time_sec); + } else { + updateMembers(loopCount, delta_time_sec); + } +} + +void SGSubsystemGroup::updateMembers(int loopCount, double delta_time_sec) +{ + while (loopCount-- > 0) { + for (auto member : _members) { + member->update(delta_time_sec); // indirect call + } + } // of multiple update loop +} + +void SGSubsystemGroup::updateMembersWithTiming(int loopCount, double delta_time_sec) +{ SGTimeStamp timeStamp; TimerStats lvTimerStats(_timerStats); TimerStats overrunItems; @@ -435,7 +459,7 @@ SGSubsystemGroup::update (double delta_time_sec) if (member->name.size()) _timerStats[member->name] += timeStamp.elapsedMSec() / 1000.0; - if (recordTime && reportTimingCb) { + if (reportTimingCb) { member->updateExecutionTime(timeStamp.elapsedMSec()*1000); if (timeStamp.elapsedMSec() > SGSubsystemMgr::maxTimePerFrame_ms) { overrunItems[member->name] += timeStamp.elapsedMSec(); @@ -444,6 +468,7 @@ SGSubsystemGroup::update (double delta_time_sec) } } } // of multiple update loop + _lastExecutionTime = _executionTime; _executionTime += outerTimeStamp.elapsedMSec(); if (overrun) { @@ -493,8 +518,8 @@ SGSubsystemGroup::update (double delta_time_sec) } _lastTimerStats.clear(); _lastTimerStats.insert(_timerStats.begin(), _timerStats.end()); - } + void SGSubsystem::reportTimingStats(TimerStats *__lastValues) { std::string _name = ""; diff --git a/simgear/structure/subsystem_mgr.hxx b/simgear/structure/subsystem_mgr.hxx index a8e99f69..877e4701 100644 --- a/simgear/structure/subsystem_mgr.hxx +++ b/simgear/structure/subsystem_mgr.hxx @@ -445,7 +445,10 @@ private: void notifyWillChange(SGSubsystem* sub, SGSubsystem::State s); void notifyDidChange(SGSubsystem* sub, SGSubsystem::State s); - + + void updateMembers(int loopCount, double dt); + void updateMembersWithTiming(int loopCount, double dt); + friend class SGSubsystemMgr; void set_manager(SGSubsystemMgr* manager);