Debian Codegraph @

mypyc/doc/introduction.rst

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