A public relations problem
A public relations problem
Posted Jul 13, 2015 15:41 UTC (Mon) by Kwi (subscriber, #59584)In reply to: A public relations problem by arvidma
Parent article: A better story for multi-core Python
I learnt about the GIL, when I was writing a piece of code that needed (frequently) to traverse [preferably in parallel] a (big) tree structure and perform some calculations on each node.
I agree that Python is not the best tool for that job. The answer here would be C or Cython.
Even without the GIL, you'd probably have lock contention on the reference counters for any Python function called while processing your tree.
All CPython objects, including functions, are reference counted; while executing a function, the reference count is increased.
>>> import sys >>> def foo(): ... print(sys.getrefcount(foo)) ... >>> print(sys.getrefcount(foo)) 2 >>> foo() 4
Reference counting is another reason why multithreaded CPython is bad for performance critical stuff. Note that, unlike removing the GIL, removing reference counting would change the semantics of the language. That's why e.g. PyPy (which uses garbage collection) is not the "standard" interpreter.
(Now, PyPy still has the GIL – except in the experimental STM branch – but my experience indicates that PyPy using a single thread is likely faster that a hypotehical GIL-free CPython using four cores.)
Python compromises performance in numerous places, by design, whether it's by allowing crazy monkey patching of modules at runtime or by rejecting tail call optimizations.
Did you know that from module import func and calling func gives better performance than import module and calling module.func (in a tight loop)? It's obvious when you know Python, but it can be surprising to newcomers.
In the end, Python values other features higher than performance; and again, that's largely a design decision.
