Unlocking Python's Cores: Hardware Usage and Energy Implications of Removing the GIL

AI Systems and Hardware OLPP ADCEEM
2026年03月05日
Python 的全局解释器锁(GIL)会阻止多线程程序同时在多个 CPU 核心上执行,即使启用了多个线程亦然。然而,自 Python 3.13 起,已提供一种实验性构建版本,支持禁用 GIL。尽管此前已有研究探讨了禁用 GIL 对运行速度的提升效果,但其对能耗及硬件资源利用率的影响却较少受到关注。本研究采用四类典型工作负载——基于 NumPy 的计算任务、顺序执行内核、多线程数值计算任务以及多线程面向对象任务——在 Python 3.14.2 的带 GIL 版本与无 GIL(free-threaded)版本之间,系统测量并对比了执行时间、CPU 利用率、内存占用量以及能耗。 研究结果揭示了一种明显的权衡关系:对于可并行化且数据彼此独立的工作负载,无 GIL 构建版本最多可将执行时间缩短至原来的 1/4,能耗也相应降低,同时实现有效的多核并行利用;但代价是内存使用量有所上升。相比之下,顺序型工作负载无法从移除 GIL 中获益,反而能耗增加 13%–43%。类似地,在多线程频繁访问并修改同一对象的工作负载中,由于锁竞争加剧,性能提升幅度显著减弱,甚至出现性能退化。在所有测试工作负载中,能耗均与执行时间呈严格正比关系,表明禁用 GIL 并未显著改变单位时间的功耗(即瞬时功率),即便此时 CPU 利用率明显提高。就内存表现而言,无 GIL 构建版本整体内存用量有所上升,这一增长在虚拟内存中尤为明显,而在物理内存中则相对不显著;其主要原因在于:每个对象需额外加锁、运行时引入了更多线程安全机制,以及采用了全新的内存分配器。 上述发现表明,Python 的无 GIL 构建版本并非一种普适性的性能改进方案。开发者在实际采用前,应审慎评估自身工作负载是否具备高效并行执行的潜力与条件。
Python's Global Interpreter Lock prevents execution on more than one CPU core at the same time, even when multiple threads are used. However, starting with Python 3.13 an experimental build allows disabling the GIL. While prior work has examined speedup implications of this disabling, the effects on energy consumption and hardware utilization have received less attention. This study measures execution time, CPU utilization, memory usage, and energy consumption using four workload categories: NumPy-based, sequential kernels, threaded numerical workloads, and threaded object workloads, comparing GIL and free-threaded builds of Python 3.14.2. The results highlight a trade-off. For parallelizable workloads operating on independent data, the free-threaded build reduces execution time by up to 4 times, with a proportional reduction in energy consumption, and effective multi-core utilization, at the cost of an increase in memory usage. In contrast, sequential workloads do not benefit from removing the GIL and instead show a 13-43% increase in energy consumption. Similarly, workloads where threads frequently access and modify the same objects show reduced improvements or even degradation due to lock contention. Across all workloads, energy consumption is proportional to execution time, indicating that disabling the GIL does not significantly affect power consumption, even when CPU utilization increases. When it comes to memory, the no-GIL build shows a general increase, more visible in virtual memory than in physical memory. This increase is primarily attributed to per-object locking, additional thread-safety mechanisms in the runtime, and the adoption of a new memory allocator. These findings suggest that Python's no-GIL build is not a universal improvement. Developers should evaluate whether their workload can effectively benefit from parallel execution before adoption.
许愿