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? |
Re: Optimization - Global variables vs natives
In general, you shouldn't worry about it. Just use the functions.
|
Re: Optimization - Global variables vs natives
Even for OnGameFrame? With a loop on all the players?
|
Re: Optimization - Global variables vs natives
Yes
|
Re: Optimization - Global variables vs natives
Unless you're doing something absolutely insane, like creating and filling thousands of arrays per frame.
|
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:
PHP Code:
|
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". |
Re: Optimization - Global variables vs natives
|
Re: Optimization - Global variables vs natives
Quote:
|
Re: Optimization - Global variables vs natives
Quote:
|
All times are GMT -4. The time now is 10:08. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.