Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Official ZPE/YASS documentationCompiling

ZPE has supported compiling code since the very first version. In fact, ZPE supported compiling before the runtime was even developed!

For those who do not know, compilation of programs is the process of turning the original script into something the computer can understand, in this case byte-code. ZPE does this in three processes:

  1. preparation of input code
  2. parsing of prepared input code
  3. applying lexical analysis on the parsed code
  4. conversion of each chunk of code into the appropriate byte-code representation and forming the FAST (Flat Abstract Syntax Tree).

When the -r ZAC is used, this is the first part of running the program. This means that each time the program is run these four steps need to be run before it can actually be run through the runtime or interpreter. It isn't slow, it just isn't as fast as if these steps could be avoided. And in fact, they can.

Compiling the program into a separate file makes them considerably faster to run. When we use the -r ZAC we are performing what we call Just-In-Time compilation, when we use the -c ZAC we are using optimised compilation. This kind of compilation works by converting the code into the byte-code and then running through it once to see where optimisations can be made, adding further to the performance gains. For example, assertions are not included in compiled programs to make performance even better. For loops are optimised so that if they are using a fixed value (non-conditional) they move to a better design. None of this is applied with the interpreted version.

The results were impressive in version 1.7.9 (first version to include full compiler optimisations) and yielded the following results for a for loop that looped 100,000 times:

zsh

time zpe for_loop_unoptimised.zen

real	0m20.555s
user	0m7.786s
sys	0m2.185s

time zpe for_loop_optimised.zen

real	0m17.576s
user	0m3.952s
sys	0m2.065s

When using the -c ZAC, it is possible to stop compiler optimisations being applied by adding the -optimise false to the command line.

Comments
Feedback 👍
Comments are sent via email to me.