gno
gno copied to clipboard
feat(VM): propagate values from immutable variables
If variables have constant values of a primitive type and they are never mutated (or mutated by constant expressions), just like values from constants are currently propagated, we can do the same for this set of variables. This is an optimization to avoid computation like searching for the value through the name valuepath during runtime.
Conditions for propagation
- There is no mutation
- There are no references given out
- The initial value during definition is a literal
- The type of the value is primitive
This must be done in 2 passes. First pass needs to establish which variables adhere to these conditions and mark them. The second pass can do this propagation based on that metadata.
Here is an example.
func Foo(b int) {
a := 5
if a > 1 {
println(a)
}
}
This code would turn into
func Foo(b int) {
a := 5
if 5 > 1 {
println(5)
}
}