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

Shouldn't the stocks inside .inc files always use "static"?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 07-23-2018 , 09:21   Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #1

We all know that static is mostly used in functions that are called very often (e.g. client_PreThink), so shouldn't the stocks inside .inc files always be declared variables using static instead of new?

Take for example the is_user_admin function from amxmisc.inc - it has a variable inside it that's declared with new. When people use this function inside of client_PreThink, then they are basically using new inside of one of the most often called functions. In this case using static is much better, so the variable won't be created 100 times per second.

This is just a simple example with a very small function. There are many other functions inside of the default AMXX .inc files that use way more variables inside them declared with new. I'm pretty sure that plugins can become much more optimized if we change the usage of new to static in the include files. I don't think there's a downside if this change is made. What do you think?
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 07-23-2018 , 09:47   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #2

There isn't an upside to it either.
__________________
asherkin is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 07-23-2018 , 12:36   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #3

How come? Isn't creating a variable once better than creating and destroying it 100 times per second?
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 07-23-2018 , 13:39   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #4

"Creating a variable" is just incrementing a number by 4.
__________________
asherkin is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 07-23-2018 , 13:42   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #5

Then there's no point of static even existing?
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 07-23-2018 , 13:52   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #6

`static` is an encapsulation thing, not an optimisation thing.

It is a way to get a variable that has the lifetime of a global (so it isn't reinitialised on every invocation), but the visibility of a local variable (so you know other functions aren't poking at it).
__________________
asherkin is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-23-2018 , 15:07   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #7

It can be an optimization when you have large arrays (talking about at least thousands of elements) in a function that's VERY frequently called, but may as well make it global at that point. memset-ing a few kilobytes is trivial for modern CPUs and even then you are probably shaving off a few nanoseconds of processing time each second. People in AMXX go ham over this thinking their server will work noticeably better if they make everything static.
Make readable and maintainable code. Stop thinking about micro-optimizations at every line of code you write. If performance is an issue (and it isn't for most people, they are tripping) you can delegate the task to a module.
__________________

Last edited by klippy; 07-23-2018 at 15:19.
klippy is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 07-23-2018 , 15:31   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #8

Quote:
Originally Posted by KliPPy View Post
If performance is an issue (and it isn't for most people, they are tripping) you can delegate the task to a module.
This is also bad advice in a lot of cases, most native calls' time is overwhelmingly consumed by the pawn->native->pawn interop. Pawn is fast, yo (and SourcePawn is even faster.)
__________________
asherkin is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 07-23-2018 , 17:54   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #9

Does AMXModX support file-level static variables? I know SourceMod does.

Those have their uses too; specifically they're a global variable that prevents name collision in the plugins that use that include.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-23-2018 , 18:00   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #10

Quote:
Originally Posted by Powerlord View Post
Does AMXModX support file-level static variables? I know SourceMod does.

Those have their uses too; specifically they're a global variable that prevents name collision in the plugins that use that include.
Yes, I use them all the time.
__________________
klippy is offline
Reply


Thread Tools
Display Modes

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:23.


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