Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3k
Mypyc C Code Generator
Mypyc compiles a function into two functions, a native function anda wrapper function:
The native function takes a fixed number of C arguments with thecorrect C types. It assumes that all argument have correct types.
The wrapper function conforms to the Python C API calling conventionand takes an arbitrary set of arguments as Python objects. It processes the arguments,checks their types, unboxes values with special representations andcalls the native function. The return value from the native functionis translated back to a Python object ("boxing").
Calls to other compiled functions don't go through the Python modulenamespace but directly call the target native C function. This makescalls very fast compared to CPython. This is calledearly binding.
Currently early binding is only possible between modules that are compiled together.If you, for example, install a module compiled with mypyc using pip, and useit in your code, calls to the installed module will use slow Python-levelcalls.
The generated code does runtime type checking so that it can assume thatvalues always have the declared types. Whenever accessing CPythonvalues which might have unexpected types we need to insert a runtimetype check operation. For example, when getting a list item we need toinsert a runtime type check (an unbox or a cast operation), sincePython lists can contain arbitrary objects.
The generated code uses various helpers defined inmypyc/lib-rt/CPy.h
. The implementations are in various.c
filesundermypyc/lib-rt
.