From 6f3fe6d827b8eed34db7bd06a796849731abbbfb Mon Sep 17 00:00:00 2001 From: ccw808 Date: Thu, 5 Jan 2017 00:28:04 +0000 Subject: [PATCH] Added hack to make lua GC more aggressive when using OOP Matrix & Vector --- Client/mods/deathmatch/logic/lua/LuaCommon.cpp | 4 ++++ Server/mods/deathmatch/logic/lua/LuaCommon.cpp | 4 ++++ Shared/mods/deathmatch/logic/lua/CLuaShared.h | 2 ++ .../mods/deathmatch/logic/luadefs/CLuaMatrixDefs.cpp | 1 + .../mods/deathmatch/logic/luadefs/CLuaVector2Defs.cpp | 2 +- .../mods/deathmatch/logic/luadefs/CLuaVector3Defs.cpp | 2 +- .../mods/deathmatch/logic/luadefs/CLuaVector4Defs.cpp | 2 +- vendor/lua/src/lapi.c | 10 ++++++++-- vendor/lua/src/lua.h | 2 +- 9 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Client/mods/deathmatch/logic/lua/LuaCommon.cpp b/Client/mods/deathmatch/logic/lua/LuaCommon.cpp index 3f5aa84c06..7a2ca3e33e 100644 --- a/Client/mods/deathmatch/logic/lua/LuaCommon.cpp +++ b/Client/mods/deathmatch/logic/lua/LuaCommon.cpp @@ -163,24 +163,28 @@ void lua_pushvector ( lua_State* luaVM, const CVector4D& vector ) { CLuaVector4D* pVector = new CLuaVector4D ( vector ); lua_pushobject ( luaVM, "Vector4", ( void* ) reinterpret_cast < unsigned int * > ( pVector->GetScriptID () ) ); + lua_addtotalbytes(luaVM, LUA_GC_EXTRA_BYTES); } void lua_pushvector ( lua_State* luaVM, const CVector& vector ) { CLuaVector3D* pVector = new CLuaVector3D ( vector ); lua_pushobject ( luaVM, "Vector3", ( void* ) reinterpret_cast < unsigned int * > ( pVector->GetScriptID () ) ); + lua_addtotalbytes(luaVM, LUA_GC_EXTRA_BYTES); } void lua_pushvector ( lua_State* luaVM, const CVector2D& vector ) { CLuaVector2D* pVector = new CLuaVector2D ( vector ); lua_pushobject ( luaVM, "Vector2", ( void* ) reinterpret_cast < unsigned int * > ( pVector->GetScriptID () ) ); + lua_addtotalbytes(luaVM, LUA_GC_EXTRA_BYTES); } void lua_pushmatrix ( lua_State* luaVM, const CMatrix& matrix ) { CLuaMatrix* pMatrix = new CLuaMatrix ( matrix ); lua_pushobject ( luaVM, "Matrix", ( void* ) reinterpret_cast < unsigned int * > ( pMatrix->GetScriptID () ) ); + lua_addtotalbytes(luaVM, LUA_GC_EXTRA_BYTES); } // Just do a type check vs LUA_TNONE before calling this, or bant diff --git a/Server/mods/deathmatch/logic/lua/LuaCommon.cpp b/Server/mods/deathmatch/logic/lua/LuaCommon.cpp index fa70eccb39..2e3e157324 100644 --- a/Server/mods/deathmatch/logic/lua/LuaCommon.cpp +++ b/Server/mods/deathmatch/logic/lua/LuaCommon.cpp @@ -254,24 +254,28 @@ void lua_pushvector ( lua_State* luaVM, const CVector4D& vector ) { CLuaVector4D* pVector = new CLuaVector4D ( vector ); lua_pushobject ( luaVM, "Vector4", ( void* ) reinterpret_cast < unsigned int * > ( pVector->GetScriptID () ) ); + lua_addtotalbytes(luaVM, LUA_GC_EXTRA_BYTES); } void lua_pushvector ( lua_State* luaVM, const CVector& vector ) { CLuaVector3D* pVector = new CLuaVector3D ( vector ); lua_pushobject ( luaVM, "Vector3", ( void* ) reinterpret_cast < unsigned int * > ( pVector->GetScriptID () ) ); + lua_addtotalbytes(luaVM, LUA_GC_EXTRA_BYTES); } void lua_pushvector ( lua_State* luaVM, const CVector2D& vector ) { CLuaVector2D* pVector = new CLuaVector2D ( vector ); lua_pushobject ( luaVM, "Vector2", ( void* ) reinterpret_cast < unsigned int * > ( pVector->GetScriptID () ) ); + lua_addtotalbytes(luaVM, LUA_GC_EXTRA_BYTES); } void lua_pushmatrix ( lua_State* luaVM, const CMatrix& matrix ) { CLuaMatrix* pMatrix = new CLuaMatrix ( matrix ); lua_pushobject ( luaVM, "Matrix", ( void* ) reinterpret_cast < unsigned int * > ( pMatrix->GetScriptID () ) ); + lua_addtotalbytes(luaVM, LUA_GC_EXTRA_BYTES); } // Just do a type check vs LUA_TNONE before calling this, or bant diff --git a/Shared/mods/deathmatch/logic/lua/CLuaShared.h b/Shared/mods/deathmatch/logic/lua/CLuaShared.h index 8733a9b049..0bd7790fc4 100644 --- a/Shared/mods/deathmatch/logic/lua/CLuaShared.h +++ b/Shared/mods/deathmatch/logic/lua/CLuaShared.h @@ -7,6 +7,8 @@ *****************************************************************************/ #pragma once +#define LUA_GC_EXTRA_BYTES 30 // Used in hack to make lua GC more aggressive when using OOP Matrix & Vector + // Lua function definitions (shared) #include "luadefs/CLuaBitDefs.h" #include "luadefs/CLuaCryptDefs.h" diff --git a/Shared/mods/deathmatch/logic/luadefs/CLuaMatrixDefs.cpp b/Shared/mods/deathmatch/logic/luadefs/CLuaMatrixDefs.cpp index 78ea76730c..95a37ea883 100644 --- a/Shared/mods/deathmatch/logic/luadefs/CLuaMatrixDefs.cpp +++ b/Shared/mods/deathmatch/logic/luadefs/CLuaMatrixDefs.cpp @@ -126,6 +126,7 @@ int CLuaMatrixDefs::Destroy ( lua_State* luaVM ) if ( !argStream.HasErrors () ) { delete pMatrix; + lua_addtotalbytes(luaVM, -LUA_GC_EXTRA_BYTES); lua_pushboolean ( luaVM, true ); return 1; } diff --git a/Shared/mods/deathmatch/logic/luadefs/CLuaVector2Defs.cpp b/Shared/mods/deathmatch/logic/luadefs/CLuaVector2Defs.cpp index 1fa741e38c..f620df543f 100644 --- a/Shared/mods/deathmatch/logic/luadefs/CLuaVector2Defs.cpp +++ b/Shared/mods/deathmatch/logic/luadefs/CLuaVector2Defs.cpp @@ -143,7 +143,7 @@ int CLuaVector2Defs::Destroy ( lua_State* luaVM ) if ( !argStream.HasErrors () ) { delete pVector; - + lua_addtotalbytes(luaVM, -LUA_GC_EXTRA_BYTES); lua_pushboolean ( luaVM, true ); return 1; } diff --git a/Shared/mods/deathmatch/logic/luadefs/CLuaVector3Defs.cpp b/Shared/mods/deathmatch/logic/luadefs/CLuaVector3Defs.cpp index 565c28f607..f3ce25b3fa 100644 --- a/Shared/mods/deathmatch/logic/luadefs/CLuaVector3Defs.cpp +++ b/Shared/mods/deathmatch/logic/luadefs/CLuaVector3Defs.cpp @@ -168,7 +168,7 @@ int CLuaVector3Defs::Destroy ( lua_State* luaVM ) if ( !argStream.HasErrors () ) { delete pVector; - + lua_addtotalbytes(luaVM, -LUA_GC_EXTRA_BYTES); lua_pushboolean ( luaVM, true ); return 1; } diff --git a/Shared/mods/deathmatch/logic/luadefs/CLuaVector4Defs.cpp b/Shared/mods/deathmatch/logic/luadefs/CLuaVector4Defs.cpp index 465354ba02..18b16e600a 100644 --- a/Shared/mods/deathmatch/logic/luadefs/CLuaVector4Defs.cpp +++ b/Shared/mods/deathmatch/logic/luadefs/CLuaVector4Defs.cpp @@ -189,7 +189,7 @@ int CLuaVector4Defs::Destroy ( lua_State* luaVM ) if ( !argStream.HasErrors () ) { delete pVector; - + lua_addtotalbytes(luaVM, -LUA_GC_EXTRA_BYTES); lua_pushboolean ( luaVM, true ); return 1; } diff --git a/vendor/lua/src/lapi.c b/vendor/lua/src/lapi.c index a48739e2f4..88b1a8ac6a 100644 --- a/vendor/lua/src/lapi.c +++ b/vendor/lua/src/lapi.c @@ -551,7 +551,6 @@ LUA_API int lua_pushthread (lua_State *L) { } - /* ** get functions (Lua -> stack) */ @@ -898,7 +897,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, } -#ifdef WITH_STRING_DUMP +#ifdef WITH_STRING_DUMP LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) { int status; TValue *o; @@ -1128,3 +1127,10 @@ LUA_API int luaX_is_apicheck_enabled() return 0; } #endif + +// MTA addition to tweak GC behaviour +LUA_API void lua_addtotalbytes(lua_State *L, int n) +{ + global_State *g = G(L); + g->totalbytes += n; +} diff --git a/vendor/lua/src/lua.h b/vendor/lua/src/lua.h index e4c7117b57..8223e5792d 100644 --- a/vendor/lua/src/lua.h +++ b/vendor/lua/src/lua.h @@ -186,7 +186,6 @@ LUA_API void (lua_pushboolean) (lua_State *L, int b); LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); LUA_API int (lua_pushthread) (lua_State *L); - /* ** get functions (Lua -> stack) */ @@ -357,6 +356,7 @@ LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count); LUA_API lua_Hook lua_gethook (lua_State *L); LUA_API int lua_gethookmask (lua_State *L); LUA_API int lua_gethookcount (lua_State *L); +LUA_API void lua_addtotalbytes(lua_State *L, int n); struct lua_Debug {