Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up element data #912

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
80f24d5
Optimized setElementData on the server-side, but its throwing errors …
Pirulax May 1, 2019
2033eb8
Implement CFastHashMap to server-side, fix errors
Pirulax May 2, 2019
e0c640c
Remove MapInsert function from CFastHashMap
Pirulax May 2, 2019
07229c7
fix license in CCustomData.cpp
Pirulax May 2, 2019
9074533
Fix indentation
Pirulax May 2, 2019
a995ca2
Fix indentation
Pirulax May 2, 2019
8ab5fab
fix license in CCustomData.cpp finally
Pirulax May 2, 2019
788f003
Change OldVariable to oldVariable as suggested by Qaisjp
Pirulax May 2, 2019
17dd9d4
Implement faster element data on client-side as well(there are still …
Pirulax May 2, 2019
9f829fd
remove unnecesarry conversion from SString to SStringX
Pirulax May 2, 2019
d612446
Change SString strName to const char* szName because of crashes(it wa…
Pirulax May 2, 2019
3bf5dbd
Update
Pirulax May 2, 2019
0e1373f
changes made + inlined Map functions, hope i didnt broke anything
Pirulax Aug 12, 2019
a9782d9
changed comment in CRPCFunctions.h to describe the reason behind Proc…
Pirulax Aug 12, 2019
1d182b3
Update Client/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp
qaisjp Apr 2, 2020
83df612
Apply suggestions from code review
qaisjp Apr 2, 2020
c611622
Update Client/mods/deathmatch/logic/CCustomData.cpp
qaisjp Apr 2, 2020
55b22ca
delete make_uname.bat
qaisjp Apr 2, 2020
e11216c
Implemeting faster element data search, also optimizing existing code
Pirulax May 15, 2020
e7ef771
its fucked up, ill rewrite it once again, with another approach
Pirulax May 15, 2020
ff2acbd
server/CLuaArgument: small refactor, and add a move semantics support
Pirulax May 16, 2020
9ea9a10
server/CLuaArgument: small refactor, and add a move semantics support
Pirulax May 16, 2020
527b6aa
rewrite existing set/get/delete/remove/ElementData, also a few refact…
Pirulax May 16, 2020
c077f66
fix CStaticFuncDefs::SyncElementData, refactor CPlayerManager::Broadc…
Pirulax May 16, 2020
7d01bc3
Delete duplicated code(git went weird, and copied the file into itself
Pirulax May 16, 2020
e25ee35
Refactor CGame::Packet_CustomData
Pirulax May 16, 2020
a73bc60
*/CLuaArguments.*
Pirulax May 16, 2020
eb87937
Rewrite CCustomData
Pirulax May 16, 2020
f63e2ce
Small refactor to set/getElementData function
Pirulax May 16, 2020
6258a43
Remove duplicate code(added by git probably)
Pirulax May 16, 2020
3fa06e3
Small refactor of CStaticFuncDefs.cpp(2 lines)
Pirulax May 16, 2020
08ff641
Fix CLuaArgument constructor def and declaration mismatch. Fix typo
Pirulax May 16, 2020
25121e3
Add move semantics support to CLuaArguments::PushArgument
Pirulax May 16, 2020
885e9a9
Fix compiler error
Pirulax May 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 13 additions & 16 deletions Client/mods/deathmatch/logic/CClientEntity.cpp
Expand Up @@ -458,33 +458,30 @@ bool CClientEntity::GetCustomDataBool(const char* szName, bool& bOut, bool bInhe
return false;
}

void CClientEntity::SetCustomData(const char* szName, const CLuaArgument& Variable, bool bSynchronized)
bool CClientEntity::SetCustomData(const char* szName, const CLuaArgument& Variable, bool bSynchronized)
{
assert(szName);
if (strlen(szName) > MAX_CUSTOMDATA_NAME_LENGTH)
qaisjp marked this conversation as resolved.
Show resolved Hide resolved
{
// Don't allow it to be set if the name is too long
CLogger::ErrorPrintf("Custom data name too long (%s)", *SStringX(szName).Left(MAX_CUSTOMDATA_NAME_LENGTH + 1));
return;
return false;
}

// Grab the old variable
CLuaArgument oldVariable;
SCustomData* pData = m_pCustomData->Get(szName);
if (pData)
CLuaArgument OldVariable;
if (m_pCustomData->Set(szName, Variable, bSynchronized, &OldVariable))
Copy link
Contributor

@qaisjp qaisjp May 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work correctly if there is no previous element data?

Copy link
Contributor Author

@Pirulax Pirulax May 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, since the original one worked like this as well, it only set a value to CLuaArgument if there was something, otherwise an empty CLuaArgument is == nil

{
oldVariable = pData->Variable;
}

// Set the new data
m_pCustomData->Set(szName, Variable, bSynchronized);
// Trigger the onClientElementDataChange event on us
CLuaArguments Arguments;
Arguments.PushString(szName);
Arguments.PushArgument(OldVariable);
Arguments.PushArgument(Variable);
CallEvent("onClientElementDataChange", Arguments, true);

// Trigger the onClientElementDataChange event on us
CLuaArguments Arguments;
Arguments.PushString(szName);
Arguments.PushArgument(oldVariable);
Arguments.PushArgument(Variable);
CallEvent("onClientElementDataChange", Arguments, true);
return true;
}
return false;
}

void CClientEntity::DeleteCustomData(const char* szName)
Expand Down
2 changes: 1 addition & 1 deletion Client/mods/deathmatch/logic/CClientEntity.h
Expand Up @@ -202,7 +202,7 @@ class CClientEntity : public CClientEntityBase
bool GetCustomDataFloat(const char* szKey, float& fOut, bool bInheritData);
bool GetCustomDataInt(const char* szKey, int& iOut, bool bInheritData);
bool GetCustomDataBool(const char* szKey, bool& bOut, bool bInheritData);
void SetCustomData(const char* szName, const CLuaArgument& Variable, bool bSynchronized = true);
bool SetCustomData(const char* szName, const CLuaArgument& Variable, bool bSynchronized = true);
void DeleteCustomData(const char* szName);

virtual bool GetMatrix(CMatrix& matrix) const;
Expand Down
31 changes: 21 additions & 10 deletions Client/mods/deathmatch/logic/CCustomData.cpp
Expand Up @@ -14,35 +14,43 @@

void CCustomData::Copy(CCustomData* pCustomData)
{
std::map<std::string, SCustomData>::const_iterator iter = pCustomData->IterBegin();
CFastHashMap<SString, SCustomData>::const_iterator iter = pCustomData->IterBegin();
for (; iter != pCustomData->IterEnd(); iter++)
{
Set(iter->first.c_str(), iter->second.Variable);
Set(iter->first, iter->second.Variable);
}
}

SCustomData* CCustomData::Get(const char* szName)
{
assert(szName);
qaisjp marked this conversation as resolved.
Show resolved Hide resolved

std::map<std::string, SCustomData>::iterator it = m_Data.find(szName);
CFastHashMap<SString, SCustomData>::iterator it = m_Data.find(szName);
qaisjp marked this conversation as resolved.
Show resolved Hide resolved
if (it != m_Data.end())
return &it->second;

return NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just replace the content of Get with a call with MapFind?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could but that'd be just a pointless function call, and we can to squeeze out as much performance as we can, and I think this isn't premature optimization(yet)

Copy link
Contributor

@qaisjp qaisjp May 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there will be a real difference

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it converting from SString to const char* and then back?
Or it's optimized by the compiler already?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

99% chance optimised away. If in doubt, measure the difference

}

void CCustomData::Set(const char* szName, const CLuaArgument& Variable, bool bSynchronized)
bool CCustomData::Set(const char* szName, const CLuaArgument& Variable, bool bSynchronized, CLuaArgument* pOldVariable)
{
assert(szName);
qaisjp marked this conversation as resolved.
Show resolved Hide resolved

// Grab the item with the given name
qaisjp marked this conversation as resolved.
Show resolved Hide resolved
SCustomData* pData = Get(szName);
if (pData)
{
// Update existing
pData->Variable = Variable;
pData->bSynchronized = bSynchronized;
{
if (pData->Variable != Variable || pData->bSynchronized != bSynchronized)
{
// Set old variable if needed
if (pOldVariable)
*pOldVariable = pData->Variable;

// Update existing
pData->Variable = Variable;
pData->bSynchronized = bSynchronized;

return true;
}
}
else
{
Expand All @@ -51,13 +59,16 @@ void CCustomData::Set(const char* szName, const CLuaArgument& Variable, bool bSy
newData.Variable = Variable;
newData.bSynchronized = bSynchronized;
m_Data[szName] = newData;

return true;
}
return false;
}

bool CCustomData::Delete(const char* szName)
{
// Find the item and delete it
std::map<std::string, SCustomData>::iterator it = m_Data.find(szName);
CFastHashMap<SString, SCustomData>::iterator it = m_Data.find(szName);
if (it != m_Data.end())
{
m_Data.erase(it);
Expand Down
9 changes: 5 additions & 4 deletions Client/mods/deathmatch/logic/CCustomData.h
Expand Up @@ -10,6 +10,7 @@

#pragma once

#include "SharedUtil.FastHashMap.h"
#include "lua/CLuaArgument.h"

#define MAX_CUSTOMDATA_NAME_LENGTH 128
Expand All @@ -26,13 +27,13 @@ class CCustomData
void Copy(CCustomData* pCustomData);

SCustomData* Get(const char* szName);
void Set(const char* szName, const CLuaArgument& Variable, bool bSynchronized = true);
bool Set(const char* szName, const CLuaArgument& Variable, bool bSynchronized = true, CLuaArgument* pOldVariable = nullptr);

bool Delete(const char* szName);

std::map<std::string, SCustomData>::const_iterator IterBegin() { return m_Data.begin(); }
std::map<std::string, SCustomData>::const_iterator IterEnd() { return m_Data.end(); }
CFastHashMap<SString, SCustomData>::const_iterator IterBegin() { return m_Data.begin(); }
CFastHashMap<SString, SCustomData>::const_iterator IterEnd() { return m_Data.end(); }

private:
std::map<std::string, SCustomData> m_Data;
CFastHashMap<SString, SCustomData> m_Data;
};
@@ -1,3 +1,4 @@
<<<<<<< HEAD
/*****************************************************************************
*
* PROJECT: Multi Theft Auto
Expand Down
2 changes: 1 addition & 1 deletion Client/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp
Expand Up @@ -1738,9 +1738,9 @@ int CLuaElementDefs::SetElementData(lua_State* luaVM)
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
if (pLuaMain)
{
// Warn and truncate if key is too long
if (strKey.length() > MAX_CUSTOMDATA_NAME_LENGTH)
{
// Warn and truncate if key is too long
m_pScriptDebugging->LogCustom(luaVM, SString("Truncated argument @ '%s' [%s]", lua_tostring(luaVM, lua_upvalueindex(1)),
*SString("string length reduced to %d characters at argument 2", MAX_CUSTOMDATA_NAME_LENGTH)));
strKey = strKey.Left(MAX_CUSTOMDATA_NAME_LENGTH);
Expand Down