Skip to content

Commit 474fd5c

Browse files
authored
[mypyc] Updates to introduction in the documentation (#10179)
Improve clarity and add a comparison against Cython.
1 parent de7514d commit 474fd5c

File tree

1 file changed

+57
-52
lines changed

1 file changed

+57
-52
lines changed

mypyc/doc/introduction.rst

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Mypyc compiles Python modules to C extensions. It uses standard Python
66
<https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html>`_ to
77
generate fast code.
88

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,
1111
but it's mostly compatible with standard Python.
1212

1313
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
1616
supported.
1717

1818
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.
2022

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.
3325

3426
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).
3628

37-
Motivation
29+
Why mypyc?
3830
----------
3931

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.
5934

6035
**Expressive types.** Mypyc fully supports standard Python type hints.
6136
Mypyc has local type inference, generics, optional types, tuple types,
6237
union types, and more. Type hints act as machine-checked
6338
documentation, making code not only faster but also easier to
6439
understand and modify.
6540

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+
6646
**Fast program startup.** Mypyc uses ahead-of-time compilation, so
6747
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.
7449

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.
7752

7853
**Waiting for compilation is optional.** Compiled code also runs as
7954
normal Python code. You can use interpreted Python during development,
8055
with familiar and fast workflows.
8156

8257
**Runtime type safety.** Mypyc protects you from segfaults and memory
8358
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.)
8561

8662
**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.
8964

9065
Use cases
9166
---------
9267

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
9469
Python modules or functions. Add type annotations and compile these
9570
modules for easy performance gains.
9671

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.
10076

10177
**Take advantage of existing type hints.** If you already use type
10278
annotations in your code, adopting mypyc will be easier. You've already
@@ -110,6 +86,33 @@ performance while staying in the comfort of Python.
11086
for a Python developer. With mypyc you may get performance similar to
11187
the original C, with the convenience of Python.
11288

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+
113116
How does it work
114117
----------------
115118

@@ -120,6 +123,8 @@ Mypyc uses several techniques to produce fast code:
120123

121124
* Mypyc enforces type annotations (and type comments) at runtime,
122125
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.
123128

124129
* Compiled code uses optimized, type-specific primitives.
125130

@@ -140,6 +145,6 @@ Mypyc uses several techniques to produce fast code:
140145
Development status
141146
------------------
142147

143-
Mypyc is currently *alpha software*. It's only recommended for
148+
Mypyc is currently alpha software. It's only recommended for
144149
production use cases with careful testing, and if you are willing to
145150
contribute fixes or to work around issues you will encounter.

0 commit comments

Comments
 (0)