Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't generate multiple VM constants for variables with aliases
While poking around with Numbat's VM, I noticed if there's a variable with aliases, like so: ``` @Aliases(foo2) let foo = 2 m ^ 3 ``` then that is (hand-waving a bit here) compiled down as if it were this: ``` let foo = 2 m ^ 3 let foo2 = 2 m ^ 3 ``` leading to redundant VM constants (e.g. two 2 constants), redundant calculations, and even possibly different values if the right-hand side of the assignment is not pure (if it contains `random()` for instance). This commit resolves that duplication by using the variable's name and all its aliases when resolving identifiers to locals, instead of pushing a separate local for each alias. While at it, I've also removed the `depth` field from the `Local` struct, as it is equivalent to the index of the vector of locals in which the local resides. The only place in which the field is used is completely gratuituous anyway (the condition always evaluates to true since all locals in `self.locals[d]` have the depth `d`). There are no real performance improvements from this change, but it does give a nice reduction in the number of instructions and constants generated by the bytecode writer. Specifically, for a very simple Numbat program that just evaluates `1`, the number of constants goes down from 780 to 736, and the number of instructions in the <main> .CODE section goes down from 1235 to 1055 instructions.
- Loading branch information