From fa50c36519b0908f5433b97f06532364559c2a36 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Sat, 11 May 2024 09:19:55 +0200 Subject: [PATCH] rocket: avoid doing 2 or 3 Cvar::Get() trap calls per OnUpdate() call per cvar element --- src/cgame/rocket/rocketConditionalElement.h | 19 ++++++++++++++-- src/cgame/rocket/rocketCvarInlineElement.h | 25 ++++++++++++++++----- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/cgame/rocket/rocketConditionalElement.h b/src/cgame/rocket/rocketConditionalElement.h index 91d86783549..b03aae5931b 100644 --- a/src/cgame/rocket/rocketConditionalElement.h +++ b/src/cgame/rocket/rocketConditionalElement.h @@ -90,7 +90,22 @@ class RocketConditionalElement : public Rml::Element virtual void OnUpdate() { - if ( dirty_value || ( !cvar.empty() && cvar_value != Cvar::GetValue( cvar.c_str() ).c_str() ) ) + Rml::String value; + bool modified = false; + + if ( dirty_value ) + { + value = Cvar::GetValue( cvar ); + modified = true; + } + else if ( !cvar.empty() ) + { + value = Cvar::GetValue( cvar ); + + modified = cvar_value != value; + } + + if ( modified ) { if ( IsConditionValid() ) { @@ -107,7 +122,7 @@ class RocketConditionalElement : public Rml::Element } } - cvar_value = Cvar::GetValue( cvar.c_str() ).c_str(); + cvar_value = value; if ( dirty_value ) { diff --git a/src/cgame/rocket/rocketCvarInlineElement.h b/src/cgame/rocket/rocketCvarInlineElement.h index 935e9b56cba..289ae89b032 100644 --- a/src/cgame/rocket/rocketCvarInlineElement.h +++ b/src/cgame/rocket/rocketCvarInlineElement.h @@ -83,19 +83,34 @@ class RocketCvarInlineElement : public Rml::Element virtual void OnUpdate() { - if ( dirty_value || ( !cvar.empty() && cvar_value.c_str() != Cvar::GetValue( cvar.c_str() ) ) ) + Rml::String value; + bool modified = false; + + if ( dirty_value ) + { + value = Cvar::GetValue( cvar ); + modified = true; + } + else if ( !cvar.empty() ) + { + value = Cvar::GetValue( cvar ); + + modified = cvar_value != value; + } + + if ( modified ) { - Rml::String value = cvar_value = Cvar::GetValue( cvar.c_str() ).c_str(); + cvar_value = value; - if (!format.empty()) + if ( !format.empty() ) { if (type == NUMBER) { - value = va( format.c_str(), atof( Cvar::GetValue( cvar.c_str() ).c_str() ) ); + value = va( format.c_str(), atof( cvar_value.c_str() ) ); } else { - value = va( format.c_str(), Cvar::GetValue(cvar.c_str()).c_str() ); + value = va( format.c_str(), cvar_value.c_str() ); } }