lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

We are using Lua 5.02 in our game engine, and although we had no problems on previous projects, the garbage collection now seems to be a bottleneck (noticable pause in the main loop when the GC executes).

The problem stems from my creation of some temporary global variables inside each tick of the main loop.

Inside our main loop, in C++, I am checking the value of a few lua variables/expressions...

    if (LuaGetBool(L, "x == 5", FALSE)) {
    if (LuaGetBool(L, "gSomeGlobalBoolean", FALSE)) {

where LuaGetBool is basically this...

BOOL LuaGetBool(lua_State *L, const CHAR *pszExpression, BOOL bDefault)
    BOOL b;

    MyDoString(BuildString("_tmp = %s", pszExpression)); // BuildString is just a fancy sprintf() that returns the string
    int nType;   

    lua_getglobal(L, "_tmp");
    nType = lua_type(L, -1);

    b = bDefault;
    if (nType == LUA_TBOOLEAN) {
        b = (lua_toboolean(L, -1) != 0);

    MyDoString("_tmp = nil");


I also have nearly identical functions:  LuaGetFloat(), LuaGetInt(), LuaGetString()...

As you can see, I am creating a temporary global variable _tmp to hold the value of the _expression_.  I understand that the code for dostring will do some temp allocations as well.  The eventual collection of these things takes so much time that the frame rate suffers (there is a noticable pause every 1 to 4 seconds).

I know that I could avoid the use of _tmp when getting the value of an actual global variable (like in the case LuaGetBool(L, "gSomeGlobalBoolean")) by using lua_getglobal(),  but there must a better way to generically get the value of an _expression_ (as well as members of tables) ?

Although it seems that the incremental GC of Lua v5.1 would probably solve this problem, upgrading is a last resort...I am looking for solutions that don't involve upgrading.

Thanks, in advance.