-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tools : PyTeal optimization utility #247
Conversation
pyteal/compiler/compiler.py
Outdated
@@ -1,4 +1,7 @@ | |||
from typing import List, Tuple, Set, Dict, Optional, cast | |||
from pyteal.compiler import optimizer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@algoidurovic Unrelated to the line: Since we're mostly agreed on the optimization to-be-provided, can you update the user guide?
Nominally, https://pyteal.readthedocs.io/ ought to explain how users can opt-in for optimization.
Co-authored-by: Michael Diamant <[email protected]>
Co-authored-by: Michael Diamant <[email protected]>
Add missing exponentiation operation in document
* updating to use new syntax for seq * rewording
* adding exports directly to top level __all__ * apply black formatter * adding initial generate script * fmt * rm all from all * adding check to travis * reading in original __init__ and using its imports, dont write to filesystem if --check is passed * make messages more profesh * fix flags after black formatted them * y * flippin black formatter * help text fix * asdfasdf
This reverts commit 5636ccd.
This reverts commit 7cb7b9a.
This reverts commit cc405a5.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have one concern that must be addressed before releasing this:
- The optimization looks correct except for the case where a user makes a
ScratchVar
with a specifically requested slot ID. In that case, we need to respect that slot ID and not optimize the scratch slot away, so this optimization should likely not touch anyScratchSlot
with a user-defined ID. Based on the current code, this is not checked.- Item 1 from the next list would be one way to solve this, since if you operate on
ScratchSlot
objects you can tell if they're user-defined or not.
- Item 1 from the next list would be one way to solve this, since if you operate on
And a few that need not be addressed now, but should be before more optimization work takes place. We can talk about these in more detail next week if you'd like.
- This optimization happens after
ScratchSlot
objects have been assigned integer IDs. This step of the compilation will fail if more than 256 scratch slots are in use, so it would be more useful if this optimization can be applied before that happens. I realize the flow of the compiler as it is currently doesn't make this easy, but it should be possible to reorder compilation events to make this happen. - Currently this optimization happens on the linearized list of
TealComponent
s. For this simple optimization, that's acceptable, but anything more complex will need access to more control flow information than is available at this stage and should probably operate with the control flow graph. For that reason I'd recommend changing this optimization to also operate onTealBlock
s before we implement more advanced features.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some minor comments here and there after a second pass on implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, thanks for addressing all concerns!
This PR implements the first iteration of the PyTeal optimization utility. The first optimization to be included is the removal of contiguous store/load opcodes that are deemed redundant.
Background:
PyTeal often uses ScratchSlots implicitly to bind sections of the source code together. This pattern is best illustrated by the implementations of PyTeal subroutines and MultiValue expressions. The issue arises at the TEAL level when ScratchSlots are utilized naively, leading to redundant store/load opcodes (ie the ops cancel each other out) that can be removed without affecting the behavior of the program.
The conditions for applying this optimization are very simple -- and perhaps unnecessarily narrow:
[store i, load i]
where i is the index of a scratch slot.Testing:
The unit tests cover both the optimization of hardcoded low level teal and the optimization of teal generated by the compiler from a PyTeal program. Tests cover subroutines, MultiValue expressions, ScratchVars, and more.
In order to gauge the practical utility of this optimization, I applied it to the Algorand Auction Demo. Unfortunately, the scope of the optimization was too narrow to apply to the demo. Further investigation (market research?) is needed here to determine a fruitful direction to take the optimizer in the future.