Raised This Month: $51 Target: $400
 12% 

ConVar Handles Optimization


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Luke Penny
Senior Member
Join Date: Jan 2010
Location: Canada
Old 03-12-2011 , 16:52   ConVar Handles Optimization
Reply With Quote #1

I'm doing a lot of convar changes frequently - wondering what would be the most efficient way to do this. Over the course of months I've used three different methods but I'm not sure which would be best. I'm changing some 200 cvars around every 5-15 minutes.

PHP Code:
new Handle:cvar;
cvar FindConVar("variable1");
SetConVarInt(cvar1);
cvar FindConVar("variable2");
SetConVarInt(cvar2);

new 
Handle:var1;
new 
Handle:var2;
var1 FindConVar("variable1")
SetConVarInt(var11);
var2 FindConVar("variable2")
SetConVarInt(var22);

//or simply...

SetConVarInt(FindConVar("variable"), 1);
SetConVarInt(FindConVar("variable2"), 2); 
I'm pretty sure the first one is a stupid way to go about it. However comparing the last two, considering that I'm changing them frequently I think I could avoid finding the handle again if each one had it's own handle?

Any suggestions as to which is the most efficient?
Luke Penny is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 03-12-2011 , 17:13   Re: ConVar Handles Optimization
Reply With Quote #2

I read somewhere that you should put FindConVar in OnPluginStart() as it's a resourceful function and bad to call over and over for the same thing. Store the returned handles as global handle variables and set your convars using those.
__________________
Silvers is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 03-13-2011 , 06:18   Re: ConVar Handles Optimization
Reply With Quote #3

That's correct, FindConVar is very expensive and should therefore be called only once (OnPluginStart). The GetConVar* Functions are cheap and can be used just like that.
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 03-13-2011 , 07:03   Re: ConVar Handles Optimization
Reply With Quote #4

Ok, I'm also interested about this topic.
I have start made this way when check server cvars and when it change.
PHP Code:
new Handle:mp_startmoney INVALID_HANDLE;
new 
startmoney;

public 
OnPluginStart()
{
    
mp_startmoney FindConVar("mp_startmoney");
    
startmoney GetConVarInt(mp_startmoney);
    
HookConVarChange(mp_startmoneyConVarChanged);
}

public 
ConVarChanged(Handle:convar, const String:oldValue[], const String:newValue[])
{
    
startmoney GetConVarInt(mp_startmoney);

Is it good way built this way ?
Bacardi is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 03-13-2011 , 10:51   Re: ConVar Handles Optimization
Reply With Quote #5

Yeah that looks fine Bacardi.
__________________
Silvers is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 03-13-2011 , 11:08   Re: ConVar Handles Optimization
Reply With Quote #6

But wait a minute, why I didn't though this earlier.
There can compare handles ??
PHP Code:
new Handle:mp_startmoney INVALID_HANDLE;
new 
startmoney;

public 
OnPluginStart()
{
    
mp_startmoney FindConVar("mp_startmoney");
    
startmoney GetConVarInt(mp_startmoney);
    
HookConVarChange(mp_startmoneyConVarChanged);
}

public 
ConVarChanged(Handle:convar, const String:oldValue[], const String:newValue[])
{
    if(
convar == mp_startmoney)    // This better ?
    
{
        
startmoney StringToInt(newValue);
    }

- And one less GetConvar function on use
Bacardi is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 03-13-2011 , 11:30   Re: ConVar Handles Optimization
Reply With Quote #7

What would the point of that be? Optimization?
Quote:
Originally Posted by berni View Post
The GetConVar* Functions are cheap and can be used just like that.

You may want to check in OnPluginStart that your handles are not INVALID_HANDLE but even then its highly unlikely to fail unless you don't supply the correct convar name.
__________________
Silvers is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:11.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode