Module compiler

Source
Expand description

This module compiles parsed Expressions into an optimized AST node called an Instruction. The compiled form is much faster, especially for constants.

§Compile-time Optimizations

§Constant Folding

Operations with constants can be calculated at compile time so they don’t need to be calculated during eval().

For example, 1 + x + 1 will be compiled into x + 2, saving some time during eval().

If the entire expression only consists of constants (no variables), then the expression can be reduced to a final result at compile time, resulting in near-native speed during eval().

§Algebraic Simplification

  • Subtraction is converted to Addition.
  • Division is converted to Multiplication.
  • Built-in functions with constant arguments are evaluated.
  • Constant terms are combined.
  • Logical operator short-circuits are applied and no-op branches are discarded.

§Optimized Memory Layout and Execution

  • Variable-length Expression/Value AST nodes are converted into constant-sized Instruction nodes.
  • The IC enumeration helps to eliminate expensive function calls.

Structs§

InstructionI
An InstructionI represents an index into Slab.cs.instrs.

Enums§

IC
This enumeration boosts performance because it eliminates expensive function calls for constant values.
Instruction
An Instruction is an optimized AST node resulting from compilation.

Traits§

Compiler
You must use the Compiler trait before you can call .compile() on parsed Expressions.