AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Optimization - Global variables vs natives (https://forums.alliedmods.net/showthread.php?t=301204)

micapat 09-11-2017 16:07

Optimization - Global variables vs natives
 
Well, here is the big question :bee:.

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?

ddhoward 09-11-2017 16:23

Re: Optimization - Global variables vs natives
 
In general, you shouldn't worry about it. Just use the functions.

micapat 09-11-2017 16:28

Re: Optimization - Global variables vs natives
 
Even for OnGameFrame? With a loop on all the players?

Neuro Toxin 09-11-2017 17:56

Re: Optimization - Global variables vs natives
 
Yes

ddhoward 09-11-2017 18:14

Re: Optimization - Global variables vs natives
 
Unless you're doing something absolutely insane, like creating and filling thousands of arrays per frame.

Timocop 09-11-2017 18:25

Re: Optimization - Global variables vs natives
 
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;
    
//...



micapat 09-11-2017 18:28

Re: Optimization - Global variables vs natives
 
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".

Timocop 09-11-2017 19:27

Re: Optimization - Global variables vs natives
 
https://wiki.alliedmods.net/Optimizi...Mod_Scripting)

headline 09-11-2017 20:07

Re: Optimization - Global variables vs natives
 
Quote:

Originally Posted by micapat (Post 2548305)
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.

ddhoward 09-11-2017 21:06

Re: Optimization - Global variables vs natives
 
Quote:

Originally Posted by Timocop (Post 2548317)

A lot of that is outdated. decl, for example, doesn't exist in the new syntax.


All times are GMT -4. The time now is 10:08.

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