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* pszPlaylist = server.playlist.c_str();
if (m_serverBrowserTextFilter.PassFilter(pszHostName)
|| m_serverBrowserTextFilter.PassFilter(pszHostMap)
|| m_serverBrowserTextFilter.PassFilter(pszPlaylist))
if (m_serverBrowserTextFilter.PassFilter(pszHostName, &pszHostName[server.name.length()])
|| m_serverBrowserTextFilter.PassFilter(pszHostMap, &pszHostMap[server.map.length()])
|| m_serverBrowserTextFilter.PassFilter(pszPlaylist, &pszPlaylist[server.playlist.length()]))
{
filteredServers.push_back(&server);
}
@ -297,28 +297,32 @@ void CBrowser::DrawBrowserPanel(void)
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)
{
const NetGameServer_t* const server = filteredServers[i];
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);
const ImGuiTextFlags textFlags = ImGuiTextFlags_NoWidthForLargeClippedText;
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::Text("%s", pszHostMap);
const char* const pszHostMap = server->map.c_str();
ImGui::TextEx(pszHostMap, &pszHostMap[server->map.length()], textFlags);
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::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::Text("%s", pszHostPort);
ImGui::Text("%d", server->port);
ImGui::TableNextColumn();
string svConnectBtn = "Connect##";
@ -814,7 +818,7 @@ void CBrowser::UpdateHostingStatus(void)
break;
}
const NetGameServer_t netGameServer
NetGameServer_t netGameServer
{
hostname->GetString(),
hostdesc.GetString(),
@ -847,10 +851,10 @@ void CBrowser::UpdateHostingStatus(void)
// host data on the server browser
// Input : &gameServer -
//-----------------------------------------------------------------------------
void CBrowser::SendHostingPostRequest(const NetGameServer_t& gameServer)
void CBrowser::SendHostingPostRequest(NetGameServer_t& gameServer)
{
#ifndef CLIENT_DLL
std::thread request([&, gameServer]
std::thread request([&, gameServer = std::move(gameServer)]
{
string hostRequestMessage;
string hostToken;
@ -860,7 +864,7 @@ void CBrowser::SendHostingPostRequest(const NetGameServer_t& gameServer)
g_TaskQueue.Dispatch([&, result, hostRequestMessage, hostToken, hostIp]
{
InstallHostingDetails(result, hostRequestMessage.c_str(), hostToken.c_str(), hostIp);
InstallHostingDetails(result, hostRequestMessage, hostToken, hostIp);
}, 0);
}
);
@ -875,7 +879,7 @@ void CBrowser::SendHostingPostRequest(const NetGameServer_t& gameServer)
// *hostToken -
// &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
m_hostMessage = hostMessage;
@ -889,14 +893,10 @@ void CBrowser::InstallHostingDetails(const bool postFailed, const char* const ho
if (postFailed)
{
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
{

View File

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

View File

@ -360,7 +360,7 @@ void CConsole::DrawOptionsPanel(void)
m_colorTextLogger.Copy(true);
}
ImGui::Text("Console HotKey:");
ImGui::TextEx("Console HotKey:", nullptr, ImGuiTextFlags_NoWidthForLargeClippedText);
ImGui::SameLine();
int selected = g_ImGuiConfig.m_ConsoleConfig.m_nBind0;
@ -382,7 +382,7 @@ void CConsole::DrawOptionsPanel(void)
g_ImGuiConfig.Save();
}
ImGui::Text("Browser HotKey:");
ImGui::TextEx("Browser HotKey:", nullptr, ImGuiTextFlags_NoWidthForLargeClippedText);
ImGui::SameLine();
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::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_reclaimFocus = true;
BuildSummaryText(newInputText.c_str());
BuildSummaryText(newInputText.c_str(), newInputText.size());
}
ImGui::PopID();
@ -688,7 +688,8 @@ bool CConsole::RunAutoComplete(void)
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
@ -718,14 +719,25 @@ void CConsole::ResetAutoCompleteData(void)
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
//-----------------------------------------------------------------------------
static void AppendValueString(string& targetString, const char* const toAppend)
{
targetString.append(" = ["); // Assign current value to string if its a ConVar.
targetString.append(toAppend);
targetString.append("]");
EncaseAppendString(targetString, toAppend, " = [", "]");
}
//-----------------------------------------------------------------------------
@ -733,12 +745,12 @@ static void AppendValueString(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(" - \"");
targetString.append(toAppend);
targetString.append("\"");
return;
}
EncaseAppendString(targetString, toAppend, " - \"", "\"");
}
//-----------------------------------------------------------------------------
@ -787,7 +799,7 @@ void CConsole::CreateSuggestionsFromPartial(void)
AppendDocString(docString, commandBase->GetHelpText());
AppendDocString(docString, commandBase->GetUsageText());
}
m_vecSuggest.push_back(ConAutoCompleteSuggest_s(commandName + docString, commandBase->GetFlags()));
m_vecSuggest.emplace_back(commandName + docString, commandBase->GetFlags());
}
else
{
@ -827,11 +839,11 @@ void CConsole::ProcessCommand(const char* const inputText)
// formats the number of history items instead
// 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(..)'.
StringRTrim(conVarFormatted, " ;", true);
@ -918,7 +930,7 @@ bool CConsole::LoadFlagIcons(void)
// Get all flag image resources for displaying flags.
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];
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;
}
case ImGuiInputTextFlags_CallbackAlways:
@ -1086,7 +1098,7 @@ int CConsole::TextEditCallback(ImGuiInputTextCallbackData* iData)
ResetAutoCompleteData();
}
BuildSummaryText(iData->Buf);
BuildSummaryText(iData->Buf, iData->BufTextLen);
break;
}
}
@ -1118,7 +1130,7 @@ void CConsole::HandleCommand()
m_inputTextBufModified = true;
}
BuildSummaryText("");
BuildSummaryText("", 0);
m_reclaimFocus = true;
}
@ -1143,7 +1155,7 @@ void CConsole::HandleSuggest()
const int vecIndex = parked ? 0 : m_suggestPos;
DetermineInputTextFromSelectedSuggestion(m_vecSuggest[vecIndex], m_selectedSuggestionText);
BuildSummaryText(m_selectedSuggestionText.c_str());
BuildSummaryText(m_selectedSuggestionText.c_str(), m_selectedSuggestionText.size());
m_inputTextBufModified = 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
// 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 !!!
//-----------------------------------------------------------------------------
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();
}
@ -1300,7 +1312,7 @@ const vector<string>& CConsole::GetHistory(void) const
void CConsole::ClearHistory(void)
{
m_vecHistory.clear();
BuildSummaryText("");
BuildSummaryText("", 0);
}
//-----------------------------------------------------------------------------

View File

@ -30,7 +30,7 @@ private:
void CreateSuggestionsFromPartial(void);
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;
void DetermineInputTextFromSelectedSuggestion(const ConAutoCompleteSuggest_s& suggest, string& svInput);
@ -83,6 +83,11 @@ private:
text = inText;
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
{
return text.compare(a) == 0;