Increasing performance with @def macro — Gideros Forum

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Increasing performance with @def macro

Maintainer
edited June 2016
Gideros Studio has powerful built-in feature: Lua code preprocessing with macros which is based on great work of Steve Donovan: https://github.com/stevedonovan/LuaMacro
You can use built-in macros or write your own. In this tutorial I will show you how to use built-in '@def' macro.

NOTE: To enable preprocessing you need to turn on 'Macro Support' in Settings of Gideros Studio first.

@def macro has two syntax forms:
 ```@def name body --> oneline @def (name --> multiline body )```
 ```@def HELLO "Hello, World!" print(HELLO) --> Hello, World```
We are defined a simple constant. Why constants are good? Because they have no performance overhead as variables have and they also don't occupy valuable variable space.

More complex example:
 ```@def CUBE(x) ((x)*(x)*(x)) local n = 5 print(CUBE(n)) --> 125```
Instead of 'function CUBE(x) return x * x * x end' (which is expensive in this case) that macro-function will replace all xs with any given expression i.e. will be unrolled to 'n * n * n'.

Unlike C LuaMacros are lexically scoped and their expansion controlled by Lua itself:
 ```@def x 1 do @def x 2 print(x) --> 2 end print(x) --> 1```
Another macro-function example:
 ```@def (complain(msg,n) for i = 1,n do print msg end ) complain("Hello!", 5) --> print "Hello" 5 times```
As you can see @def macro is well suited to inline optimized Lua code. It's also well known to programmers because of C 'define' directive which is frequently used.

You can find other macros in 'Gideros/Tools/luamacro/macro/' folder and if you want to write new macro you must put it here. To require them in your code use @require:
 ```@require "do" y = 0 @do(i, 1, 10, y = y + 1) print(y)```
+1 -1 (+8 / -0 )Share on Facebook

• Maintainer
Great tutorial, I hate the fact that Lua doesn't have constants - this fixes it.

Likes: antix

Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
https://deluxepixel.com
+1 -1 (+1 / -0 )Share on Facebook
• Member
edited June 2016
Awesome stuff

Doesn't seem to work at all with ZeroBrane Studio
• Maintainer
Yes, macros could be added to it though.

Likes: antix

Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
https://deluxepixel.com
+1 -1 (+1 / -0 )Share on Facebook
• Member
edited June 2016
Beforw I get too excited, questions!

When would this be used practically, in a game? Could it replace local variables that don't change? What kind of performance boost will this give?
My Gideros games: www.totebo.com
• Member
@totebo - yes local variables that never change = constants. It's hard to know what kind of boost you will get but you will get something.
• Maintainer
edited June 2016
@totebo In Lua you have a limited number of variables - these are not variables - before Lua gets the code to process they are converted to whatever you define them as.

eg
 ```@def start_lives 3 . . . local lives=start_lives```
will become just
 `local lives=3`
before Lua gets to see it.

It also uses one less variable than
 ```local start_lives=3 . . . local lives=start_lives```
And because Lua converts code into bytecode, it's possible that:
 `local lives=3`
is faster to execute than
 `local lives=variable_name`
Because it doesn't have to search through the variable name table to find the value of the variable. The other very, very, very slight optimisation is that there is also no
 `local start_lives=3`
statement anymore, so you also init one line faster!

Likes: antix, totebo

Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
https://deluxepixel.com
+1 -1 (+2 / -0 )Share on Facebook