Nasal performance and benchmarks

From FlightGear wiki
Jump to navigation Jump to search
This article is a stub. You can help the wiki by expanding it.

This article shall collect best practices for coding in Nasal based on regular performance tests.

Motivation

Over the years many things have been improved so some old findings regarding performance are not true anymore. Regular benchmarking and updating should help the FlightGear community to have a clear picture of which code constructs perform well. Sometimes performance and "good" coding style (e.g. readability, re-usability, maintainability) might be in conflict.

Benchmark add-on

The benchmark add-on can be downloaded here. It is basically a collection of nasal snippets to be tested. A minimal GUI (menu item, simple dialog box) allows to start some benchmarks.

A benchmark test runs some function multiple time (repeat count) and measures the time taken (result). The results of any benchmark is currently printed to the console, so you have to start FlightGear with console window to see this. (The repeat count should be configurable via the GUI, this is on my to do list.)

Performance analysis

Performance of Nasal snippets is tested by means of the Benchmark add-on which makes use of the debug.benchmark_time() function. The default repeat count for tests is 10000 (ten thousand). Depending on your hardware some tests may block your Flightgear many seconds. On Windows you may see the usual warnings about FlightGear not reacting any more. In the next section available benchmarks are described. Test results follow below.

Results (time taken to complete a benchmark test) obviously depend on the hardware used, so the relevant information is not the absolute time but the ratio when comparing two code snippets. Also, one should repeat benchmarks a couple of times and see if results are in the same order of magnitude. Benchmarks should be run only after FlightGear is fully ready, e.g. scenery and aircraft model loaded etc.

currently available benchmarks

simpleClass

Object oriented programming (OOP, "programming with classes") gives several advantages (readability, re-usability and easier maintainance of code, to give just some examples) but might generate slightly slower code. For most real-world cases this should be neglectible, nevertheless it is a simple, easy to understand test scenario.

var simpleClass = {
    new: func {
        var obj = {
            parents: [simpleClass],
            value: 0,
        };
        return obj;
    },
    
    set: func(value) { me.value = value; },
    get: func { return me.value; },
};

var c = simpleClass.new();

var test_get_method = func {
    var x = c.get();
}
var test_get_direct = func {
    var x = c.value;
}
var test_set_method = func {
    c.set(1);
}
var test_set_direct = func {
    c.value = 1;
}

property tree access

As the property tree is the central data exchange in FlightGear it is worth to know how to efficiently access it. This benchmarks compares the getprop() agains the props.Node.getValue().

var path = "/_benchmark/getprop/prop";
var myprops = [];
append(myprops, props.getNode(path, 1));

var test_getvalue = func {
    myprops[0].getValue();
}
var test_setvalue = func {
    myprops[0].setValue(21);
}

var test_getprop = func {
    getprop(path);
}
var test_setprop = func {
    setprop(path, 42);
}

var init = func {
    for (var i = 0; i < repeat; i += 1) {
        append(myprops, props.getNode(path~i, 1));
    }
}
printResult("test_getNode", debug.benchmark_time(init, repeat));
printResult("test_getvalue", debug.benchmark_time(test_getvalue, repeat));
printResult("test_getprop", debug.benchmark_time(test_getprop, repeat));

Canvas API - Text class

tbd

Results 2020-04

simpleClass

Repeat count = 100000

Interpretation: using class methods can be 2-3x slower but is still ok.

Test Time [s] Comment
get via class.get() 0.080
get via class.variable 0.040
set via class.set() 0.120
set via class.variable 0.040

Property access

Repeat count = 100,000

It seems that any operation involving a property path (string) is relativly slow. If you have to access a property more than once, e.g. in an update loop, you should create a props.Node object outside the loop and use object.getValue().

Test Time [s] Comment
props.getNode(path,1) 0.400 getting the node object seems expansive...
node.setValue() 0.100 ... while using it is fine
node.getValue() 0.100
setprop() 0.090 - 0.160
getprop() 0.090 - 0.170

Canvas API - Text class

Optimized canvas API with caching and props.Node (not merged to FGDATA at the time of writing). Older implementation below for reference. Repeat = 50,000

Test Time [s] Comment
setText() same value 0.046
setText() different value 0.561
Old
setText() same value 0.330
setText() different value 0.600
setTextFast() same value 0.130
setTextFast() different value 0.460
updateText() same value 0.040
updateText() different value 0.670

References

Regression tests

Articles

See also

Discussions

References