[Python-ideas] Type Hinting - Performance booster ?

Stefan Behnel stefan_ml at behnel.de
Wed Dec 24 12:45:11 CET 2014

Ludovic Gasc schrieb am 21.12.2014 um 00:55:
> With the future official support of Type Hinting in Python, is it means
> that CPython could use this pieces of information to store variables with
> more efficient data structures, not only check types ?
> It could possible to have better performance one day with Type Hinting like
> you have with Cython (explicit types declaration) or PyPy (guessing types) ?
> Is it realistic to except that one day, or I've missed some mountains ;-) ?
> If this is correct, better performances will be a great consequence for
> Type Hinting

Regarding Cython, it's actually unlikely that it will bring anything. The
proposed feature is about specifying Python object types for function
arguments and (to a certain extent) local variables, and Cython is already
pretty good in guessing those or doing optimistic optimisations that just
say "if it looks like you're using a dict, let's generate special code that
speeds up dict access and leaves other stuff to a fallback code path".

The type semantics are even different for builtin types. When you type a
variable as "dict" in Cython, it will reject subtypes as there's no use in
typing a variable as "dict" if it can't be optimised as a dict due to
potential subtype overrides. The normal optimistic "fast for dicts, works
for all objects" code that Cython generates here is exactly as fast as code
that would allow only dict or subtypes.

Typing a variable as arbitrary size Python "int" is also useless as there
is no advantage Cython can draw from it, but even typing a variable as
Python "float", which is identical to a C double, isn't helpful due to the
semantic difference for subtypes. Cython could be slightly improved to also
generate special casing low level code paths for Python integer and
floating point operations, but then, why waste time doing that when simply
typing a variable as plain C int or double gives you full native speed
without any of the hassle of runtime type checks, bloated special casing
code paths, etc.?

And typing variables with user implemented Python classes is equally
useless as there are no special optimisations that Cython could apply to them.

So, from the POV of Cython, I do see the advantage of a syntax that
integrates with Python's own syntax (Cython's "pure Python mode" [1] has
provided similar typing support for years now), but please don't expect any
performance advantage from this.


[1] http://docs.cython.org/src/tutorial/pure.html

More information about the Python-ideas mailing list