解锁Python的核心:移除GIL的硬件使用和能耗影响
Unlocking Python's Cores: Hardware Usage and Energy Implications of Removing the GIL
AI总结:
研究发现移除Python GIL在并行工作负载中可显著提升性能和能效,但对顺序工作负载和高锁竞争场景有负面影响,开发者需根据具体需求评估采用。
摘要:
Python的全局解释器锁阻止了同时在多个CPU核心上执行,即使使用多个线程。然而,从Python 3.13开始,一个实验性版本允许禁用GIL。尽管之前的研究所探讨了禁用GIL带来的加速影响,但其对能耗和硬件使用的影响却较少被关注。本研究通过四个工作负载类别测量执行时间、CPU利用率、内存使用和能耗:基于NumPy的、顺序内核、线程数值工作负载和线程对象工作负载,比较Python 3.14.2的GIL禁用和自由线程版本。结果突显了权衡。对于在独立数据上并行化的工作负载,自由线程版本将执行时间减少高达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.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。