Version 2 (modified by 11 years ago) ( diff ) | ,
---|
Writing efficient MetaModelica code
In the bootstrapped compiler, together with the extended MetaModelica/Modelica things you can use there are some restrictions:
- avoid
matchcontinue
as much as possible and usematch
instead, combined with tail recursion if needed (functions where you expect to maybe iterate over more than 50 elements should be tail-recursive). - Write tail recursive functions: if a function calls itself it should do that as last thing in the then part or in the last statement. You are required to bind all outputs in the same order as the function outputs or tail recursion does not work (so no wildcards ignoring one output).
- Inlining functions can be used to great effect, but it interferes with separate compilation so only use it within a module.
- Use builtin functions whenever possible: they have implementations that are better than you can achieve using MetaModelica code (for example: stringAppendList and stringDelimitList only use a single memory allocation, list reduce,map, and filter using the built-in operator avoid the extra listReverse)
- Avoid using the construct
case x equation true = fn(x); then (); case x equation false = fn(x); then ();
. The bootstrapped compiler will not merge the two cases into one and algorithms that ran in linear time using RML might run in quadratic time using the bootstrapped compiler if you do this. It also precludes optimisations such as tail recursion because you usematchcontinue
instead ofmatch
. Usecase x guard fn(x) then ()
instead; it is possible to use this withmatch
.
Note:
See TracWiki
for help on using the wiki.