argparse.ArgumentParser is slow when parsing a very long option list
@serhiy-storchaka đang làm issue này rồi.
Từ ngày 2/10/2024.
- #124909 của @serhiy-storchaka — đang mở
Đá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
- 25/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
- cli, performance
Hướng nghiên cứu
Bắt đầu với argparse.ArgumentParser và parse_args(), sử dụng cách tái hiện lặp lại --item=x trong issue để quan sát khả năng mở rộng. Kiểm tra các PR được liên kết để tìm những công việc đã xử lý hành vi này. Được xem là hoàn thành khi việc phân tích một danh sách tùy chọn rất dài không còn thể hiện mức tăng trưởng O(N^2) đã được báo cáo.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug report
The following parser exhibits O(N^2) behavior when parsing a very long option list (e.g. --item=a --item=b ...):
parser = argparse.ArgumentParser(description="test")
parser.add_argument('--item', dest='accumulate', action='append')
Here is a simple repro that measures the time parse_args() take depending on the number of options:
#!/usr/bin/env python3
import argparse
from datetime import datetime
def run_test(num_iterations):
"""Parse a list with 'append' action and measure the time it takes."""
parser = argparse.ArgumentParser(description="test")
parser.add_argument('--item', dest='accumulate', action='append')
start = datetime.now()
args = parser.parse_args(['--item=x' for _ in range(num_iterations)])
end = datetime.now()
diff = end - start
print(f"{num_iterations:8} iterations: {diff} s")
def main():
for i in range(8, 18):
run_test(pow(2, i))
if __name__ == "__main__":
main()
The output on my machine:
> ./repro.py
256 iterations: 0:00:00.002699 s
512 iterations: 0:00:00.008444 s
1024 iterations: 0:00:00.028508 s
2048 iterations: 0:00:00.104813 s
4096 iterations: 0:00:00.389232 s
8192 iterations: 0:00:01.552962 s
16384 iterations: 0:00:06.132139 s
32768 iterations: 0:00:25.233547 s
65536 iterations: 0:01:51.921170 s
131072 iterations: 0:07:42.047914 s
Your environment
- CPython versions tested on: HEAD of main (a9d58feccfd956dc99195af6872b06446738d7db)
- Operating system and architecture: Fedora release 36 (Thirty Six)
Linked PRs
- gh-124740
- gh-124745
- gh-124909
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 36k
- Merge trung bình
- 1 ngày 9 giờ
- Pull request đã merge (30 ngày)
- 558
Hướng dẫn đóng góp
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 python/cpython
-
docs pending
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
build type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
stdlib topic-email type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Tất cả issue của python/cpython
Issue tương tự
-
area/auth bug comp/agent P3 platform/discord type/security
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
NousResearch/hermes-agent#117848 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
bancolombia/sentinel#23 ·
-
test md Đang mởCI
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100