Beware of Python optimization
One of my hosters announced recently that it is enabling Python “optimization” from now on, which will result in “.pyo” instead of “.pyc”. I couldn’t let it go: I ranted in twitter :-) That made them roll it back.
This incident proves what noone reads documentation, even good one! So I’ll help you with that. In short,
Never enable “Python optimization”, unless you understand what it does and it’s related to what you’re trying to archieve
Such warning is written in mod_wsgi docs, but this sentence is probably too long to be read:
Overall, if you do not understand what the normal ‘python’ executable ‘-O’ option does, how the Python runtime changes it behaviour as a result, and you don’t know exactly how your application would be affected by enabling this option, then do not use this option.
So, how Python runtime’s behaviour changes as a result of an optimization? Let’s take a look at Python’s very own docs:
A program doesn’t run any faster when it is read from a .pyc or .pyo file than when it is read from a .py file
It does not change (actually, it says that “it does not run faster”, but, well, I’m afraid it doesn’t change anything interesting at all).
Okay, so the runtime benefit of this so-called optimization is none. There’s some benefit in the speed of module importing, but such thing happens only once during the lifetime of the process. If you’re a web developer, such lifetime is too long to care about optimization of some O(1) event.
But why is it harmful? Because debugging and sanity checks are going to be a pain in the ass: the only thing “optimization” does is removing your asserts (the higher level optimization also removes __doc__-strings, and it is the only thing it does, too).
What? You’re not using asserts? Make sure nothing from your requirements.txt does.
And even then, think twice before switching on useless optimizations.
Please send your comments, ideas, rants and job offers at v.golev@gmail.com.
Made with Nginx, Jekyll, Git, EC2 and Emacs.