`TreeInterpreter` creates reference cycle, causing GC pressure
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- python
- 領域
- performance
調査の方向性
parser.py の ParsedResult.search() から始めて TreeInterpreter の生成を調べ、次に visitor.py の Visitor._method_cache 周辺を読んで、束縛メソッドによる参照サイクルを確認します。提案されているインタープリターのキャッシュ方式を比較し、提供された 100 万回反復の再現プログラムを実行します。繰り返し発生する TreeInterpreter のサイクルと、それに伴う GC への負荷を回避できれば完了です。
索引モデルが issue の本文から書いたものです。
説明
We recently noticed that a heavy JMESpath workload was triggering a large number of garbage collection runs. We are using jmespath.compile(), and we tracked this down to the jmespath.visitor.TreeInterpreter that is created on every call to `ParsedResult.search(): https://github.com/jmespath/jmespath.py/blob/bbe7300c60056f52413603cf3e2bcd0b6afeda3d/jmespath/parser.py#L508
It appears that TreeInterpreter creates a reference cycle, which leads to the GC being triggered frequently to clean up the cycles. As far as I can tell, the problem comes from the Visitor._method_cache: https://github.com/jmespath/jmespath.py/blob/bbe7300c60056f52413603cf3e2bcd0b6afeda3d/jmespath/visitor.py#L91-L93
...which store references to methods that are bound to self in a member of self.
Possible solution
We worked around the problem by monkey patching ParsedResult so that it (1) caches a default_interpreter for use when options=None, and (2) uses it in search(). If I understand correctly, we could go further and use a global TreeInterpreter for all ParsedResult instances. The TreeInterpreter seems to be stateless apart from self._method_cache and that implementation seems to be thread-safe (with only the risk of multiple lookups for the same method in a multithreaded case).
I'd be happy to contribute a PR for either version if this would be welcome.
How to reproduce
The following reproducer shows the problem:
import jmespath
import gc
gc.set_debug(gc.DEBUG_COLLECTABLE)
pattern = jmespath.compile("foo")
value = {"foo": "bar"}
for _ in range(1000000):
pattern.search(value)
...where the output contains one million repetitions of something like:
gc: collectable <TreeInterpreter 0x10f634fa0>
gc: collectable <dict 0x10f63e780>
gc: collectable <Options 0x10f634520>
gc: collectable <Functions 0x10f6345b0>
gc: collectable <method 0x10f63ee80>
gc: collectable <dict 0x10f63eb00>
- 主要言語
- Python
- スター
- 2.5k
- フォーク
- 231
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
jmespath/jmespath.py のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
jmespath/jmespath.py#401 ·
-
floor() and ceil() raise OverflowError / ValueError on Infinity and NaN対応中かも @guocn が 49 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
jmespath/jmespath.py#381 ·
-
Lexer gives quoted identifiers an `end` that is the token length, not its end offset対応中かも @chiliec が 50 日前に担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 20/100
jmespath/jmespath.py#380 ·
-
sdist is missing `tests/__init__.py`対応中かも @deepakganesh78 が 127 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 38/100
jmespath/jmespath.py#341 · コメント 1 件 ·
-
Different behavior on merge function between the JS lib and the Python lib再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 42/100
jmespath/jmespath.py#329 · コメント 1 件 ·
jmespath/jmespath.py の issue をすべて見る
似ている issue
-
bug TAG: unit tests
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
learningequality/ricecooker#898 ·
-
task
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
freelawproject/litigant-portal#1285 ·
メンテナーはふだん 2 日以内に返信
-
least_squares returns a nonfinite objective for an invalid initial residual対応中かも @sylvesterkaczmarek が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
google-deepmind/mujoco#4023 ·
メンテナーはふだん 11 日以内に返信
-
Five Australian birds: Red-capped Parrot, Red-winged Fairywren, Gilbert’s Honeyeater, Western Spinebill, Western Wattlebird対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
arnegiacomo/fugleramme#427 ·
メンテナーはふだん 1 日以内に返信
-
MONDO's zebu and cat Arnold-Chiari terms are is_a a dog disease (monarch-initiative/mondo#10939)オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
NCATSTranslator/Babel#1317 ·
メンテナーはふだん 2 日以内に返信