`_abc._abc_subclasscheck` has very poor performance and (I think) a memory leak
オープン
まだ誰も着手していません。
extension-modules
performance
type-bug
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 36k
- 平均マージ
- 1日 9時間
- マージ済み PR(30日)
- 558
説明
Bug report
I'm been hunting a memory leak in pydantic and I keep seeing _abc_subclasscheck popping up in the memray flamegraph.
Locally I've seen memray report that _abc_subclasscheck is using 2.8GB of memory in some cases!
I can't get anything that bad in a minimal example, but I have the following:
from abc import ABCMeta
from datetime import datetime
class MyMetaclass(ABCMeta):
pass
class MyClass(metaclass=MyMetaclass):
pass
def main():
class Foobar(MyClass):
pass
assert issubclass(Foobar, MyClass)
assert not issubclass(int, MyClass)
assert not issubclass(str, MyClass)
assert not issubclass(datetime, MyClass)
t = type('A', (), {})
assert not issubclass(t, MyClass)
if __name__ == '__main__':
import os, psutil
process = psutil.Process(os.getpid())
mb = 1024 * 1024
last = 0
for i in range(5_000):
main()
# mem = process.memory_info().rss
# print(f'{i + 1:>4d} {mem / mb:8.2f}MB {(mem - last) / mb:+8.2f}MB | {"━" * int(mem / 8_000_000)}')
# last = mem
A few things to note:
- the commented out last few lines print current memory and change in memory over time
- I'm not sure exactly which
issubclasscalls are necessary, certainly I don't see a rise in memory with just the last one - some memory drops (presumably related to clearing the abc caches?) is happening, but overall memory is increasing
- The performance of
issubclasson an abc is very poor indeed - this script takes 14seconds, if i switchABCMetatotypeit takes 121ms!
Your environment
- Python 3.10.0 installed with venv
- Ubuntu 21.10
Linked PRs
- gh-131914
- gh-141171
- gh-144941
- gh-150540
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供された Python 再現プログラムを実行して ABCMeta と type を比較し、その後、リンクされている PR gh-131914、gh-141171、gh-144941、gh-150540 を確認します。再現プログラムから得られた証拠に基づいて、報告された issubclass の性能とメモリ増加の挙動を特定して解決できれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- backend
- issue の種類
- バグ
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 20/100