csv.Sniffer.sniff() takes quadratic time on long runs of spaces
Maintainer thường phản hồi trong vòng 1 ngày
@serhiy-storchaka đang làm issue này rồi.
Từ ngày 26/10/2026.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- python
- Lĩnh vực
- performance
Hướng nghiên cứu
Read Lib/csv.py and inspect the regex search in Sniffer._split_dormant() described in the issue. Reproduce the slowdown with the supplied timing sample, then check that the proposed lookbehind preserves matching results while avoiding repeated attempts within a space run. Done means the long-run timings no longer grow quadratically and the existing csv tests pass.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug report
Bug description:
On main, csv.Sniffer().sniff() takes quadratic time when the sample contains a quote character and a long run of spaces that is not followed by one:
import csv, time
for n in (8000, 16000, 32000, 64000):
sample = 'a' + ' ' * n + 'x"\n'
t = time.perf_counter()
try:
csv.Sniffer().sniff(sample)
except csv.Error:
pass
print(n, round(time.perf_counter() - t, 4))
Seconds, on macOS 27.0.1 (Apple M3):
| n | main (debug build) | main + fix (debug build) | 3.15.0 (release, for comparison) |
|---|---|---|---|
| 8000 | 0.050 | 0.011 | 0.010 |
| 16000 | 0.153 | 0.002 | 0.004 |
| 32000 | 0.598 | 0.004 | 0.007 |
| 64000 | 2.373 | 0.008 | 0.015 |
The sample ('a,b' + ' ' * 100_000 + 'c"\n') * 4 takes 23.2 s on main and 0.06 s with the fix.
The cause is the regex in Sniffer._split_dormant() (Lib/csv.py):
m = re.compile(fr'(?:^|([\r\n{cls}]))'
fr' *{quotechar}').search(part, pos)
The space is a delimiter candidate whenever the sample contains one, so it is in cls. The search then tries a match at every space of the run: [...] matches that space, * consumes the rest of the run, and the match fails on the next character. A run of n spaces costs O(n**2).
A match that starts at a space preceded by a space is never the leftmost one: a match with the same group and the same end also starts at the beginning of the run. Adding the lookbehind (?<! ) after the group skips those positions without changing any result.
This was introduced in gh-83273 (25fea6d64e1) and is in 3.16.0a1. 3.15 and older are not affected.
CPython versions tested on:
3.15, CPython main branch
Operating systems tested on:
macOS
Linked PRs
- gh-160557
- Ngôn ngữ chính
- Python
- Star
- 77.7k
- Fork
- 38.3k
- Merge trung bình
- 13 giờ 43 phút
- Pull request đã merge (30 ngày)
- 704
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc 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
-
topic-socket type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
python/cpython#160561 · 7 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
tests topic-profiling type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
python/cpython#160334 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`iso2022_jp_3` encodes U+9B1D but decodes the result as U+9B1CCó thể đã có người làm @kinqsradio đã nhận hôm nay. Đang mởextension-modules type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Remove unused FVS_* macrosĐang mởinterpreter-core type-feature
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
python/cpython#160109 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
extension-modules type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
python/cpython#160001 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của python/cpython
Issue tương tự
-
Web transport: small test gapsĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
scikit-learn-contrib/category_encoders#553 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
tests/strategies_test.py:174 goes uncovered on about 1 run in 100, failing the coverage ratchetĐang mởflaky-watch
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
btclib-org/btclib#2839 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
BarrelList constructor drops false-valued iterable contentsCó thể đã có người làm @Gambit-Checkmate đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 19 ngày
-
[Performance]: Redundant object-storage pull when model and tokenizer share the same RunAI URIĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
vllm-project/vllm#63863 ·
Maintainer thường phản hồi trong vòng 1 ngày