Lock contention inside `_PyType_LookupRef`
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Feature or enhancement
Proposal:
As part of adding free-threading support to LibCST, we noticed there is a lot of lock contention on TYPE_LOCK inside the _PyType_LookupRef function. In the LibCST, the common "visitor" pattern is used. For example, in the _visitors.py module there is the code:
visit_func = getattr(self, f"visit_{type(node).__name__}", None)
The second argument to getattr() is a non-interned string and it causes the cached and lock-free path of _PyType_LookupRef() never to be taken. Instead, the TYPE_LOCK mutex is acquired on each lookup. This obviously scales very badly if there are multiple threads looking up class methods using this pattern.
Testing was done with Python 3.13 but I believe the same issue exists with 3.14.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
- gh-132381
- gh-131174
- gh-132651
- gh-132652
- gh-133669
- gh-135112
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先閱讀 CPython 中的 _PyType_LookupRef 和 TYPE_LOCK 路徑,接著檢查 LibCST 的 _visitors.py 中的 visitor 模式。比較 Python 3.13 和 3.14 上的行為,並檢視連結的 PR;完成時應包含一項經協議的變更,以處理鎖競爭問題,以及查找路徑擴展性更好的證據。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- c, python
- 領域
- backend, performance
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 20/100