Raised This Month: $ Target: $400
 0% 

Why are define macro function thingies bad?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 10-20-2014 , 16:43   Why are define macro function thingies bad?
Reply With Quote #1

I have yet for someone to give me a good explanation or example of why the following is bad:

PHP Code:
#define DOWHILE_ENTFOUND(%1,%2) %1 = -1; while ((%1 = FindEntityByClassname2(%1, %2)) != -1)

#define IsValidClient(%1) (0 < %1 && %1 <= MaxClients && IsClientInGame(%1) && !GetEntProp(%1, Prop_Send, "m_bIsCoaching"))

#define IsReplayClient(%1) (IsClientSourceTV(%1) || IsClientReplay(%1))

#define RemoveCond(%1,%2)     if (TF2_IsPlayerInCondition(%1, %2)) TF2_RemoveCondition(%1, %2)
#define InsertCond(%1,%2,%3)  if (!TF2_IsPlayerInCondition(%1, %2)) TF2_AddCondition(%1, %2, %3)

#define IsMediUber(%1)  TF2_IsPlayerInCondition(%1, TFCond_Ubercharged)
#define IsVacUber(%1)   (TF2_IsPlayerInCondition(%1, TFCond_UberBlastResist) || TF2_IsPlayerInCondition(%1, TFCond_UberBulletResist) || TF2_IsPlayerInCondition(%1, TFCond_UberFireResist))
#define IsQuickUber(%1) TF2_IsPlayerInCondition(%1, TFCond_MegaHeal)
#define IsAnyUber(%1)   (IsMediUber(%1) || IsVacUber(%1) || IsQuickUber(%1))

#define IsPlayerOwner(%1)   GetAdminFlag(GetUserAdmin(%1), Admin_Root)
#define IsPlayerAdmin(%1)   GetAdminFlag(GetUserAdmin(%1), Admin_Slay) 
As opposed to

PHP Code:
stock FindSomeEnts()
{
    
decl ent;

    
ent = -1;
    while (
ent FindEntityByClassname2(ent"func_breakable") != -1)
    {
        
//thing
    
}

    
ent = -1;
    while (
ent FindEntityByClassname2(ent"obj_dispenser") != -1)
    {
        
//thingy
    
}
}

stock bool:IsValidClient(clientbool:replaycheck true)
{
    if (
client <= || client MaxClients) return false;
    if (!
IsClientInGame(client)) return false;
    if (
GetEntProp(clientProp_Send"m_bIsCoaching")) return false;
    if (
replaycheck)
    {
        if (
IsClientSourceTV(client) || IsClientReplay(client)) return false;
    }
    return 
true;
}

stock RemoveCond(clientcond)
{
    if (
TF2_IsPlayerInCondition(clientcond))  // I'm also curious, is it even necessary to check if they're in the condition before removing it?
    
{
        
TF2_RemoveCondition(clientcond);        // Can I just call this even if they're not in the condition?
    
}

So far all I've heard is that "I just shouldn't" and the discussion from this thread: https://forums.alliedmods.net/showth...76#post2175576

Which I guess makes me wary of doing var++ inside of one of these functions but doesn't apply to how I use that dowhile in every single case I use it... so is the dowhile still bad?

I kinda prefer inliney macros, especially for things like that FindEntity loop that I use very commonly in the same exact way that's less of a pain to type out with the macro.

And afaik shouldn't macros be faster (I know caring about micro-optimizations is pointless but I have OCD for it because I used to work a lot with assembly with lots of limits on how many cycles you want to be using up) than pushing a return and whatnot.

The only other thing I've heard is that... I guess the defines would be harder to read and comment? And I guess when I have stuff like IsVacUber that are really long, readability is also lost. But is that it? And is it really a big deal in the programming world?

Edit: Just read this: https://wiki.alliedmods.net/Introduc...ourcepawn#decl

Note: decl should not be used on single cell variables. There is almost never any benefit.

Why isn't there? Does it mean to say 'the benefit is so extremely negligible so who cares' or 'It's literally detrimental in some cases'.

Additionally, does making something 'static' actually change performance in any way? I have a friend who's all OCD about putting things in their own files and making everything statics, and I'm not sure if that's only to avoid making global variables or if static does anything else besides making it local scope only.
__________________

Last edited by Chdata; 10-20-2014 at 16:50.
Chdata is offline
Leonardo
Veteran Member
Join Date: Feb 2010
Location: 90's
Old 10-20-2014 , 17:18   Re: Why are define macro function thingies bad?
Reply With Quote #2

I think it just a bit slows down the compilation time, nothing else.

I don't see defined functions very useful tho.
Also, 0 < %1 <= MaxClients instead of 0 < %1 && %1 <= MaxClients.

Last edited by Leonardo; 10-20-2014 at 17:19.
Leonardo is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 10-20-2014 , 17:22   Re: Why are define macro function thingies bad?
Reply With Quote #3

A few reasons:
  • Macros are inserted literally everywhere you use them. This adds to the overall size of the compiled function. While function calls do add some overhead, they do so while only being present in the compiled file once.
  • Macros are a lot less readable than functions are. They also don't get syntax highlighted in most editors.

Decl:

As it says, there's no benefit to decling non-array values. Not only that, but doing so on certain types (such as Handles) may end up with unexpected behavior. Remember: decl means the initial value of your value is whatever was in memory before it got allocated. The only time you should use decl is large arrays/strings where you're going to immediately populate it. Incidentally, decl is likely going away in a future SourceMod version, so you really should be using new instead.

Static:

To my knowledge, static doesn't change performance in any way if you do it at the file level... it's solely a visibility operator.

At the function level, making a variable static has the side effect of making it never leave memory until your plugin is unloaded. This is because function-level static variables are actually global variables with the visibility locked to your function.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 10-20-2014 , 23:28   Re: Why are define macro function thingies bad?
Reply With Quote #4

Quote:
Originally Posted by Leonardo View Post
I think it just a bit slows down the compilation time, nothing else.

I don't see defined functions very useful tho.
Also, 0 < %1 <= MaxClients instead of 0 < %1 && %1 <= MaxClients.
0 < i <= MaxClients

true <= MaxClients

??

So does the overall size of a plugin have an impact on RAM? I don't think whole plugin files are loaded into RAM or something, are they? o.o (Not that I'm trying to argue for macros being better, this is just me being curious).

I already knew it inserts it everywhere which I figured was fine because ttttterabytes of space.

If decl is removed, does that mean

new String:string[256] = "as"; // will not initiate everything beyond what was initiated just like that wiki link says a decl'd string with initiation does right now?

ô.o?
__________________

Last edited by Chdata; 10-20-2014 at 23:37.
Chdata is offline
xerox8521
Senior Member
Join Date: Sep 2011
Old 10-22-2014 , 09:18   Re: Why are define macro function thingies bad?
Reply With Quote #5

Quote:
Originally Posted by Powerlord
decl is likely going away in a future SourceMod version
Why that ? I mean it would reduce certain private plugins performance if you have to use new instead of decl when using big strings

Altough this is more or less SA-MP specific http://forum.sa-mp.com/showthread.php?t=166680

Last edited by xerox8521; 10-22-2014 at 09:28.
xerox8521 is offline
rswallen
SourceMod Donor
Join Date: Jun 2013
Location: 127.0.0.1
Old 10-22-2014 , 09:21   Re: Why are define macro function thingies bad?
Reply With Quote #6

Future SM is also getting proper strings (as opposed to the char arrays currently in use)
__________________
rswallen is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 10-22-2014 , 09:59   Re: Why are define macro function thingies bad?
Reply With Quote #7

Dunno how sp works, but decl is supposed to be faster, since you don't initialize the data.
Static is supposed to be faster as well, since it is pre allocated and initilized, and their address is available directly.. When using other variables you will have to be grabbing them off of the stack, putting it into a register... which is slower.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
splewis
Veteran Member
Join Date: Feb 2014
Location: United States
Old 10-22-2014 , 16:25   Re: Why are define macro function thingies bad?
Reply With Quote #8

Quote:
Originally Posted by Chdata View Post
So does the overall size of a plugin have an impact on RAM? I don't think whole plugin files are loaded into RAM or something, are they? o.o (Not that I'm trying to argue for macros being better, this is just me being curious).
I'm hardly an expert, but I believe plugins are executed entirely out of memory. This is why you can delete a plugin from the plugins/ directory and it keeps working until it gets unloaded from memory on the map change.
__________________

Last edited by splewis; 10-22-2014 at 16:25.
splewis is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 10-22-2014 , 17:10   Re: Why are define macro function thingies bad?
Reply With Quote #9

Quote:
Originally Posted by splewis View Post
I'm hardly an expert, but I believe plugins are executed entirely out of memory. This is why you can delete a plugin from the plugins/ directory and it keeps working until it gets unloaded from memory on the map change.
I'm not an expert either, but I can tell you that's how programs traditionally work... for instance, on UNIX systems use the fork-exec model, with initial processes forking from the init/systemd process. The Exec family of system calls replace the currently running program with one it loads into memory.

While it could be different for plugins, it's easier to load the entire thing into memory if it's not using resource forks or the like.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 10-22-2014 at 17:13.
Powerlord is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 10-22-2014 , 21:53   Re: Why are define macro function thingies bad?
Reply With Quote #10

Alright I was confused because I figure programs do work that way, but I was introduced to programming via SNES assembly where all of the code is read straight from ROM.

And of course in that case we also kept track of variables by looking at individual RAM address like $0000 or $1EF9 or $7F0050 etc.
__________________
Chdata 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 16:05.


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