`TreeInterpreter` creates reference cycle, causing GC pressure
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- python
- Lĩnh vực
- performance
Hướng nghiên cứu
Bắt đầu trong parser.py tại ParsedResult.search() và kiểm tra việc tạo TreeInterpreter, sau đó đọc visitor.py quanh Visitor._method_cache để xác nhận chu kỳ tham chiếu của bound method. So sánh các cách tiếp cận được đề xuất để lưu đệm interpreter và chạy trình tái hiện được cung cấp với một triệu lần lặp; hoàn tất nghĩa là tránh được các chu kỳ TreeInterpreter lặp lại và áp lực GC liên quan.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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>
- Ngôn ngữ chính
- Python
- Star
- 2.5k
- Fork
- 231
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của jmespath/jmespath.py
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
jmespath/jmespath.py#401 ·
-
floor() and ceil() raise OverflowError / ValueError on Infinity and NaNCó thể đã có người làm @guocn đã nhận 49 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
jmespath/jmespath.py#381 ·
-
Lexer gives quoted identifiers an `end` that is the token length, not its end offsetCó thể đã có người làm @chiliec đã nhận 50 ngày trước. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 20/100
jmespath/jmespath.py#380 ·
-
sdist is missing `tests/__init__.py`Có thể đã có người làm @deepakganesh78 đã nhận 127 ngày trước. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 38/100
jmespath/jmespath.py#341 · 1 bình luận ·
-
Different behavior on merge function between the JS lib and the Python libCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 42/100
jmespath/jmespath.py#329 · 1 bình luận ·
Tất cả issue của jmespath/jmespath.py
Issue tương tự
-
test_real_pandoc_failure_is_one_path_free_line fails on macOS CI with current Homebrew pandocĐang mởbug TAG: unit tests
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
learningequality/ricecooker#898 ·
-
task
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
freelawproject/litigant-portal#1285 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
least_squares returns a nonfinite objective for an invalid initial residualCó thể đã có người làm @sylvesterkaczmarek đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
google-deepmind/mujoco#4023 ·
Maintainer thường phản hồi trong vòng 11 ngày
-
Five Australian birds: Red-capped Parrot, Red-winged Fairywren, Gilbert’s Honeyeater, Western Spinebill, Western WattlebirdCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
arnegiacomo/fugleramme#427 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
MONDO's zebu and cat Arnold-Chiari terms are is_a a dog disease (monarch-initiative/mondo#10939)Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
NCATSTranslator/Babel#1317 ·
Maintainer thường phản hồi trong vòng 2 ngày