Repository navigation
Conversation
…ed '[' Each '[' scanned to the end of the pattern looking for ']'. When there is no ']' after the current position, that scan always fails, and so does the scan for every later '['. Find the last ']' once and treat any '[' after it as a literal without scanning.
kksecure
force-pushed
the
gh-163137-fnmatch-quadratic
branch
from
December 4, 2026 11:19
640b47f to
880c6f2
Compare
eendebakpt
reviewed
Dec 4, 2026
| star_indices = [] | ||
|
|
||
| i, n = 0, len(pat) | ||
| last_close = pat.rfind(']') |
Contributor
There was a problem hiding this comment.
This adds some overhead to the cases where no [ or ']` is involved. Can you benchmark some relevant cases?
eendebakpt
reviewed
Dec 4, 2026
Comment on lines
124
to
126
| while j < n and pat[j] != ']': | ||
| j = j+1 | ||
| if j >= n: |
Contributor
There was a problem hiding this comment.
This part is still a linear search. We could do something like
Suggested change
| j = pat.find(']', j) if may_close else -1 | |
| if j < 0: | |
| # No ']' left: every later '[' is a literal too. | |
| may_close = False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Each
[in a pattern scans to the end of the pattern looking for the closing]. If there is none, the[becomes a literal and the next[scans again, so a pattern of many unclosed brackets ("[!" * n) takes O(n²) time infnmatch.translate()and everything built on it (fnmatch,fnmatchcase,filter,glob.translate(),pathlib.PurePath.full_match()).If there is no
]after the current position, the scan can't succeed, and neither can the scan for any later[. This change finds the last]once and skips the scan when we're past it. The output is unchanged:test_fnmatch,test_globandtest_pathlibpass (1,449 tests).translate()gives identical output before and after on 300,000 random patterns over[]!-^*?ab\.test_translate_unclosed_brackets: about 0.8 s with the fix. Without it, the same test takes 8 min 14 s on a debug build.Timings for
"[!" * non a release build (3.15.2): 32 KB went from 9.7 s to under 0.01 s, and 200 KB now takes 0.03 s.