python / python/cpython

Dramatic slowdown with random.random on free-threading build.

未關閉
#118,392 5 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

performance topic-free-threading
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

out

This slowdown was found with one of my favorite benchmarks, which is calculating the pi value with the Monte Carlo method.

import os
import random
import time
from threading import Thread

def monte_carlo_pi_part(n: int, idx: int, results: list[int]) -> None:
    count = 0
    for i in range(n):
        x = random.random()
        y = random.random()

        if x*x + y*y <= 1:
            count += 1
    results[idx] = count


n = 10000
threads = []
num_threads = 100
results = [0] * num_threads
a = time.time()
for i in range(num_threads):
    t = Thread(target=monte_carlo_pi_part, args=(n, i, results))
    t.start()
    threads.append(t)

while threads:
    t = threads.pop()
    t.join()

b = time.time()
print(sum(results) / (n * num_threads) * 4)
print(b-a)

Acquiring critical sections for random methods causes this slowdown.
Removing @critical_section from the method, which uses genrand_uint32 and then updating genrand_uint32 to use atomic operation makes the performance acceptable.

Build Elapsed PI
Default (with specialization) 0.16528010368347168 3.144508
Free-threading (with no specialization) 0.548654317855835 3.1421
Free-threading with my patch (with no specialization) 0.2606849670410156 3.141108
Linked PRs
  • gh-118393
  • gh-118396

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先閱讀 random.random 的實作與 genrand_uint32 路徑,包括 issue 中所述的 @critical_section 使用方式。接著重現提供的多執行緒 Monte Carlo 基準測試,再檢視連結的 PR 118393 與 118396;當 free-threading 造成的效能下降獲得改善,且不改變亂數的正確性時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
performance
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。