module documentation

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 ConstantError Errors raised during constant folding.
Function build_folded_type Convert a typestruct to a vm type.
Function from_literal 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_literal Convert a typestruct item to a simplified form for ease of use.
Constant MAX_VAR_SIZE Undocumented
Class _Collection A linear collection (e.g. list, tuple, set).
Class _CollectionBuilder Build up a collection of constants.
Class _Constant A folded python constant.
Class _FoldConstants Fold constant literals in pyc code.
Class _FoldedOps Mapping from a folded opcode to the top level constant that replaces it.
Class _Map A dictionary.
Class _MapBuilder Build up a map of constants.
Class _Stack A simple opcode stack.
def build_folded_type(ctx, state, const): (source)

Convert a typestruct to a vm type.

def from_literal(tup): (source)

Convert from simple literal form to the more uniform typestruct.

def optimize(code): (source)

Fold all constant literals in the bytecode into LOAD_FOLDED_CONST ops.

def to_literal(typ, always_tuple=False): (source)

Convert a typestruct item to a simplified form for ease of use.

MAX_VAR_SIZE: int = (source)

Undocumented

Value
64