From 1331c3c67b10eae8e192a07cd4c17571d2797fa4 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sun, 9 Feb 2025 00:30:06 +0100 Subject: [PATCH] VGui: use cached string lengths for notify logs Don't recalculate the string length when displaying it on the VGui console. --- src/core/logger.cpp | 2 +- src/vgui/vgui_debugpanel.cpp | 16 ++++++++++------ src/vgui/vgui_debugpanel.h | 32 +++++++++++++++++--------------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/core/logger.cpp b/src/core/logger.cpp index d3a6c85f..c4c286e6 100644 --- a/src/core/logger.cpp +++ b/src/core/logger.cpp @@ -352,7 +352,7 @@ void EngineLoggerSink(LogType_t logType, LogLevel_t logLevel, eDLL_T context, if (g_bSdkInitialized && logLevel >= LogLevel_t::LEVEL_NOTIFY) { // Draw to mini console. - g_TextOverlay.AddLog(overlayContext, message.c_str()); + g_TextOverlay.AddLog(overlayContext, message.c_str(), (ssize_t)message.length()); } #endif // !DEDICATED } diff --git a/src/vgui/vgui_debugpanel.cpp b/src/vgui/vgui_debugpanel.cpp index 34811e51..828ba995 100644 --- a/src/vgui/vgui_debugpanel.cpp +++ b/src/vgui/vgui_debugpanel.cpp @@ -111,7 +111,7 @@ void CTextOverlay::Update(void) //----------------------------------------------------------------------------- // Purpose: add a log to the vector. //----------------------------------------------------------------------------- -void CTextOverlay::AddLog(const eDLL_T context, const char* pszText) +void CTextOverlay::AddLog(const eDLL_T context, const char* pszText, const ssize_t textLen) { Assert(pszText); @@ -121,7 +121,11 @@ void CTextOverlay::AddLog(const eDLL_T context, const char* pszText) } AUTO_LOCK(m_Mutex); - m_NotifyLines.AddToTail(CTextNotify{ context, con_notifytime.GetFloat() , pszText }); + + const int newLine = m_NotifyLines.AddToTail(); + TextNotify_s& notify = m_NotifyLines[newLine]; + + notify.Init(context, con_notifytime.GetFloat(), pszText, textLen); while (m_NotifyLines.Count() > 0 && (m_NotifyLines.Count() > con_notifylines.GetInt())) @@ -145,7 +149,7 @@ void CTextOverlay::DrawNotify(void) for (int i = 0, j = m_NotifyLines.Count(); i < j; i++) { - const CTextNotify& notify = m_NotifyLines[i]; + const TextNotify_s& notify = m_NotifyLines[i]; Color c = GetLogColorForType(notify.m_Type); const float flTimeleft = notify.m_flLifeRemaining; @@ -214,10 +218,10 @@ void CTextOverlay::ShouldDraw(const float flFrameTime) FOR_EACH_VEC_BACK(m_NotifyLines, i) { - CTextNotify* pNotify = &m_NotifyLines[i]; - pNotify->m_flLifeRemaining -= flFrameTime; + TextNotify_s& notify = m_NotifyLines[i]; + notify.m_flLifeRemaining -= flFrameTime; - if (pNotify->m_flLifeRemaining <= 0.0f) + if (notify.m_flLifeRemaining <= 0.0f) { m_NotifyLines.Remove(i); continue; diff --git a/src/vgui/vgui_debugpanel.h b/src/vgui/vgui_debugpanel.h index 957e6008..76314e32 100644 --- a/src/vgui/vgui_debugpanel.h +++ b/src/vgui/vgui_debugpanel.h @@ -2,21 +2,23 @@ #include "core/stdafx.h" #include "mathlib/color.h" -struct CTextNotify -{ - CTextNotify(const eDLL_T type, const float flTime, const char* pszText) - { - this->m_Text = pszText; - this->m_flLifeRemaining = flTime; - this->m_Type = type; - } - eDLL_T m_Type; - float m_flLifeRemaining; - CUtlString m_Text; -}; - class CTextOverlay { +private: + struct TextNotify_s + { + void Init(const eDLL_T type, const float flTime, const char* pszText, const ssize_t textLen) + { + this->m_Type = type; + this->m_flLifeRemaining = flTime; + this->m_Text.SetDirect(pszText, textLen); + } + + eDLL_T m_Type; + float m_flLifeRemaining; + CUtlString m_Text; + }; + public: CTextOverlay() { @@ -26,7 +28,7 @@ public: } void Update(void); - void AddLog(const eDLL_T context, const char* pszText); + void AddLog(const eDLL_T context, const char* pszText, const ssize_t textLen); void DrawNotify(void); void DrawFormat(const int x, const int y, const Color c, const char* pszFormat, ...) const; void ShouldDraw(const float flFrameTime); @@ -38,7 +40,7 @@ public: private: Color GetLogColorForType(const eDLL_T type) const; - CUtlVector m_NotifyLines; + CUtlVector m_NotifyLines; int m_nFontHeight; // Hardcoded to 16 in this engine. mutable CThreadFastMutex m_Mutex;