Skip to content

Commit 745a2d5

Browse files
committed
Re-enabled multithreaded lua GC, it seems faster
1 parent 828bc66 commit 745a2d5

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

Source/Managers/LuaMan.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ void LuaStateWrapper::Initialize() {
3333
luabind::open(m_State);
3434
tracy::LuaRegister(m_State);
3535

36-
// Disable gc. We do this manually, so we can thread it to occur parallel with non-lua updates
37-
// Not doing this for now... see StartAsyncGarbageCollection()
38-
// lua_gc(m_State, LUA_GCSTOP, 0);
36+
if (!g_SettingsMan.LuaMultithreadedGarbageCollectionDisabled())
37+
{
38+
// Disable gc. We do this manually, so we can thread it to occur parallel with non-lua updates
39+
// See LuaMan::StartAsyncGarbageCollection()
40+
lua_gc(m_State, LUA_GCSTOP, 0);
41+
}
3942

4043
const luaL_Reg libsToLoad[] = {
4144
// Basic Lua libraries
@@ -1279,10 +1282,10 @@ void LuaMan::Update() {
12791282
void LuaMan::StartAsyncGarbageCollection() {
12801283
ZoneScoped;
12811284

1282-
// For now we're not doing this... because it's slower than normal (blocking) GC collection during the update
1283-
// This is because Lua is trash and basically GCSTEP is meaningless and can cause memory leak runaway, whereas GCCOLLECT is ultra-expensive
1284-
// So for now we do normal GC collection :(
1285-
return;
1285+
if (g_SettingsMan.LuaMultithreadedGarbageCollectionDisabled())
1286+
{
1287+
return;
1288+
}
12861289

12871290
std::vector<LuaStateWrapper*> allStates;
12881291
allStates.reserve(m_ScriptStates.size() + 1);
@@ -1298,7 +1301,7 @@ void LuaMan::StartAsyncGarbageCollection() {
12981301
g_ThreadMan.GetPriorityThreadPool().submit([luaState]() {
12991302
ZoneScopedN("Lua Garbage Collection");
13001303
std::lock_guard<std::recursive_mutex> lock(luaState->GetMutex());
1301-
lua_gc(luaState->GetLuaState(), LUA_GCCOLLECT, 0); // we'd use GCSTEP but fuck lua it's trash
1304+
lua_gc(luaState->GetLuaState(), LUA_GCCOLLECT, 0);
13021305
lua_gc(luaState->GetLuaState(), LUA_GCSTOP, 0);
13031306
}));
13041307
}

Source/Managers/SettingsMan.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ void SettingsMan::Clear() {
4949
m_DisableFactionBuyMenuThemeCursors = false;
5050
m_PathFinderGridNodeSize = SCENEGRIDSIZE;
5151
m_AIUpdateInterval = 2;
52-
5352
m_NumberOfLuaStatesOverride = -1;
5453
m_ForceImmediatePathingRequestCompletion = false;
54+
m_LuaMultithreadedGarbageCollectionDisabled = false;
5555

5656
m_SkipIntro = false;
5757
m_ShowToolTips = true;
@@ -168,6 +168,7 @@ int SettingsMan::ReadProperty(const std::string_view& propName, Reader& reader)
168168
MatchProperty("AIUpdateInterval", { reader >> m_AIUpdateInterval; });
169169
MatchProperty("NumberOfLuaStatesOverride", { reader >> m_NumberOfLuaStatesOverride; });
170170
MatchProperty("ForceImmediatePathingRequestCompletion", { reader >> m_ForceImmediatePathingRequestCompletion; });
171+
MatchProperty("LuaMultithreadedGarbageCollectionDisabled", { reader >> m_LuaMultithreadedGarbageCollectionDisabled; });
171172
MatchProperty("EnableParticleSettling", { reader >> g_MovableMan.m_SettlingEnabled; });
172173
MatchProperty("EnableMOSubtraction", { reader >> g_MovableMan.m_MOSubtractionEnabled; });
173174
MatchProperty("DeltaTime", { g_TimerMan.SetDeltaTimeSecs(std::stof(reader.ReadPropValue())); });
@@ -294,6 +295,7 @@ int SettingsMan::Save(Writer& writer) const {
294295
writer.NewPropertyWithValue("PathFinderGridNodeSize", m_PathFinderGridNodeSize);
295296
writer.NewPropertyWithValue("AIUpdateInterval", m_AIUpdateInterval);
296297
writer.NewPropertyWithValue("NumberOfLuaStatesOverride", m_NumberOfLuaStatesOverride);
298+
writer.NewPropertyWithValue("LuaMultithreadedGarbageCollectionDisabled", m_LuaMultithreadedGarbageCollectionDisabled);
297299
writer.NewPropertyWithValue("ForceImmediatePathingRequestCompletion", m_ForceImmediatePathingRequestCompletion);
298300
writer.NewPropertyWithValue("EnableParticleSettling", g_MovableMan.m_SettlingEnabled);
299301
writer.NewPropertyWithValue("EnableMOSubtraction", g_MovableMan.m_MOSubtractionEnabled);

Source/Managers/SettingsMan.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ namespace RTE {
104104
/// Gets whether pathing requests will be forced to immediately complete for the next frame, or if they can take multiple frames to calculate.
105105
/// @return Whether pathing requests will be forced to immediately complete for the next frame
106106
bool GetForceImmediatePathingRequestCompletion() const { return m_ForceImmediatePathingRequestCompletion; }
107+
108+
/// Gets whether Lua multithreaded garbage collection is disabled.
109+
/// @return Whether Lua multithreaded garbage collection is disabled.
110+
bool LuaMultithreadedGarbageCollectionDisabled() const { return m_LuaMultithreadedGarbageCollectionDisabled; }
107111
#pragma endregion
108112

109113
#pragma region Gameplay Settings
@@ -398,6 +402,7 @@ namespace RTE {
398402
int m_AIUpdateInterval; //!< How often actor's AI should be updated, i.e. every n simulation updates.
399403
int m_NumberOfLuaStatesOverride; //!< Overrides how many threaded Lua states we'll use. -1 for no override, which defaults to the maximum number of concurrent hardware threads.
400404
bool m_ForceImmediatePathingRequestCompletion; //!< Whether pathing requests will be forced to immediately complete for the next frame, or if they can take multiple frames to calculate.
405+
bool m_LuaMultithreadedGarbageCollectionDisabled; //!< Whether Lua multithreaded garbage collection is disabled.
401406

402407
bool m_SkipIntro; //!< Whether to play the intro of the game or skip directly to the main menu.
403408
bool m_ShowToolTips; //!< Whether ToolTips are enabled or not.

0 commit comments

Comments
 (0)