Archive: Macros vs Functions


Macros vs Functions
Okay, I'm sure this has been asked before but I'll be damned if I can find it in either the wiki or the Forum (a google search looked promising [ http://www.fredshack.com/docs/nsis.html ]but the author of the page ultimately did not address the issue).

So, here goes.

Macros vs Functions
The basic question: when to use what, and why?
The more involved question: pros and cons to each? (likely to be part of 'why')

Now I can think of a couple off the top of my head, I'll just list the presumable pros (assume them to be cons for the opposite)

Macros: you can 'sorta' pass them parameters as you would in most scripting languages:


!macro parm1 parm2 parm3
something with ${parm1} ${parm2} ${parm3}
!macroend


Macros: run faster?
Now this one is a contentious one. In many languages, macros execute faster than functions. Does this hold true for NSIS, and does it really matter?

Functions: no code duplication
Macros insert the same code whereever !insertmacro is used, so the actual code is likely to be duplicated and make for a larger installer; though it's not likely to matter?

Functions: no worrying about goto labels
Many macros using a goto require labels to be uniqueified by manner of ${__LINE__} as otherwise labels may be duplicated. Even if it doesn't get used twice within the same section, the name of the label should be uniqueified as a basic label such as "loop" may already exist in the section the macro is inserted into. In a function, there is no such worry.

Macros & Functions: stack manipulation
it seems that both macros and functions require stack manipulation if you want to use variables - otherwise you may overwrite variable values from before the !insertmacro / before the !call .

I'm tempted as it is to make most of my macros functions with macro front-ends - e.g. have the main body of code be the Function, while using the macro to push elements to the stack, with those elements coming from the parameter passing. But given that I don't know the full Macros vs Functions story, I'm not sure if that will come bite me in the behind further down the road.

If there is indeed a wiki on this already, please point me to it. If there is a thread on it, please point me to it - and I'd be happy to turn it into a wiki page. If it's all just tiny tidbits scattered about, people - like myself - are never going to find them all to get a good grasp on macros vs functions.

You've got them all right. Macros are a bit faster because there's no Call or Return opcodes, but the difference is neglectable. Functions do take less space because there's no code duplication. However, compression might be able to take care of that.

There's also another issue of the File command. When used in functions, it won't add the file size to the section it's called from. When used in macros on the other hand, the code will actually be in the section so it will.

I think there might have been a few threads about some of those issues, but I don't remember one on the entire topic. A wiki page would be great.


Done for now with the Wiki;
http://nsis.sourceforge.net/Macro_vs_Function

I wanted to add it to the FAQ (I actually put it in that category), but doesn't look like I can edit that page - probably a good thing ;)

I'll run a spellchecker over it later, clean up some other tidbits where needed.. hopefully all the example code provided (even if they're just illustrative) isn't bogus, etc.


The FAQ automatically lists all pages in the appropriate categories. If it doesn't show up immediately, you can purge the page cache by appending "?action=purge" to the URL.

The page seems very extensive. Thanks for your work. I haven't had a chance to read it all yet, but I'll take a deeper look sooner or later.


Yeah, I thought I'd get into what Macros and Functions -are-(and their fundamental differences) before addressing what/where/problems. Thanks :) I'll re-check the FAQ and try the purge if it's not there yet.

Edit: no purge required, there 'tis.


Excellent info, thanks