Index: simulation2/Simulation2.h =================================================================== --- simulation2/Simulation2.h +++ simulation2/Simulation2.h @@ -226,6 +226,8 @@ const CSimContext& GetSimContext() const; ScriptInterface& GetScriptInterface() const; + void MaybeIncrementalGC(const double timeBudgetMilliseconds); + bool ComputeStateHash(std::string& outHash, bool quick); bool DumpDebugState(std::ostream& stream); bool SerializeState(std::ostream& stream); Index: simulation2/Simulation2.cpp =================================================================== --- simulation2/Simulation2.cpp +++ simulation2/Simulation2.cpp @@ -363,8 +363,6 @@ PROFILE3("sim update"); PROFILE2_ATTR("turn %d", (int)m_TurnNumber); - const double turnRealLengthStartSeconds = timer_Time(); - fixed turnLengthFixed = fixed::FromInt(turnLength) / 1000; /* @@ -494,17 +492,6 @@ } } - // Calculate how much time we have this thread idle for before the next turn is scheduled to start - - // (TODO: we ought to schedule this for a frame where we're not - // running the sim update, to spread the load) - - // Convert seconds to milliseconds. - const double turnRealLengthMilliseconds = (timer_Time() - turnRealLengthStartSeconds) * 1000; - // Reserve 1 ms for overhead, may be insufficent / unneeded. - const double timeBudgetMilliseconds = turnLength - 1.0 - turnRealLengthMilliseconds; - scriptInterface.GetContext()->MaybeIncrementalGC(timeBudgetMilliseconds); - if (m_EnableOOSLog) DumpState(); @@ -992,3 +979,9 @@ return Script::StringifyJSON(rq, &ais); } + +void CSimulation2::MaybeIncrementalGC(const double timeBudgetMilliseconds) +{ + const ScriptInterface& scriptInterface = GetScriptInterface(); + scriptInterface.GetContext()->MaybeIncrementalGC(timeBudgetMilliseconds); +} Index: simulation2/system/TurnManager.cpp =================================================================== --- simulation2/system/TurnManager.cpp +++ simulation2/system/TurnManager.cpp @@ -108,6 +108,8 @@ if (m_ReadyTurn <= m_CurrentTurn && m_CommandDelay > 1) break; + const double turnRealLengthStartSeconds = timer_Time(); + // To avoid confusing the profiler, we need to trigger the new turn // while we're not nested inside any PROFILE blocks g_Profiler.Turn(); @@ -149,6 +151,18 @@ // Set the time for the next turn update m_DeltaSimTime -= m_TurnLength / 1000.f; + + // Calculate how much time we have this thread idle for before the next turn is scheduled to start + + // TODO: Maybe we ought to schedule this for a frame where we're not running the sim update to spread the load. + // This favors animation frame rate over preventing simulation lag. + + // Convert seconds to milliseconds. + const double turnRealLengthMilliseconds = (timer_Time() - turnRealLengthStartSeconds) * 1000; + // Reserve 1 ms for overhead, may be insufficent / unneeded. + const double timeBudgetMilliseconds = m_TurnLength - 1.0 - turnRealLengthMilliseconds; + m_Simulation2.MaybeIncrementalGC(timeBudgetMilliseconds); + } return true;