Skip to content

Advanced compiler optimisation #34

Open
@rkalis

Description

@rkalis

In #18 I listed several potential compiler optimisations of which the lowest hanging fruit was implemented in 0.2.0. The remaining optimisations are documented below, although this list might be updated in the future.

  • Improved stack ordering. By looking at the script we know when values are needed by certain opcodes. If we are able to order the initial stack correctly, there is no need to OP_PICK or OP_ROLL these values when they're needed, as they are already available at the top of the stack.
  • Simplification. If a contract contains e.g. 2 + 2 == 4, we already know beforehand that this is going to evaluate to true, so this whole expression can be simplified to just true. However, most well-implemented contracts will likely not have many of these instances, as the implementer can manually do a lot of this simplification themselves. So the benefit of implementing this optimisation is questionable.
  • Improved deep replacement. When a variable is reassigned inside a conditional block, we replace the variable at its original position in the stack. This uses an algorithm that sort of digs into the stack with OP_SWAP OP_TOALTSTACK, and then puts all other values back where they belong with OP_FROMALTSTACK. This can be improvedby using more specific stack operations for specific depths rather than OP_SWAP.
  • Inlining of single-use variables. Some variables are assigned and then only used once in the remaining code. Right now they are pushed to the stack when they are assigned and then retrieved from the stack when they are used. This could be changed to pushing these values to the stack when they are used instead. This would save the opcodes required to retrieve the value (e.g. OP_6 OP_ROLL).

Metadata

Metadata

Assignees

No one assigned

    Labels

    breakingbreaking changecashc-compilerRelates to the cashc compiler

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions