Re: Annotating pure functions to improve inline caching/optimization

Philipp Burch <[email protected]> Mon, 19 Sep 2022 17:52:37 +0200
Newsgroups gmane.comp.python.devel
Message-ID <[email protected]>
Hi Ken,

thank you for the inputs. Just one more comment:

>> I actually find myself often factoring such data out of loops in Python,  whereas in C I would just leave that to the optimizer/compiler.
> 
> The compiler in CPython can't really do that because it's not safe in Python. The user could've overridden `__add__` to do more things than just addition.

I'm totally aware that Python, or dynamic languages in general, are 
terrible for any optimizer, because there is basically nothing that it 
can be sure about. However, this is exactly the point of marking 
functions as "pure". It would not make the code technically safe to 
optimize, but transfer the responsibility from the interpreter to the 
programmer. If the programmer says that something is safe to 
optimize/cache, then the interpreter can't be blamed if something breaks 
when it actually does so.

On the other hand, a language as dynamic as Python probably is just not 
designed for an optimizer, so either you do it by hand or use an 
extension module  to speed critical parts up. Fair enough.

Best regards,
Philipp