Constant folding optimisation for bytecode. This optimisation adds a new pseudo-opcode, LOAD_FOLDED_CONST, which encodes the type of a complex literal constant in its `arg` field, in a "typestruct" format described below. There is a corresponding function, build_folded_type, which constructs a vm type from the encoded typestruct. The type structure stored in LOAD_FOLDED_CONST is an immutable (for easy hashing) tree with the following elements: ('prim', <python type>) : a primitive type, e.g. ('prim', str) (tag, types) : a collection type; 'types' represent the type params frozenset(types): a union of types tag = prim | tuple | list | map | set the types python supports for a literal constant types = a tuple of type | frozenset(types) where the size of the tuple depends on the tag, e.g ('map', (k, v)) For ease of testing and debugging there is also a simplified literal syntax to construct and examine these typestructs, see constant_folding_test for examples. This is less uniform, and therefore not recommended to use other than for input/output.
Exception |
|
Errors raised during constant folding. |
Function | build |
Convert a typestruct to a vm type. |
Function | from |
Convert from simple literal form to the more uniform typestruct. |
Function | optimize |
Fold all constant literals in the bytecode into LOAD_FOLDED_CONST ops. |
Function | to |
Convert a typestruct item to a simplified form for ease of use. |
Constant | MAX |
Undocumented |
Class | _ |
A linear collection (e.g. list, tuple, set). |
Class | _ |
Build up a collection of constants. |
Class | _ |
A folded python constant. |
Class | _ |
Fold constant literals in pyc code. |
Class | _ |
Mapping from a folded opcode to the top level constant that replaces it. |
Class | _ |
A dictionary. |
Class | _ |
Build up a map of constants. |
Class | _ |
A simple opcode stack. |