Skip to content
Permalink
Browse files

run full Lua GC sweep after loading handlers

  • Loading branch information...
rtri
rtri committed Aug 31, 2019
1 parent 5fc162a commit cc3dd2a965af9b5034a27b1afbbd82c8ce5c0a83
@@ -315,7 +315,7 @@ void CGame::AddTimedJobs()
// SimFrame handles gc when not paused, this all other cases
// do not check the global synced state, never true in demos
if (luaGCControl == 1 || simFrameDeltaTime > gcForcedDeltaTime)
eventHandler.CollectGarbage();
eventHandler.CollectGarbage(false);

CInputReceiver::CollectGarbage();
return true;
@@ -791,6 +791,7 @@ void CGame::LoadFinalize()
if (saveFileHandler == nullptr) {
ENTER_SYNCED_CODE();
eventHandler.GamePreload();
eventHandler.CollectGarbage(true);
LEAVE_SYNCED_CODE();
}

@@ -1520,7 +1521,7 @@ void CGame::SimFrame() {
// keep garbage-collection rate tied to sim-speed
// (fixed 30Hz gc is not enough while catching up)
if (luaGCControl == 0)
eventHandler.CollectGarbage();
eventHandler.CollectGarbage(false);

eventHandler.GameFrame(gs->frameNum);
}
@@ -95,6 +95,7 @@ namespace {
return false; \
\
HandlerInstance = new HandlerType(); \
HandlerInstance->CollectGarbage(true); \
return (HandlerInstance->IsValid()); \
}

@@ -108,6 +109,7 @@ namespace {
return false; \
\
HandlerInstance = new HandlerType(onlySynced); \
HandlerInstance->CollectGarbage(true); \
return (HandlerInstance->IsValid()); \
}

@@ -432,10 +432,7 @@ bool CLuaHandle::RunCallInTraceback(lua_State* L, const LuaHashString& hs, int i

if (error == LUA_ERRMEM) {
// try to free some memory so other lua states can alloc again
for (int i = 0; i < 20; ++i) {
CollectGarbage();
}

CollectGarbage(true);
// kill the entire handle next frame
KillActiveHandle(L);
}
@@ -2500,12 +2497,12 @@ void CLuaHandle::DownloadProgress(int ID, long downloaded, long total)
/******************************************************************************/
/******************************************************************************/

void CLuaHandle::CollectGarbage()
void CLuaHandle::CollectGarbage(bool forced)
{
const float gcMemLoadMult = D.gcCtrl.baseMemLoadMult;
const float gcRunTimeMult = D.gcCtrl.baseRunTimeMult;

if (spring_lua_alloc_skip_gc(gcMemLoadMult))
if (!forced && spring_lua_alloc_skip_gc(gcMemLoadMult))
return;

LUA_CALL_IN_CHECK_NAMED(L, (GetLuaContextData(L)->synced)? "Lua::CollectGarbage::Synced": "Lua::CollectGarbage::Unsynced");
@@ -2531,7 +2528,7 @@ void CLuaHandle::CollectGarbage()
const spring_time endTime = startTime + spring_msecs(gcLoopRunTime);

// perform GC cycles until time runs out or iteration-limit is reached
while (gcItersInBatch < D.gcCtrl.itersPerBatch && spring_gettime() < endTime) {
while (forced || (gcItersInBatch < D.gcCtrl.itersPerBatch && spring_gettime() < endTime)) {
gcItersInBatch++;

if (!lua_gc(L_GC, LUA_GCSTEP, gcStepsPerIter))
@@ -259,7 +259,7 @@ class CLuaHandle : public CEventClient

//FIXME void MetalMapChanged(const int x, const int z);

void CollectGarbage() override;
void CollectGarbage(bool forced) override;

void DownloadQueued(int ID, const std::string& archiveName, const std::string& archiveType) override;
void DownloadStarted(int ID) override;
@@ -187,6 +187,10 @@ class CSplitLuaHandle
syncedLuaHandle.CheckStack();
unsyncedLuaHandle.CheckStack();
}
void CollectGarbage(bool forced) {
syncedLuaHandle.CollectGarbage(forced);
unsyncedLuaHandle.CollectGarbage(forced);
}

static CUnsyncedLuaHandle* GetUnsyncedHandle(lua_State* L) {
if (!CLuaHandle::GetHandleSynced(L))
@@ -95,7 +95,7 @@ bool CLuaMenuController::Draw()
// we should not become the active controller unless this holds (see ::Activate)
assert(luaMenu != nullptr);

eventHandler.CollectGarbage();
eventHandler.CollectGarbage(false);
infoConsole->PushNewLinesToEventHandler();
mouse->Update();
mouse->UpdateCursors();
@@ -351,7 +351,7 @@ class CEventClient
virtual void DrawLoadScreen();
virtual void LoadProgress(const std::string& msg, const bool replace_lastline);

virtual void CollectGarbage() {}
virtual void CollectGarbage(bool forced) {}
virtual void DbgTimingInfo(DbgTimingInfoType type, const spring_time start, const spring_time end) {}
virtual void Pong(uint8_t pingTag, const spring_time pktSendTime, const spring_time pktRecvTime) {}
virtual void MetalMapChanged(const int x, const int z) {}
@@ -555,9 +555,9 @@ void CEventHandler::UnitHarvestStorageFull(const CUnit* unit)
/******************************************************************************/
/******************************************************************************/

void CEventHandler::CollectGarbage()
void CEventHandler::CollectGarbage(bool forced)
{
ITERATE_EVENTCLIENTLIST_NA(CollectGarbage);
ITERATE_EVENTCLIENTLIST(CollectGarbage, forced);
}

void CEventHandler::DbgTimingInfo(DbgTimingInfoType type, const spring_time start, const spring_time end)
@@ -290,7 +290,7 @@ class CEventHandler
/// percentage when reconnecting to a running game
void GameProgress(int gameFrame);

void CollectGarbage();
void CollectGarbage(bool forced);
void DbgTimingInfo(DbgTimingInfoType type, const spring_time start, const spring_time end);
void Pong(uint8_t pingTag, const spring_time pktSendTime, const spring_time pktRecvTime);
void MetalMapChanged(const int x, const int z);

0 comments on commit cc3dd2a

Please sign in to comment.
You can’t perform that action at this time.