Why Fine-Grained Locking Was Avoided
Instead of a single GIL, the alternative would have been fine-grained locking on every object, which would have introduced significant performance overhead for single-threaded applications, increased interpreter complexity, and made C extension integration much harder.
The top 3
- Single-Threaded Performance: 2x Slower Benchmark: Early experiments showed that fine-grained locking would make typical single-threaded Python code, which was the majority at the time, run at least twice as slow due to the constant overhead of acquiring and releasing numerous locks for every object operation.
- Complexity and Deadlock Risk: CPython's Challenge: Implementing fine-grained locks across all of CPython's internal data structures to protect reference counts and other interpreter state was deemed too complex, significantly increasing development difficulty and the risk of introducing hard-to-debug deadlocks.
- C Extension Ecosystem: Simplified Thread Safety: The GIL simplified C extension development by allowing authors to assume exclusive access to Python objects, eliminating the need for complex thread-safety analysis within each extension and fostering the growth of the C-based ecosystem. Rewriting existing C extensions like NumPy for thread safety without the GIL is considered a 'generational project'.
Sources
Open the full topic