3053: Optimize uuid lookup r=heinrich5991 a=Jupeyy

Already wrote that patch when i noticed its not about this, maybe we can use it, else nvm

Co-authored-by: Jupeyy <jupjopjap@gmail.com>
This commit is contained in:
bors[bot] 2020-10-11 13:07:36 +00:00 committed by GitHub
commit 1a79fdec82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 10 deletions

View file

@ -68,7 +68,7 @@ R find_linear(R range, T value)
template<class R, class T>
R find_binary(R range, T value)
{
range = partition_linear(range, value);
range = partition_binary(range, value);
if(range.empty())
return range;
if(range.front() == value)

View file

@ -46,6 +46,7 @@ public:
Returns a sorted range that contains the whole array.
*/
range all() { return range(parent::list, parent::list + parent::num_elements); }
range all() const { return range(parent::list, parent::list + parent::num_elements); }
};
#endif // TL_FILE_SORTED_ARRAY_HPP

View file

@ -61,12 +61,12 @@ void ParseUuid(CUuid *pUuid, char *pBuffer)
&p[8], &p[9], &p[10], &p[11], &p[12], &p[13], &p[14], &p[15]);
}
bool CUuid::operator==(const CUuid &Other)
bool CUuid::operator==(const CUuid &Other) const
{
return mem_comp(this, &Other, sizeof(*this)) == 0;
}
bool CUuid::operator!=(const CUuid &Other)
bool CUuid::operator!=(const CUuid &Other) const
{
return !(*this == Other);
}
@ -90,6 +90,11 @@ void CUuidManager::RegisterName(int ID, const char *pName)
dbg_assert(LookupUuid(Name.m_Uuid) == -1, "duplicate uuid");
m_aNames.add(Name);
CNameIndexed NameIndexed;
NameIndexed.m_Uuid = Name.m_Uuid;
NameIndexed.m_ID = GetIndex(ID);
m_aNamesSorted.add(NameIndexed);
}
CUuid CUuidManager::GetUuid(int ID) const
@ -104,12 +109,10 @@ const char *CUuidManager::GetName(int ID) const
int CUuidManager::LookupUuid(CUuid Uuid) const
{
for(int i = 0; i < m_aNames.size(); i++)
sorted_array<CNameIndexed>::range it = ::find_binary(m_aNamesSorted.all(), Uuid);
if(!it.empty())
{
if(Uuid == m_aNames[i].m_Uuid)
{
return GetID(i);
}
return GetID(it.front().m_ID);
}
return UUID_UNKNOWN;
}

View file

@ -2,6 +2,7 @@
#define ENGINE_SHARED_UUID_MANAGER_H
#include <base/tl/array.h>
#include <base/tl/sorted_array.h>
enum
{
@ -17,8 +18,9 @@ struct CUuid
{
unsigned char m_aData[16];
bool operator==(const CUuid &Other);
bool operator!=(const CUuid &Other);
bool operator==(const CUuid &Other) const;
bool operator!=(const CUuid &Other) const;
bool operator<(const CUuid &Other) const { return mem_comp(m_aData, Other.m_aData, sizeof(m_aData)) < 0; }
};
CUuid RandomUuid();
@ -33,12 +35,23 @@ struct CName
const char *m_pName;
};
struct CNameIndexed
{
CUuid m_Uuid;
int m_ID;
bool operator<(const CNameIndexed &Other) const { return m_Uuid < Other.m_Uuid; }
bool operator<(const CUuid &Other) const { return m_Uuid < Other; }
bool operator==(const CUuid &Other) const { return m_Uuid == Other; }
};
class CPacker;
class CUnpacker;
class CUuidManager
{
array<CName> m_aNames;
sorted_array<CNameIndexed> m_aNamesSorted;
public:
void RegisterName(int ID, const char *pName);