AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   General (https://forums.alliedmods.net/forumdisplay.php?f=7)
-   -   Shouldn't the stocks inside .inc files always use "static"? (https://forums.alliedmods.net/showthread.php?t=309363)

OciXCrom 07-23-2018 09:21

Shouldn't the stocks inside .inc files always use "static"?
 
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?

asherkin 07-23-2018 09:47

Re: Shouldn't the stocks inside .inc files always use "static"?
 
There isn't an upside to it either.

OciXCrom 07-23-2018 12:36

Re: Shouldn't the stocks inside .inc files always use "static"?
 
How come? Isn't creating a variable once better than creating and destroying it 100 times per second?

asherkin 07-23-2018 13:39

Re: Shouldn't the stocks inside .inc files always use "static"?
 
"Creating a variable" is just incrementing a number by 4.

OciXCrom 07-23-2018 13:42

Re: Shouldn't the stocks inside .inc files always use "static"?
 
Then there's no point of static even existing?

asherkin 07-23-2018 13:52

Re: Shouldn't the stocks inside .inc files always use "static"?
 
`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).

klippy 07-23-2018 15:07

Re: Shouldn't the stocks inside .inc files always use "static"?
 
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.

asherkin 07-23-2018 15:31

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

Originally Posted by KliPPy (Post 2605681)
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.)

Powerlord 07-23-2018 17:54

Re: Shouldn't the stocks inside .inc files always use "static"?
 
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.

klippy 07-23-2018 18:00

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

Originally Posted by Powerlord (Post 2605712)
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.


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

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