Optimisation: What is Variable Expansion

Variable expansion is a technique similar to Loop Unrolling. However, instead of simply unrolling the loop, it modifies the loop to allow multiple steps of each loop to be computed together.

Example:

int val = 0;
for(int i =0; i < n; i++)
val += arr[i]

Becomes:

int val = 0
for(int i =0; i < n; i+=2)
{
val += arr[i]
val += arr[i+1]
}

This optimisation, like loop unrolling, reduces the burden of the computation for the logic of the loop. It also allows for further optimisation by allowing for the stages of each loop to be considered for optimisation together in a simpler form.

This type of optimisation is great for loops that cannot be unrolled but could still benefit from trading off some code space for some performance.