-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Mypyc Implementation Overview
Mypyc compiles a Python module (or a set of modules) to C, and compiles the generated C to a Python C extension module (or modules). You can compile only a subset of your program to C -- compiled and interpreted code can freely and transparently interact. You can also freely use any Python libraries (including C extensions) in compiled code.
Mypyc will only make compiled code faster. To see a significant speedup, you must make sure that most of the time is spent in compiled code -- and not in libraries, for example.
Mypyc has these passes:
-
Type check the code using mypy and infer types for variables and expressions. This produces a mypy AST (defined in
mypy.nodes
) and a type map that describes the inferred types (mypy.types.Type
) of all expressions (as PEP 484 types). -
Translate the mypy AST into a mypyc-specific intermediate representation (IR).
- The IR is defined in
mypyc.ir
(see below for an explanation of the IR). - Various primitive operations used in the IR are defined in
mypyc.primitives
. - The translation to IR happens in
mypyc.irbuild
. The top-level logic is inmypyc.irbuild.main
.
- The IR is defined in
-
Insert checks for uses of potentially uninitialized variables (
mypyc.transform.uninit
). -
Insert exception handling (
mypyc.transform.exceptions
). -
Insert explicit reference count inc/dec opcodes (
mypyc.transform.refcount
). -
Translate the IR into C (
mypyc.codegen
). -
Compile the generated C code using a C compiler (
mypyc.build
).
Details of the mypyc implementation are described in these pages: