Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Increasing performance with @def macro - Gideros Forum

Increasing performance with @def macro

n1cken1cke Maintainer
edited June 2016 in Step by step tutorials
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:
<a href="/profile/def">@def</a> name body --> oneline
<a href="/profile/def">@def</a> (name --> multiline
  body
)
Let's start with simple example:
<a href="/profile/def">@def</a> 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:
<a href="/profile/def">@def</a> 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:
<a href="/profile/def">@def</a> x 1
do
  <a href="/profile/def">@def</a> x 2
  print(x) --> 2
end
print(x) --> 1
Another macro-function example:
<a href="/profile/def">@def</a> (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:
<a href="/profile/require">@require</a> "do"
y = 0
<a href="/profile/do%28i">@do(i</a>, 1, 10, y = y + 1)
print(y)
+1 -1 (+8 / -0 ) Share on Facebook

Comments

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

    Likes: antix

    +1 -1 (+1 / -0 ) Share on Facebook
  • antixantix Member
    edited June 2016
    Awesome stuff :)

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

    Likes: antix

    +1 -1 (+1 / -0 ) Share on Facebook
  • totebototebo 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
  • antixantix 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.
  • SinisterSoftSinisterSoft 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
    <a href="/profile/def">@def</a> 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

    +1 -1 (+2 / -0 ) Share on Facebook
Sign In or Register to comment.