Skip to content
Permalink
Browse files

Avoid the userdata cache for vectors and matrices

  • Loading branch information...
sbx320 committed Nov 1, 2019
1 parent 3184a77 commit 0d0d60c7df86413d03066903549c565083a716c4
@@ -118,10 +118,21 @@ void lua_pushuserdata(lua_State* luaVM, void* pData)
lua_pushobject(luaVM, NULL, pData);
}

void lua_pushobject(lua_State* luaVM, const char* szClass, void* pObject)
void lua_pushobject(lua_State* luaVM, const char* szClass, void* pObject, bool bSkipCache)
{
if (szClass)
if (szClass == nullptr)
{
lua_pushlightuserdata(luaVM, pObject);
return;
}

if (bSkipCache)
{
*(void**)lua_newuserdata(luaVM, sizeof(void*)) = pObject;
}
else
{
// Lookup the userdata in the cache table
lua_pushstring(luaVM, "ud");
lua_rawget(luaVM, LUA_REGISTRYINDEX);

@@ -146,40 +157,38 @@ void lua_pushobject(lua_State* luaVM, const char* szClass, void* pObject)

// userdata is already on the stack, just remove the table
lua_remove(luaVM, -2);

// Assign the class metatable
lua_getclass(luaVM, szClass);
lua_setmetatable(luaVM, -2); // element
return;
}
lua_pushlightuserdata(luaVM, pObject);

// Assign the class metatable
lua_getclass(luaVM, szClass);
lua_setmetatable(luaVM, -2); // element
}

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_pushobject(luaVM, "Vector4", (void*)reinterpret_cast<unsigned int*>(pVector->GetScriptID()), true);
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_pushobject(luaVM, "Vector3", (void*)reinterpret_cast<unsigned int*>(pVector->GetScriptID()), true);
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_pushobject(luaVM, "Vector2", (void*)reinterpret_cast<unsigned int*>(pVector->GetScriptID()), true);
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_pushobject(luaVM, "Matrix", (void*)reinterpret_cast<unsigned int*>(pMatrix->GetScriptID()), true);
lua_addtotalbytes(luaVM, LUA_GC_EXTRA_BYTES);
}

@@ -56,7 +56,7 @@ void lua_pushtimer(lua_State* luaVM, CLuaTimer* pElement);
void lua_pushxmlnode(lua_State* luaVM, CXMLNode* pElement);
void lua_pushuserdata(lua_State* luaVM, void* pData);

void lua_pushobject(lua_State* luaVM, const char* szClass, void* pObject);
void lua_pushobject(lua_State* luaVM, const char* szClass, void* pObject, bool bSkipCache = false);

void lua_pushvector(lua_State* luaVM, const CVector4D& vector);
void lua_pushvector(lua_State* luaVM, const CVector& vector);
@@ -200,10 +200,21 @@ void lua_pushuserdata(lua_State* luaVM, void* pData)
lua_pushobject(luaVM, NULL, pData);
}

void lua_pushobject(lua_State* luaVM, const char* szClass, void* pObject)
void lua_pushobject(lua_State* luaVM, const char* szClass, void* pObject, bool bSkipCache)
{
if (szClass)
if (szClass == nullptr)
{
lua_pushlightuserdata(luaVM, pObject);
return;
}

if (bSkipCache)
{
*(void**)lua_newuserdata(luaVM, sizeof(void*)) = pObject;
}
else
{
// Lookup the userdata in the cache table
lua_pushstring(luaVM, "ud");
lua_rawget(luaVM, LUA_REGISTRYINDEX);

@@ -228,40 +239,38 @@ void lua_pushobject(lua_State* luaVM, const char* szClass, void* pObject)

// userdata is already on the stack, just remove the table
lua_remove(luaVM, -2);

// Assign the class metatable
lua_getclass(luaVM, szClass);
lua_setmetatable(luaVM, -2); // element
return;
}
lua_pushlightuserdata(luaVM, pObject);

// Assign the class metatable
lua_getclass(luaVM, szClass);
lua_setmetatable(luaVM, -2); // element
}

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_pushobject(luaVM, "Vector4", (void*)reinterpret_cast<unsigned int*>(pVector->GetScriptID()), true);
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_pushobject(luaVM, "Vector3", (void*)reinterpret_cast<unsigned int*>(pVector->GetScriptID()), true);
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_pushobject(luaVM, "Vector2", (void*)reinterpret_cast<unsigned int*>(pVector->GetScriptID()), true);
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_pushobject(luaVM, "Matrix", (void*)reinterpret_cast<unsigned int*>(pMatrix->GetScriptID()), true);
lua_addtotalbytes(luaVM, LUA_GC_EXTRA_BYTES);
}

@@ -38,7 +38,7 @@ void lua_pushxmlnode(lua_State* luaVM, class CXMLNode* pNode);
void lua_pushban(lua_State* luaVM, class CBan* pBan);
void lua_pushquery(lua_State* luaVM, class CDbJobData* pJobData);
void lua_pushuserdata(lua_State* luaVM, void* value);
void lua_pushobject(lua_State* luaVM, const char* szClass, void* pObject);
void lua_pushobject(lua_State* luaVM, const char* szClass, void* pObject, bool bSkipCache = false);

void lua_pushvector(lua_State* luaVM, const CVector2D& vector);
void lua_pushvector(lua_State* luaVM, const CVector& vector);
@@ -93,4 +93,4 @@ struct SLuaDebugInfo
SString strShortSrc;
int iLine;
int infoType;
};
};

1 comment on commit 0d0d60c

@sbx320

This comment has been minimized.

Copy link
Member Author

sbx320 commented on 0d0d60c Nov 1, 2019

As lua_pushvector always allocates a new CLuaVector, the userdata cache lookup will never succeed, so we can just skip this step entirely for some significant performance gains (~20% from my testing).

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