GameUI: use cached string lengths where possible

Performance improvements.
This commit is contained in:
Kawe Mazidjatari 2025-02-09 00:27:29 +01:00
parent 73b517600d
commit 26ec02f302
4 changed files with 71 additions and 54 deletions

View File

@ -281,9 +281,9 @@ void CBrowser::DrawBrowserPanel(void)
const char* pszHostMap = server.map.c_str(); const char* pszHostMap = server.map.c_str();
const char* pszPlaylist = server.playlist.c_str(); const char* pszPlaylist = server.playlist.c_str();
if (m_serverBrowserTextFilter.PassFilter(pszHostName) if (m_serverBrowserTextFilter.PassFilter(pszHostName, &pszHostName[server.name.length()])
|| m_serverBrowserTextFilter.PassFilter(pszHostMap) || m_serverBrowserTextFilter.PassFilter(pszHostMap, &pszHostMap[server.map.length()])
|| m_serverBrowserTextFilter.PassFilter(pszPlaylist)) || m_serverBrowserTextFilter.PassFilter(pszPlaylist, &pszPlaylist[server.playlist.length()]))
{ {
filteredServers.push_back(&server); filteredServers.push_back(&server);
} }
@ -297,28 +297,32 @@ void CBrowser::DrawBrowserPanel(void)
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)
{ {
const NetGameServer_t* const server = filteredServers[i]; const NetGameServer_t* const server = filteredServers[i];
const ImGuiTextFlags textFlags = ImGuiTextFlags_NoWidthForLargeClippedText;
const char* pszHostName = server->name.c_str();
const char* pszHostMap = server->map.c_str();
const char* pszPlaylist = server->playlist.c_str();
char pszHostPort[32];
sprintf(pszHostPort, "%d", server->port);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("%s", pszHostName);
const char* const pszHostName = server->name.c_str();
ImGui::TextEx(pszHostName, &pszHostName[server->name.length()], textFlags);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("%s", pszHostMap);
const char* const pszHostMap = server->map.c_str();
ImGui::TextEx(pszHostMap, &pszHostMap[server->map.length()], textFlags);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("%s", pszPlaylist);
const char* const pszPlaylist = server->playlist.c_str();
ImGui::TextEx(pszPlaylist, &pszPlaylist[server->playlist.length()], textFlags);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("%s", Format("%3d/%3d", server->numPlayers, server->maxPlayers).c_str());
const std::string playerNums = Format("%3d/%3d", server->numPlayers, server->maxPlayers);
const char* const pszPlayerNums = playerNums.c_str();
ImGui::TextEx(pszPlayerNums, &pszPlayerNums[playerNums.length()], textFlags);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("%s", pszHostPort); ImGui::Text("%d", server->port);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
string svConnectBtn = "Connect##"; string svConnectBtn = "Connect##";
@ -814,7 +818,7 @@ void CBrowser::UpdateHostingStatus(void)
break; break;
} }
const NetGameServer_t netGameServer NetGameServer_t netGameServer
{ {
hostname->GetString(), hostname->GetString(),
hostdesc.GetString(), hostdesc.GetString(),
@ -847,10 +851,10 @@ void CBrowser::UpdateHostingStatus(void)
// host data on the server browser // host data on the server browser
// Input : &gameServer - // Input : &gameServer -
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void CBrowser::SendHostingPostRequest(const NetGameServer_t& gameServer) void CBrowser::SendHostingPostRequest(NetGameServer_t& gameServer)
{ {
#ifndef CLIENT_DLL #ifndef CLIENT_DLL
std::thread request([&, gameServer] std::thread request([&, gameServer = std::move(gameServer)]
{ {
string hostRequestMessage; string hostRequestMessage;
string hostToken; string hostToken;
@ -860,7 +864,7 @@ void CBrowser::SendHostingPostRequest(const NetGameServer_t& gameServer)
g_TaskQueue.Dispatch([&, result, hostRequestMessage, hostToken, hostIp] g_TaskQueue.Dispatch([&, result, hostRequestMessage, hostToken, hostIp]
{ {
InstallHostingDetails(result, hostRequestMessage.c_str(), hostToken.c_str(), hostIp); InstallHostingDetails(result, hostRequestMessage, hostToken, hostIp);
}, 0); }, 0);
} }
); );
@ -875,7 +879,7 @@ void CBrowser::SendHostingPostRequest(const NetGameServer_t& gameServer)
// *hostToken - // *hostToken -
// &hostIp - // &hostIp -
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void CBrowser::InstallHostingDetails(const bool postFailed, const char* const hostMessage, const char* const hostToken, const string& hostIp) void CBrowser::InstallHostingDetails(const bool postFailed, const string& hostMessage, const string& hostToken, const string& hostIp)
{ {
#ifndef CLIENT_DLL #ifndef CLIENT_DLL
m_hostMessage = hostMessage; m_hostMessage = hostMessage;
@ -889,14 +893,10 @@ void CBrowser::InstallHostingDetails(const bool postFailed, const char* const ho
if (postFailed) if (postFailed)
{ {
m_hostMessageColor = ImVec4(0.00f, 1.00f, 0.00f, 1.00f); m_hostMessageColor = ImVec4(0.00f, 1.00f, 0.00f, 1.00f);
stringstream ssMessage;
ssMessage << "Broadcasting";
if (!m_hostToken.empty())
{
ssMessage << ": share the following token for clients to connect: ";
}
m_hostMessage = ssMessage.str(); m_hostMessage = m_hostToken.empty()
? "Broadcasting"
: "Broadcasting: share the following token for clients to connect: ";
} }
else else
{ {

View File

@ -31,8 +31,8 @@ public:
void DrawHostPanel(void); void DrawHostPanel(void);
void UpdateHostingStatus(void); void UpdateHostingStatus(void);
void InstallHostingDetails(const bool postFailed, const char* const hostMessage, const char* const hostToken, const string& hostIp); void InstallHostingDetails(const bool postFailed, const string& hostMessage, const string& hostToken, const string& hostIp);
void SendHostingPostRequest(const NetGameServer_t& gameServer); void SendHostingPostRequest(NetGameServer_t& gameServer);
void ProcessCommand(const char* pszCommand) const; void ProcessCommand(const char* pszCommand) const;

View File

@ -360,7 +360,7 @@ void CConsole::DrawOptionsPanel(void)
m_colorTextLogger.Copy(true); m_colorTextLogger.Copy(true);
} }
ImGui::Text("Console HotKey:"); ImGui::TextEx("Console HotKey:", nullptr, ImGuiTextFlags_NoWidthForLargeClippedText);
ImGui::SameLine(); ImGui::SameLine();
int selected = g_ImGuiConfig.m_ConsoleConfig.m_nBind0; int selected = g_ImGuiConfig.m_ConsoleConfig.m_nBind0;
@ -382,7 +382,7 @@ void CConsole::DrawOptionsPanel(void)
g_ImGuiConfig.Save(); g_ImGuiConfig.Save();
} }
ImGui::Text("Browser HotKey:"); ImGui::TextEx("Browser HotKey:", nullptr, ImGuiTextFlags_NoWidthForLargeClippedText);
ImGui::SameLine(); ImGui::SameLine();
selected = g_ImGuiConfig.m_BrowserConfig.m_nBind0; selected = g_ImGuiConfig.m_BrowserConfig.m_nBind0;
@ -508,7 +508,7 @@ static void AddHint(const ConVarFlags::FlagDesc_t& cvarInfo, const vector<MODULE
ImGui::Image(hintRes.m_idIcon, ImVec2(float(hintRes.m_nWidth), float(hintRes.m_nHeight))); ImGui::Image(hintRes.m_idIcon, ImVec2(float(hintRes.m_nWidth), float(hintRes.m_nHeight)));
ImGui::SameLine(); ImGui::SameLine();
ImGui::Text("%s", cvarInfo.shortdesc); ImGui::TextEx(cvarInfo.shortdesc, nullptr, ImGuiTextFlags_NoWidthForLargeClippedText);
}; };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -590,7 +590,7 @@ void CConsole::DrawAutoCompletePanel(void)
m_canAutoComplete = true; m_canAutoComplete = true;
m_reclaimFocus = true; m_reclaimFocus = true;
BuildSummaryText(newInputText.c_str()); BuildSummaryText(newInputText.c_str(), newInputText.size());
} }
ImGui::PopID(); ImGui::PopID();
@ -688,7 +688,8 @@ bool CConsole::RunAutoComplete(void)
for (int j = 0; j < iret; ++j) for (int j = 0; j < iret; ++j)
{ {
m_vecSuggest.push_back(ConAutoCompleteSuggest_s(commands[j].String(), COMMAND_COMPLETION_MARKER)); const CUtlString& cmdToAdd = commands[j];
m_vecSuggest.emplace_back(cmdToAdd.String(), cmdToAdd.Length(), COMMAND_COMPLETION_MARKER);
} }
} }
else else
@ -718,14 +719,25 @@ void CConsole::ResetAutoCompleteData(void)
m_vecSuggest.clear(); m_vecSuggest.clear();
} }
template <size_t N1, size_t N2>
static void EncaseAppendString(string& targetString, const char* toEncase, const char(&open)[N1], const char(&close)[N2])
{
const size_t appLen = strlen(toEncase);
const size_t newLen = targetString.length() + (N1-1) + (N2-1) + appLen+1;
targetString.reserve(newLen);
targetString.append(open, N1-1);
targetString.append(toEncase, appLen);
targetString.append(close, N2-1);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Purpose: format appends the value string // Purpose: format appends the value string
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void AppendValueString(string& targetString, const char* const toAppend) static void AppendValueString(string& targetString, const char* const toAppend)
{ {
targetString.append(" = ["); // Assign current value to string if its a ConVar. EncaseAppendString(targetString, toAppend, " = [", "]");
targetString.append(toAppend);
targetString.append("]");
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -733,12 +745,12 @@ static void AppendValueString(string& targetString, const char* const toAppend)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void AppendDocString(string& targetString, const char* const toAppend) static void AppendDocString(string& targetString, const char* const toAppend)
{ {
if (VALID_CHARSTAR(toAppend)) if (!VALID_CHARSTAR(toAppend))
{ {
targetString.append(" - \""); return;
targetString.append(toAppend);
targetString.append("\"");
} }
EncaseAppendString(targetString, toAppend, " - \"", "\"");
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -787,7 +799,7 @@ void CConsole::CreateSuggestionsFromPartial(void)
AppendDocString(docString, commandBase->GetHelpText()); AppendDocString(docString, commandBase->GetHelpText());
AppendDocString(docString, commandBase->GetUsageText()); AppendDocString(docString, commandBase->GetUsageText());
} }
m_vecSuggest.push_back(ConAutoCompleteSuggest_s(commandName + docString, commandBase->GetFlags())); m_vecSuggest.emplace_back(commandName + docString, commandBase->GetFlags());
} }
else else
{ {
@ -827,11 +839,11 @@ void CConsole::ProcessCommand(const char* const inputText)
// formats the number of history items instead // formats the number of history items instead
// Input : inputText - // Input : inputText -
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void CConsole::BuildSummaryText(const char* const inputText) void CConsole::BuildSummaryText(const char* const inputText, const size_t textLen)
{ {
if (*inputText) if (textLen > 0)
{ {
string conVarFormatted(inputText); string conVarFormatted(inputText, textLen);
// Remove trailing space and/or semicolon before we call 'g_pCVar->FindVar(..)'. // Remove trailing space and/or semicolon before we call 'g_pCVar->FindVar(..)'.
StringRTrim(conVarFormatted, " ;", true); StringRTrim(conVarFormatted, " ;", true);
@ -918,7 +930,7 @@ bool CConsole::LoadFlagIcons(void)
// Get all flag image resources for displaying flags. // Get all flag image resources for displaying flags.
for (int i = IDB_PNG3, k = NULL; i <= IDB_PNG32; i++, k++) for (int i = IDB_PNG3, k = NULL; i <= IDB_PNG32; i++, k++)
{ {
m_vecFlagIcons.push_back(MODULERESOURCE(GetModuleResource(sdkModule, i))); m_vecFlagIcons.emplace_back(GetModuleResource(sdkModule, i));
MODULERESOURCE& rFlagIcon = m_vecFlagIcons[k]; MODULERESOURCE& rFlagIcon = m_vecFlagIcons[k];
ret = LoadTextureBuffer(reinterpret_cast<unsigned char*>(rFlagIcon.m_pData), // !TODO: Fall-back texture. ret = LoadTextureBuffer(reinterpret_cast<unsigned char*>(rFlagIcon.m_pData), // !TODO: Fall-back texture.
@ -1021,7 +1033,7 @@ int CConsole::TextEditCallback(ImGuiInputTextCallbackData* iData)
} }
} }
BuildSummaryText(iData->Buf); BuildSummaryText(iData->Buf, iData->BufTextLen);
break; break;
} }
case ImGuiInputTextFlags_CallbackAlways: case ImGuiInputTextFlags_CallbackAlways:
@ -1086,7 +1098,7 @@ int CConsole::TextEditCallback(ImGuiInputTextCallbackData* iData)
ResetAutoCompleteData(); ResetAutoCompleteData();
} }
BuildSummaryText(iData->Buf); BuildSummaryText(iData->Buf, iData->BufTextLen);
break; break;
} }
} }
@ -1118,7 +1130,7 @@ void CConsole::HandleCommand()
m_inputTextBufModified = true; m_inputTextBufModified = true;
} }
BuildSummaryText(""); BuildSummaryText("", 0);
m_reclaimFocus = true; m_reclaimFocus = true;
} }
@ -1143,7 +1155,7 @@ void CConsole::HandleSuggest()
const int vecIndex = parked ? 0 : m_suggestPos; const int vecIndex = parked ? 0 : m_suggestPos;
DetermineInputTextFromSelectedSuggestion(m_vecSuggest[vecIndex], m_selectedSuggestionText); DetermineInputTextFromSelectedSuggestion(m_vecSuggest[vecIndex], m_selectedSuggestionText);
BuildSummaryText(m_selectedSuggestionText.c_str()); BuildSummaryText(m_selectedSuggestionText.c_str(), m_selectedSuggestionText.size());
m_inputTextBufModified = true; m_inputTextBufModified = true;
m_reclaimFocus = true; m_reclaimFocus = true;
@ -1265,7 +1277,7 @@ void CConsole::ClampLogSize(void)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Purpose: adds a command to the history vector; this is the only place text // Purpose: adds a command to the history vector; this is the only place text
// is added to the vector, do not call 'm_History.push_back' elsewhere as we // is added to the vector, do not call 'm_History.emplace_back' elsewhere as we
// also manage the size of the vector here !!! // also manage the size of the vector here !!!
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void CConsole::AddHistory(const char* const command) void CConsole::AddHistory(const char* const command)
@ -1281,7 +1293,7 @@ void CConsole::AddHistory(const char* const command)
} }
} }
m_vecHistory.push_back(command); m_vecHistory.emplace_back(command);
ClampHistorySize(); ClampHistorySize();
} }
@ -1300,7 +1312,7 @@ const vector<string>& CConsole::GetHistory(void) const
void CConsole::ClearHistory(void) void CConsole::ClearHistory(void)
{ {
m_vecHistory.clear(); m_vecHistory.clear();
BuildSummaryText(""); BuildSummaryText("", 0);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -30,7 +30,7 @@ private:
void CreateSuggestionsFromPartial(void); void CreateSuggestionsFromPartial(void);
void ProcessCommand(const char* const inputText); void ProcessCommand(const char* const inputText);
void BuildSummaryText(const char* const inputText); void BuildSummaryText(const char* const inputText, const size_t textLen);
struct ConAutoCompleteSuggest_s; struct ConAutoCompleteSuggest_s;
void DetermineInputTextFromSelectedSuggestion(const ConAutoCompleteSuggest_s& suggest, string& svInput); void DetermineInputTextFromSelectedSuggestion(const ConAutoCompleteSuggest_s& suggest, string& svInput);
@ -83,6 +83,11 @@ private:
text = inText; text = inText;
flags = inFlags; flags = inFlags;
} }
ConAutoCompleteSuggest_s(const char* inText, const size_t len, const int inFlags)
{
text.assign(inText, len);
flags = inFlags;
}
bool operator==(const string& a) const bool operator==(const string& a) const
{ {
return text.compare(a) == 0; return text.compare(a) == 0;