Skip to content

gh-160556: Fix quadratic time in csv.Sniffer with long runs of spaces - #160557

Open
devdavidejesus wants to merge 1 commit into
python:mainfrom
devdavidejesus:fix-csv-sniffer-spaces
Open

devdavidejesus wants to merge 1 commit into
python:mainfrom
devdavidejesus:fix-csv-sniffer-spaces

Conversation

@devdavidejesus

@devdavidejesus devdavidejesus commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Sniffer._split_dormant() searched for (?:^|([\r\n{cls}])) *{quotechar}. When the space is a candidate delimiter, the search tried a match at every space of a run and rescanned the rest of the run each time, so a long run of spaces not followed by a quote character took quadratic time.

A match starting at a space preceded by a space is never the leftmost one, because a match with the same group and end starts at the beginning of the run. The lookbehind (?<! ) skips those positions, so the results do not change.

Tested on macOS with a debug build of main: ('a,b' + ' ' * 100_000 + 'c"\n') * 4 (the new test's sample) now takes 0.06 s instead of 23.2 s; test_csv passes, also with -R 3:3.

The regression was introduced in gh-83273 and is in 3.16.0a1.

See #160556.

…spaces

Sniffer._split_dormant() tried a match at every space of a run of
spaces when the space was a candidate delimiter, and rescanned the
rest of the run each time.  A match starting at a space preceded by a
space is never the leftmost one, so skip those positions with a
lookbehind.  The results do not change.

The regression was introduced in pythongh-83273 (25fea6d).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant