Note:
Please keep in mind that ActiveSpecializer is an experimental project, use it cautiously.
It doesn't support lambda expressions and may have difficulties with specializing non-trivial instances.

Simple Calculator

We took this Parsec calculator tutorial and adapted it a little bit
for ActiveSpecializer. In the original tutorial Parsec returns parsed expressions as double values:

ActiveSpecializer shows its bests with tree-like data structures. So we will parse expressions to AST:

Assume we have a simple equation 3 + 2 * 4. According to the parser, the following AST will be created:

graph TD
+ --> 3
+ --> *
* --> 2
* --> 4

Let’s test Specializer out:

ActiveSpecializer transforms the AST to a set of static final classes with baked-in values of the provided
equation. JIT heavily optimizes and inlines these classes while runtime. As a result, we receive an optimized expression
instance that can be reused in case we calculate an equation with an unknown value several times.

It’s time for some benchmarks. Let’s try to process an equation
((2 + 2 * 2) * -x) + 5 + 1024 / (100 + 58) * 50 * 37 - 100 + 2 * x ^ 2 % 3 in three different ways and compare the performance:

manually enter the equation

parse the equation to an AST and evaluate it without specialization

parse the equation to an AST and evaluate it with specialization

The results of the benchmark are very illustrative:

As you can see, a manually typed equations and specialized AST were processed equally fast. ActiveSpecializer sped
up AST processing 8 times.