Nasal loops and timers

From FlightGear wiki
Jump to navigation Jump to search

Nasal has several ways to implement an iteration.

A polling loop is akin to somebody permanently running to a room to check if the lights are on - a listener is like somebody being INSIDE the room SLEEPING and only WAKING up once the lights are turned on (there is no active workload involved here, other than basically "watching" the switch).

The setlistener() API is intended to catch rare events. Avoid complex loops if you don't have to.

In general, "loops" are not bad or expensive, it really depends on what you're doing inside the loop. A loop will be executed within a single frame normally - so a long-running loop will add up to the frame spacing (the time needed to create a single frame).

There's nothing magic about timers or listeners - they can just as well inflate your frame spacing.

It doesn't matter if the code/callback is run inside a loop, timer or a listener - what does matter is the complexity of the code that runs.

for, while, foreach, and forindex loops

Nasal's looping constructs are mostly C-like:

for (preloop_initialization;   # will be run prior to the first invocation of the loop, usually to initialize a loop counter
     condition_during_loop;    # will be run prior to each iteration, usually to check the loop counter  and cancel the loop if false
     post_iteration_expression # will be run after each iteration, usually to increment a loop counter
) {
    # loop body
}
for (var i = 0; i < 3; i += 1) {
    # loop body
}

while (condition) {
    # loop body
}

The differences are that there is no do{}while(); construct, and there is a foreach/forindex, which takes a local variable name as its first argument and a vector as its second:

foreach (elem; list1) { # NOTE: the delimiter is a SEMICOLON ;
    doSomething(elem);
}

In other words, even though foreach or forindex look like function calls, they work differently and they have braces after the parentheses.

The hash/vector index expression (one that uses brackets) is an lvalue that can be assigned as well as inspected:

foreach (light; lights) {
    lightNodes[light] = propertyPath;
}

To walk through all elements of a hash, use a foreach loop on the keys of they hash. Then you call pull up the values of the hash using the key. Example:

var myhash = { first: 1000, second: 250, third: 25.2 };
foreach (var i; keys(myhash)) {
    # multiply each value by 2:
    myhash[i] *= 2;

    # print the key and new value:
    print(i, ": ", myhash[i]);
}
# this will print in some order:
# first: 2000
# second: 250
# thid: 25.2

There is also a "forindex", which is like foreach except that it assigns the index of each element, instead of the value, to the loop variable.

forindex (i; list1) {
    doSomething(list1[i]);
}

Also, braceless blocks work for loops equally well:

var c = 0;
while (c < 5)
    print(c += 1);
print("end of loop\n");

skip or exit loops prematurely

If you want to skip a single loop use "continue" or to exit a loop prematurely use "break".

for (var i = 0; i < 10; i += 1) {
    # loop body

    # don't do loop for i == 3
    if (i == 3) {
        continue;
    }

    # exit for other reason
    if (want_to_quit) {
        break;
    }
}

settimer loops

Caution
Cquote1.png it's relatively easy to do bad things unintentionally. Like tie a bit of code to an FDM property and run updates of a display 120 times per second rather than the 30 times you actually need. Like start a loop multiple times so that you update the same property 30 times per frame rather than the one time you actually need. It's actually pretty hard to catch these things, because the code is formally okay, does the right thing and just eats more performance than necessary, and there's no simple output telling you that you're running 30 loops rather than the one you expect.
Cquote2.png
Note  See the #maketimer section for better control over timers

Loops using while, for, foreach, and forindex block all of FlightGear's subsystems that run in the main thread for the duration of the loop body, and can, thus, only be used for instantaneous operations that don't take too long.

For operations that should continue over a longer period, one needs a non-blocking solution. This is done by letting functions call themselves after a timed delay:

var loop = func {
    print("this line appears once every two seconds");
    settimer(loop, 2);
}

loop();        # start loop

Note that the settimer() function expects a function object (loop), not a function call (loop()) (though it is possible to make a function call return a function object--an advanced functional programming technique that you won't need to worry about if you're just getting started with Nasal).

The fewer code FlightGear has to execute, the better, so it is desirable to run loops only when they are needed. But how does one stop a loop? A once triggered timer function can't be revoked. But one can let the loop function check an outside variable and refuse calling itself, which makes the loop chain die off:

var running = true;
var loop = func {
    if (running) {
        print("this line appears once every two seconds");
        settimer(loop, 2);
    }
}

loop();        # start loop ...
...
running = false;   # ... and let it die

Loop Identifiers

The example above has a fundamental problem; which is that it isn't possible to manage the timer as it will always fire (which is why it is better to use #maketimer). However the problem can also be solved by using a loop identifier. This is simply a variable that lives at the outer scope of the module and is used to identify the currently active timer loop. Using this technique when the loop identifier is incremented the currently waiting timers will exit when it is their turn to run and the timer chain will be broken.

To do this we provide each loop chain with a loop identifier and let the loop chain function will terminate itself at the start if the id doesn't match the loop-id from the outer loop. To facilitate this also requires a delegate in the settimer call that will pass in the current loop id (that was passed into the loop chain function).

Example of using the loop indentifier technique

 var loopId = 0;
 var loop = func(id) {
     id == loopId or return;           # stop here if the id doesn't match the global loop-id
     ...
     settimer(func { loop(id) }, 2);   # call self with own loop id
 };
 
 loop(loopId);       # start loop
 ...
 loopId += 1;        # this kills off all pending loops, as none can have this new identifier yet
 ...
 loop(loopId);       # start new chain; this can also be abbreviated to:  loop(loopId += 1);

Starting timers from a fdm initialized listener

Caution  Be carfeul when using settimer from a listener on fdm-initialized as this can easily create a new timer each time the aircraft is repositioned

The correct way to use settimer from a listener on fdm-initialized is as follows:

var init = false;

var goodLoop = func {
    # logic
    settimer(goodLoop, 1);
};

setlistener("sim/signals/fdm-initialized", func {
    if (!init) {
        init = true;
        goodLoop();
    }
});

maketimer

You can use the maketimer() API since FlightGear 2.11.

maketimer example

var loopExample = func {
    if (someCondition) {
        timer.restart(0.1); # Adjust the timer frequency (ms)
    }
    
    if (finished) {
        timer.stop(); # Cancel the timer. Timer can be started again later.
    }
};

var timer = maketimer(0.25, loopExample);
timer.simulatedTime = 1; # Use simulated time, as maketimer defaults to using wallclock time and continues during pause.
timer.start();

maketimer from FDM Initialized

var timerLoop = func {
    # logic
};

var timer = maketimer(0.25, timerLoop);

setlistener("sim/signals/fdm-initialized", func {
    timer.start();
});

See also maketimer() settimer()

Loops vs listeners

Timers or listeners are only really preferable over loops when it comes to checking for some condition, because polling is called "busy-waiting", i.e. more expensive, see the previous analogy.

A listener or timer "waiting" is not resource-hungry, it's not even busy - it's not doing anything until it is "fired". Regarding setprop() /getprop() - they're not as bad as we used to think - in fact, Thorsten has shown that they're preferable over most props.nas APIs, this may however change once the whole thing is replaced with cppbind bindings.

Thus, loops aren't bad necessarily: they can be used in a less-than-optimal manner, but there are often times where they make a lot of sense. Some pros and cons of both:

Listeners: Pros:

  • Can be used to receive instant "notifications" (events), avoiding unnecessary gets,
  • Really useful for Updating another property based on changes in one - like a mirror that scales by a factor, or something. (It's kinda a pity we can't just redirect read/writes... That's something I haven't explored enough, since it's various parts of C++)

Listeners: Cons:

  • Each event is run in the same code as the event that triggers it (aka the setting-a-property code calls the event), so each event is run in the same thread and has the possibility to block the parent - which is probably not a good idea, so listeners should generally run as little code as possible. Note however that, depending on the property, it's all going to get run inside the main loop anyway - so it's no different than loops (roughly speaking).
  • May be run several times per frame.
  • Little more abstract and prone to danger than loops; you do not always know what listeners are registered where, and any listener that writes to a different property has the potential to infinitely call each other and thus segfault. (Unless you make guards on your listener against this... which is possible, but requires some work.)

Loops: Pros:

  • Very useful for running loop-like tasks - e.g. something that updates regularly, particularly if it looks at many properties, or if it changes even if the dependent-upon property(s) are not updated.
  • Run once a frame at max – which is the fastest the user will see the changes anyway.

Loops: Cons:

  • However, they are run at the end (IIRC) of the frame, so if you need instant reaction (i.e. interaction back and forth with another subsystem via properties), loops won't be quick enough, and thus listeners would be required.