@@ -6,8 +6,8 @@ Mypyc compiles Python modules to C extensions. It uses standard Python
6
6
<https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html> `_ to
7
7
generate fast code.
8
8
9
- The compiled language is a strict, statically typed Python variant.
10
- It restricts the use of some dynamic Python features to gain performance,
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
11
but it's mostly compatible with standard Python.
12
12
13
13
Mypyc uses `mypy <http://www.mypy-lang.org/ >`_ to perform type
@@ -16,87 +16,63 @@ checking and type inference. Most type system features in the stdlib
16
16
supported.
17
17
18
18
Compiled modules can import arbitrary Python modules and third-party
19
- libraries.
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.
20
22
21
- You can run the modules you compile also as normal, interpreted Python
22
- modules.
23
-
24
- You can roughly expect speedups like these (2x improvement means half the
25
- runtime):
26
-
27
- * Existing code with type annotations often gets **1.5x to 5x ** faster.
28
-
29
- * Code tuned for mypyc can be **5x to 10x ** faster.
30
-
31
- There is no simple answer to how fast your code will be when compiled.
32
- You should try it out!
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.
33
25
34
26
Mypyc currently aims to speed up non-numeric code, such as server
35
- applications. We also use mypyc to compile mypyc, of course!
27
+ applications. Mypyc is also used to compile itself (and mypy).
36
28
37
- Motivation
29
+ Why mypyc?
38
30
----------
39
31
40
- Though Python has been successful without a good performance story
41
- for non-numeric code, speed still matters:
42
-
43
- 1. Users prefer more efficient and responsive software and libraries.
44
-
45
- 2. You need less hardware to run your server application and save money.
46
-
47
- 3. You'll waste less time waiting for your tests and jobs to finish.
48
-
49
- 4. Faster code correlates with less energy use and is better for the
50
- environment.
51
-
52
- Perks
53
- -----
54
-
55
- **Easy to get started. ** Compiled code looks and behaves like normal
56
- Python code. You get the benefits of static typing while using the
57
- syntax, libraries and idioms you (and millions of developers) already
58
- know.
32
+ **Easy to get started. ** Compiled code has the look and feel of
33
+ regular Python code. Mypyc supports familiar Python syntax and idioms.
59
34
60
35
**Expressive types. ** Mypyc fully supports standard Python type hints.
61
36
Mypyc has local type inference, generics, optional types, tuple types,
62
37
union types, and more. Type hints act as machine-checked
63
38
documentation, making code not only faster but also easier to
64
39
understand and modify.
65
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
+
66
46
**Fast program startup. ** Mypyc uses ahead-of-time compilation, so
67
47
compilation does not slow down program startup. Slow program startup
68
- is a common problem with JIT compilers.
69
-
70
- **Python ecosystem supported. ** Mypyc runs on top of CPython, the
71
- standard Python implementation. Code can freely use standard library
72
- features. Use pip to install any third-party libraries you need,
73
- including C extensions.
48
+ is a common issue with JIT compilers.
74
49
75
- **Migration path for existing Python code. ** Existing Python code
76
- often requires only minor changes to compile using mypyc.
50
+ **Migration path for existing code. ** Existing Python code often
51
+ requires only minor changes to compile using mypyc.
77
52
78
53
**Waiting for compilation is optional. ** Compiled code also runs as
79
54
normal Python code. You can use interpreted Python during development,
80
55
with familiar and fast workflows.
81
56
82
57
**Runtime type safety. ** Mypyc protects you from segfaults and memory
83
58
corruption. Any unexpected runtime type safety violation is a bug in
84
- mypyc.
59
+ mypyc. Runtime values are checked against type annotations. (Without
60
+ mypyc, type annotations are ignored at runtime.)
85
61
86
62
**Find errors statically. ** Mypyc uses mypy for static type checking
87
- that will catch many bugs. This saves time you'd otherwise spend
88
- debugging.
63
+ that helps catch many bugs.
89
64
90
65
Use cases
91
66
---------
92
67
93
- **Fix performance bottlenecks. ** Often most time is spent in a few
68
+ **Fix only performance bottlenecks. ** Often most time is spent in a few
94
69
Python modules or functions. Add type annotations and compile these
95
70
modules for easy performance gains.
96
71
97
- **Compile it all. ** During development you use interpreted mode, for a
98
- quick edit-run cycle. In releases all non-test code is compiled. This
99
- is how mypy got a *4x performance improvement * over interpreted Python.
72
+ **Compile it all. ** During development you can use interpreted mode,
73
+ for a quick edit-run cycle. In releases all non-test code is compiled.
74
+ This is how mypy achieved a 4x performance improvement over interpreted
75
+ Python.
100
76
101
77
**Take advantage of existing type hints. ** If you already use type
102
78
annotations in your code, adopting mypyc will be easier. You've already
@@ -110,6 +86,33 @@ performance while staying in the comfort of Python.
110
86
for a Python developer. With mypyc you may get performance similar to
111
87
the original C, with the convenience of Python.
112
88
89
+ Differences from Cython
90
+ -----------------------
91
+
92
+ Mypyc targets many similar use cases as Cython. Mypyc does many things
93
+ differently, however:
94
+
95
+ * No need to use non-standard syntax, such as ``cpdef ``, or extra
96
+ decorators to get good performance. Clean, normal-looking
97
+ type-annotated Python code can be fast without language extensions.
98
+ This makes it practical to compile entire codebases without a
99
+ developer productivity hit.
100
+
101
+ * Mypyc has first-class support for features in the ``typing `` module,
102
+ such as tuple types, union types and generics.
103
+
104
+ * Mypyc has powerful type inference, provided by mypy. Variable type
105
+ annotations are not needed for optimal performance.
106
+
107
+ * Mypyc fully integrates with mypy for robust and seamless static type
108
+ checking.
109
+
110
+ * Mypyc performs strict enforcement of type annotations at runtime,
111
+ resulting in better runtime type safety and easier debugging.
112
+
113
+ Unlike Cython, mypyc doesn't directly support interfacing with C libraries
114
+ or speeding up numeric code.
115
+
113
116
How does it work
114
117
----------------
115
118
@@ -120,6 +123,8 @@ Mypyc uses several techniques to produce fast code:
120
123
121
124
* Mypyc enforces type annotations (and type comments) at runtime,
122
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.
123
128
124
129
* Compiled code uses optimized, type-specific primitives.
125
130
@@ -140,6 +145,6 @@ Mypyc uses several techniques to produce fast code:
140
145
Development status
141
146
------------------
142
147
143
- Mypyc is currently * alpha software * . It's only recommended for
148
+ Mypyc is currently alpha software. It's only recommended for
144
149
production use cases with careful testing, and if you are willing to
145
150
contribute fixes or to work around issues you will encounter.
0 commit comments