introduction.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. Introduction
  2. ============
  3. Mypyc compiles Python modules to C extensions. It uses standard Python
  4. `type hints
  5. <https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html>`_ to
  6. generate fast code.
  7. The compiled language is a strict, *gradually typed* Python variant. It
  8. restricts the use of some dynamic Python features to gain performance,
  9. but it's mostly compatible with standard Python.
  10. Mypyc uses `mypy <https://www.mypy-lang.org/>`_ to perform type
  11. checking and type inference. Most type system features in the stdlib
  12. `typing <https://docs.python.org/3/library/typing.html>`_ module are
  13. supported.
  14. Compiled modules can import arbitrary Python modules and third-party
  15. libraries. You can compile anything from a single performance-critical
  16. module to your entire codebase. You can run the modules you compile
  17. also as normal, interpreted Python modules.
  18. Existing code with type annotations is often **1.5x to 5x** faster
  19. when compiled. Code tuned for mypyc can be **5x to 10x** faster.
  20. Mypyc currently aims to speed up non-numeric code, such as server
  21. applications. Mypyc is also used to compile itself (and mypy).
  22. Why mypyc?
  23. ----------
  24. **Easy to get started.** Compiled code has the look and feel of
  25. regular Python code. Mypyc supports familiar Python syntax and idioms.
  26. **Expressive types.** Mypyc fully supports standard Python type hints.
  27. Mypyc has local type inference, generics, optional types, tuple types,
  28. union types, and more. Type hints act as machine-checked
  29. documentation, making code not only faster but also easier to
  30. understand and modify.
  31. **Python ecosystem.** Mypyc runs on top of CPython, the
  32. standard Python implementation. You can use any third-party libraries,
  33. including C extensions, installed with pip. Mypyc uses only valid Python
  34. syntax, so all Python editors and IDEs work perfectly.
  35. **Fast program startup.** Mypyc uses ahead-of-time compilation, so
  36. compilation does not slow down program startup. Slow program startup
  37. is a common issue with JIT compilers.
  38. **Migration path for existing code.** Existing Python code often
  39. requires only minor changes to compile using mypyc.
  40. **Waiting for compilation is optional.** Compiled code also runs as
  41. normal Python code. You can use interpreted Python during development,
  42. with familiar and fast workflows.
  43. **Runtime type safety.** Mypyc protects you from segfaults and memory
  44. corruption. Any unexpected runtime type safety violation is a bug in
  45. mypyc. Runtime values are checked against type annotations. (Without
  46. mypyc, type annotations are ignored at runtime.)
  47. **Find errors statically.** Mypyc uses mypy for static type checking
  48. that helps catch many bugs.
  49. Use cases
  50. ---------
  51. **Fix only performance bottlenecks.** Often most time is spent in a few
  52. Python modules or functions. Add type annotations and compile these
  53. modules for easy performance gains.
  54. **Compile it all.** During development you can use interpreted mode,
  55. for a quick edit-run cycle. In releases all non-test code is compiled.
  56. This is how mypy achieved a 4x performance improvement over interpreted
  57. Python.
  58. **Take advantage of existing type hints.** If you already use type
  59. annotations in your code, adopting mypyc will be easier. You've already
  60. done most of the work needed to use mypyc.
  61. **Alternative to a lower-level language.** Instead of writing
  62. performance-critical code in C, C++, Cython or Rust, you may get good
  63. performance while staying in the comfort of Python.
  64. **Migrate C extensions.** Maintaining C extensions is not always fun
  65. for a Python developer. With mypyc you may get performance similar to
  66. the original C, with the convenience of Python.
  67. Differences from Cython
  68. -----------------------
  69. Mypyc targets many similar use cases as Cython. Mypyc does many things
  70. differently, however:
  71. * No need to use non-standard syntax, such as ``cpdef``, or extra
  72. decorators to get good performance. Clean, normal-looking
  73. type-annotated Python code can be fast without language extensions.
  74. This makes it practical to compile entire codebases without a
  75. developer productivity hit.
  76. * Mypyc has first-class support for features in the ``typing`` module,
  77. such as tuple types, union types and generics.
  78. * Mypyc has powerful type inference, provided by mypy. Variable type
  79. annotations are not needed for optimal performance.
  80. * Mypyc fully integrates with mypy for robust and seamless static type
  81. checking.
  82. * Mypyc performs strict enforcement of type annotations at runtime,
  83. resulting in better runtime type safety and easier debugging.
  84. Unlike Cython, mypyc doesn't directly support interfacing with C libraries
  85. or speeding up numeric code.
  86. How does it work
  87. ----------------
  88. Mypyc uses several techniques to produce fast code:
  89. * Mypyc uses *ahead-of-time compilation* to native code. This removes
  90. CPython interpreter overhead.
  91. * Mypyc enforces type annotations (and type comments) at runtime,
  92. raising ``TypeError`` if runtime values don't match annotations.
  93. Value types only need to be checked in the boundaries between
  94. dynamic and static typing.
  95. * Compiled code uses optimized, type-specific primitives.
  96. * Mypyc uses *early binding* to resolve called functions and name
  97. references at compile time. Mypyc avoids many dynamic namespace
  98. lookups.
  99. * Classes are compiled to *C extension classes*. They use `vtables
  100. <https://en.wikipedia.org/wiki/Virtual_method_table>`_ for fast
  101. method calls and attribute access.
  102. * Mypyc treats compiled functions, classes, and attributes declared
  103. ``Final`` as immutable.
  104. * Mypyc has memory-efficient, unboxed representations for integers and
  105. booleans.
  106. Development status
  107. ------------------
  108. Mypyc is currently alpha software. It's only recommended for
  109. production use cases with careful testing, and if you are willing to
  110. contribute fixes or to work around issues you will encounter.