Using Static Evaluation for Optimization

Put simple Tips and Tricks that are not entire Tutorials in this forum
Posts: 91
Joined: 20 May 2014, 21:24

Using Static Evaluation for Optimization

07 Sep 2019, 16:53

As far as I know, this technique is not widely known. I apologize if I am mistaken.

It is helpful to be able to evaluate code before run-time.

This is normally achieved with internal (e.g. Lisp-like macro) or external code generation.

In AutoHotkey, you can achieve something similar by assigning to a static variable inside a procedure or a global variable inside the auto-execute section.

Code: Select all

GLOBAL_VAR := StrSplit("foo, bar, baz, quxx", [", "])

    static LocalVar := StrSplit("foo, bar, baz, quxx", [", "])
    return LocalVar
In this case, the expression (StrSplit("foo, bar, baz, quxx", [", "]) here) will be evaluated every time the program executes instead of only when the code was generated, but it is still better than evaluating it repeatedly.

Although this might seem obvious, its uses might not.

I had been planning to use this technique to parse human readable strings representing a function’s signature into a data structure to be used for error reporting. Avoiding repeatedly converting the string into a data structure would have allowed the code to be easy to understand while still being efficient. Abandoning the project is one reason I decided to write about this trick. I did not want it to be lost.

It is also useful for making debugging easier. Most types cannot be converted to strings in AutoHotkey, so I frequently have to resort to single-stepping through a procedure to understand what is going wrong. If you are certain a problem is not in a particular expression and you can statically evaluate it, which requires it to be a constant expression, then assigning it to a static variable will keep you from having to single-step through that part of the code repeatedly.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: Google [Bot] and 3 guests