Skip to content

Commit

Permalink
Improve HasEntProp performance (alliedmodders#1908)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirdigbot authored and StarterX4 committed Aug 2, 2023
1 parent 7c5015d commit b222303
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
41 changes: 28 additions & 13 deletions core/HalfLife2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,20 +421,25 @@ bool CHalfLife2::FindSendPropInfo(const char *classname, const char *offset, sm_
return false;
}

if (!pInfo->lookup.retrieve(offset, info))
DataTableInfo::SendPropInfo temp;

if (!pInfo->lookup.retrieve(offset, &temp))
{
sm_sendprop_info_t temp_info;
bool found = UTIL_FindInSendTable(pInfo->sc->m_pTable, offset, &temp.info, 0);
temp.name = offset;

pInfo->lookup.insert(offset, temp);

if (!UTIL_FindInSendTable(pInfo->sc->m_pTable, offset, &temp_info, 0))
if (found)
{
return false;
*info = temp.info;
}

pInfo->lookup.insert(offset, temp_info);
*info = temp_info;
return found;
}

return true;

*info = temp.info;
return info->prop != nullptr;
}

SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset)
Expand Down Expand Up @@ -468,15 +473,25 @@ bool CHalfLife2::FindDataMapInfo(datamap_t *pMap, const char *offset, sm_datatab
m_Maps.add(i, pMap, new DataMapCache());

DataMapCache *cache = i->value;
DataMapCacheInfo temp;

if (!cache->retrieve(offset, pDataTable))
if (!cache->retrieve(offset, &temp))
{
if (!UTIL_FindDataMapInfo(pMap, offset, pDataTable))
return false;
cache->insert(offset, *pDataTable);
bool found = UTIL_FindDataMapInfo(pMap, offset, &temp.info);
temp.name = offset;

cache->insert(offset, temp);

if (found)
{
*pDataTable = temp.info;
}

return found;
}

return true;
*pDataTable = temp.info;
return pDataTable->prop != nullptr;
}

void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset)
Expand Down
32 changes: 24 additions & 8 deletions core/HalfLife2.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,24 @@ using namespace SourceMod;

struct DataTableInfo
{
struct SendPropPolicy
struct SendPropInfo
{
static inline bool matches(const char *name, const sm_sendprop_info_t &info)
static inline bool matches(const char *name, const SendPropInfo &info)
{
return strcmp(name, info.prop->GetName()) == 0;
return strcmp(name, info.name.c_str()) == 0;
}
static inline uint32_t hash(const detail::CharsAndLength &key)
{
return key.hash();
}

SendPropInfo()
: name(), info{nullptr, 0}
{
}

std::string name;
sm_sendprop_info_t info;
};

static inline bool matches(const char *name, const DataTableInfo *info)
Expand All @@ -116,22 +124,30 @@ struct DataTableInfo
}

ServerClass *sc;
NameHashSet<sm_sendprop_info_t, SendPropPolicy> lookup;
NameHashSet<SendPropInfo> lookup;
};

struct DataMapCachePolicy
struct DataMapCacheInfo
{
static inline bool matches(const char *name, const sm_datatable_info_t &info)
static inline bool matches(const char *name, const DataMapCacheInfo &info)
{
return strcmp(name, info.prop->fieldName) == 0;
return strcmp(name, info.name.c_str()) == 0;
}
static inline uint32_t hash(const detail::CharsAndLength &key)
{
return key.hash();
}

DataMapCacheInfo()
: name(), info{nullptr, 0}
{
}

std::string name;
sm_datatable_info_t info;
};

typedef NameHashSet<sm_datatable_info_t, DataMapCachePolicy> DataMapCache;
typedef NameHashSet<DataMapCacheInfo> DataMapCache;

struct DelayedFakeCliCmd
{
Expand Down

0 comments on commit b222303

Please sign in to comment.