[Python-ideas] Add specialized bytecode with guards to functions

Antoine Pitrou antoine at python.org
Wed Oct 21 23:03:00 CEST 2015


Brett Cannon <brett at ...> writes:
> 
> Exactly, and especially if the check is cheap. After that you start to
potentially get into granularity, e.g. do you care if any built-in changed
or do you only care if a specific built-in changed, and how do either
approach affect performance? The trick is not breaking compatibility and not
hurting performance in the general case. But yes, knowing when the uncommon
case of overriding a built-in occurs would allow for a bunch of optimization
techniques from inlining to using a faster implementation that is compatible
with the standard built-in.

I think adding a version number to dicts would be a very cheap change
and would potentially help several CPython-based projects (including
perhaps Numba some day, though nowadays it wouldn't benefit).

However, I would also like to challenge the idea that an accelerator
has to be 100% compatible. If your API is opt-in (which Numba's is),
then you can happily relax some constraints (provided it is documented)
in exchange for much faster code. Our experience with Numba is that
nobody cares if you can't monkeypatch the len() builtin, for example.

(Numba actually goes quite far in that direction since e.g. its
integers are fixed-width; the speed of generated code, for supported
Python constructs, usually approaches or reaches C, Fortran or even
hand-tuned assembly speed)

Regards

Antoine.


More information about the Python-ideas mailing list