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

[vstdlib] optimize CCvar::FindCommandBase #110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions vstdlib/cvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "tier0/vprof.h"
#include "tier1/tier1.h"
#include "tier1/utlbuffer.h"
#include "unordered_map"
#include "string"

#ifdef _X360
#include "xbox/xbox_console.h"
Expand Down Expand Up @@ -113,6 +115,7 @@ class CCvar final : public ICvar
CUtlVector< IConsoleDisplayFunc* > m_DisplayFuncs;
int m_nNextDLLIdentifier;
ConCommandBase *m_pConCommandList;
std::unordered_map<std::string, ConCommandBase*> m_pConCommandNames;

// temporary console area so we can store prints before console display funs are installed
mutable CUtlBuffer m_TempConsoleBuffer;
Expand Down Expand Up @@ -162,6 +165,39 @@ class CCvar final : public ICvar
private:
// Standard console commands -- DO NOT PLACE ANY HIGHER THAN HERE BECAUSE THESE MUST BE THE FIRST TO DESTRUCT
CON_COMMAND_MEMBER_F( CCvar, "find", Find, "Find concommands with the specified string in their name/help text.", 0 )

/*
* BUG: The Source engine uses Q_stricmp -> V_stricmp which is case insensitive, so we need to account for that.
*/
inline void AddConVarName( ConCommandBase* variable )
{
std::string strName = variable->GetName();
std::transform( strName.begin(), strName.end(), strName.begin(), ::tolower );

m_pConCommandNames.try_emplace( strName, variable );
}

inline void RemoveConVarName( const ConCommandBase* variable )
{
std::string strName = variable->GetName();
std::transform( strName.begin(), strName.end(), strName.begin(), ::tolower );

auto it = m_pConCommandNames.find( strName );
if ( it != m_pConCommandNames.end() )
m_pConCommandNames.erase( it );
}

inline ConCommandBase* FindConVarName( const char* name ) const
{
std::string strName = name;
std::transform( strName.begin(), strName.end(), strName.begin(), ::tolower );

auto it = m_pConCommandNames.find( strName );
if ( it != m_pConCommandNames.end() )
return it->second;

return NULL;
}
};

void CCvar::CCVarIteratorInternal::SetFirst( void ) RESTRICT
Expand Down Expand Up @@ -403,6 +439,8 @@ void CCvar::RegisterConCommand( ConCommandBase *variable )
// link the variable in
variable->m_pNext = m_pConCommandList;
m_pConCommandList = variable;

AddConVarName( variable );
}

void CCvar::UnregisterConCommand( ConCommandBase *pCommandToRemove )
Expand Down Expand Up @@ -432,6 +470,7 @@ void CCvar::UnregisterConCommand( ConCommandBase *pCommandToRemove )
pPrev->m_pNext = pCommand->m_pNext;
}
pCommand->m_pNext = NULL;
RemoveConVarName( pCommandToRemove );
break;
}
}
Expand All @@ -453,6 +492,8 @@ void CCvar::UnregisterConCommands( CVarDLLIdentifier_t id )
// Unlink
pCommand->m_bRegistered = false;
pCommand->m_pNext = NULL;

RemoveConVarName( pCommand );
}

pCommand = pNext;
Expand All @@ -467,24 +508,12 @@ void CCvar::UnregisterConCommands( CVarDLLIdentifier_t id )
//-----------------------------------------------------------------------------
const ConCommandBase *CCvar::FindCommandBase( const char *name ) const
{
const ConCommandBase *cmd = GetCommands();
for ( ; cmd; cmd = cmd->GetNext() )
{
if ( !Q_stricmp( name, cmd->GetName() ) )
return cmd;
}
return NULL;
return FindConVarName( name );
}

ConCommandBase *CCvar::FindCommandBase( const char *name )
{
ConCommandBase *cmd = GetCommands();
for ( ; cmd; cmd = cmd->GetNext() )
{
if ( !Q_stricmp( name, cmd->GetName() ) )
return cmd;
}
return NULL;
return FindConVarName( name );
}


Expand Down
Loading