-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Dynamic re-ordering of vars to optimize stack usage #49
Comments
Dynamic re-ordering of vars to optimize stack usageA big cost when doing loop-heavy stuff is ensuring that the stack is in the same order as it was before the loop. If the vars [a, b, c] are in the stack before the loop, then at the end of the loop they're in the order [b, a, c], we have to shift the stack around to accomodate that. For example, here's some fibonnaci code and the output:
Output:
Note the "targeting the stack" thing, where we have to re-order the elements on the stack, to be the same as before the loop, for the next loop to work correctly. We have some extremely naive cost-estimation code that just counts the number of instructions that will be outputed (multiplied by N when in a repeat.N instruction), the cost for this code is 124. If you just re-order the initial vars a bit, like this:
Then you get a different output at the end of the loop:
Note the "No need to target the stack, same order" thing at the end of the loop. The (again, extremely naive) estimated cost for this is 94, which is 25% less, but the real savings would be even higher, because shifting stuff around on the stack is fairly expensive, afaik. So once the cost estimation stuff gets more accurate that should be even better. So instead of having to move yul code around by hand to trigger this optimization, we can do some sort of dynamic programming approach that tries every order. This wouldn't just have cost savings for loops like above. There's a lot of stack re-ordering, pushing to memory, popping from memory, etc. that happens, and varying the order of things would affect all of that. |
No description provided.
The text was updated successfully, but these errors were encountered: