Compiling Black with `mypyc`

Date: 6/1/2022 · Tags: #python, #good-reading

What is mypyc?

Mypyc compiles Python modules to C extensions. It uses standard Python type hints to generate fast code.

My first view:

  • Pros:
    • No other languages (C/Cython/Rust), just gradually typed Python variant.
    • Fast program startup via AOT (ahead-of-time) compilation to native code
    • Strict runtime type checking
    • Develop in interpreted mode for a quick edit-run cycle. Release codes in compiled mode. Optionally, include a fallback interpreted version for platforms that mypyc doesn’t support.
  • Cons:
    • Only support major primitive types and part of native operations
    • No information about how to adopt with concurrency threading/asyncio
    • No generator expressions and arbitrary descriptors
    • Could be demanding when your codes relies on untyped std libraries or third-party libraries which will slow down your efforts.

Hence, mypyc isn't an alternative compared with cython which target is to improve concurrency or speed up numeric performance (actually, nothing matched when I searched concurrency/thread in mypyc doc), but still worth looking forward to enhance pure Python codebase or toolchains runned as single executable binary.

References:

Let's see an excellent example that how mypyc let black has doubling performance.

Compiling Black with mypyc