Raised This Month: $32 Target: $400
 8% 

Optimization - Global variables vs natives


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
micapat
Veteran Member
Join Date: Feb 2010
Location: Nyuu, nyuu (France).
Old 09-11-2017 , 16:07   Optimization - Global variables vs natives
Reply With Quote #1

Well, here is the big question .

To check for example the player state (Alive or dead) or cvars values in "heavy" functions (OnGameFrame, OnPlayerRunCmd, Timer100ms,...), is it better to remember the value using a global variable (+ Some hooks to detect when the real value has changed) or to use directly the natives (IsPlayerAlive, ...)?

I'm mainly talking in terms of performance (Memory shouldn't be an issue) and in case of a complex server using multiple plugins (Like a zombie mode for example).

------------
Under Amxmodx, lot of developpers were doing like above using global variables, even for "light" functions (OnTakeDamage, OnWeaponSwitch,...), but is it the same for SourceMod?
__________________
micapat is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 09-11-2017 , 16:23   Re: Optimization - Global variables vs natives
Reply With Quote #2

In general, you shouldn't worry about it. Just use the functions.
__________________

Last edited by ddhoward; 09-11-2017 at 16:23.
ddhoward is offline
micapat
Veteran Member
Join Date: Feb 2010
Location: Nyuu, nyuu (France).
Old 09-11-2017 , 16:28   Re: Optimization - Global variables vs natives
Reply With Quote #3

Even for OnGameFrame? With a loop on all the players?
__________________
micapat is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 09-11-2017 , 17:56   Re: Optimization - Global variables vs natives
Reply With Quote #4

Yes
__________________
Neuro Toxin is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 09-11-2017 , 18:14   Re: Optimization - Global variables vs natives
Reply With Quote #5

Unless you're doing something absolutely insane, like creating and filling thousands of arrays per frame.
__________________
ddhoward is offline
Timocop
AlliedModders Donor
Join Date: Mar 2013
Location: Germany
Old 09-11-2017 , 18:25   Re: Optimization - Global variables vs natives
Reply With Quote #6

Globals/Statics are always faster. But replacing natives with cached values are not always optimal and not realy necessary. If you use arrays like strings in fequent loops or calls (like OnGameFrame()) you may want to use "static" instead of the standart declaration, because it doesnt need to initalize everytime. Which will save some CPU but increases memory and file size.

Well, just stick to natives instead of caching values. Unless you realy need to, like caching an entity reference because you need it later.

Switch to globals/statics (only non-recursive functions!):
PHP Code:
public void OnGameFrame()
{
    static 
i;
    for(
02048i++)
    {
        
//...
        
static char classname[64];
        
GetEntityClassname(iclassnamesizeof classname);
        
//...
    
}

Variable first, rest second. Use variables first in statements, dont do unessescary calls.
PHP Code:
for(int i 02048i++)
{
    if(!
g_bIsCustomCar[i] || !IsEntityValid(i))
        continue;
    
//...

Timocop is offline
micapat
Veteran Member
Join Date: Feb 2010
Location: Nyuu, nyuu (France).
Old 09-11-2017 , 18:28   Re: Optimization - Global variables vs natives
Reply With Quote #7

Ok, thanks!

Btw, is there not something like this for SourceMod: https://wiki.alliedmods.net/Optimizi...d_X_Scripting) ?
It could be nice to have something similar to give the "good pratices".
__________________

Last edited by micapat; 09-11-2017 at 18:39.
micapat is offline
Timocop
AlliedModders Donor
Join Date: Mar 2013
Location: Germany
Old 09-11-2017 , 19:27   Re: Optimization - Global variables vs natives
Reply With Quote #8

https://wiki.alliedmods.net/Optimizi...Mod_Scripting)
Timocop is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 09-11-2017 , 20:07   Re: Optimization - Global variables vs natives
Reply With Quote #9

Quote:
Originally Posted by micapat View Post
Ok, thanks!

Btw, is there not something like this for SourceMod: https://wiki.alliedmods.net/Optimizi...d_X_Scripting) ?
It could be nice to have something similar to give the "good pratices".
Here's some good ones:
  • Avoid using OnGameFrame.
  • If you're looping players in OnGameFrame, see if you can use OnPlayerRunCmd instead.
  • Avoid looping all entities, use FindEntityByClasname if applicable.

Last edited by headline; 09-11-2017 at 20:33.
headline is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 09-11-2017 , 21:06   Re: Optimization - Global variables vs natives
Reply With Quote #10

Quote:
Originally Posted by Timocop View Post
A lot of that is outdated. decl, for example, doesn't exist in the new syntax.
__________________

Last edited by ddhoward; 09-11-2017 at 21:07.
ddhoward 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 06:13.


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