https://github.com/python/cpython/commit/23ad9c5d01d6548e9ae1c5c6edd1cd2fabc3217f
commit: 23ad9c5d01d6548e9ae1c5c6edd1cd2fabc3217f
branch: main
author: dgpb <[email protected]>
committer: hauntsaninja <[email protected]>
date: 2025-12-30T07:15:59Z
summary:
gh-142939: difflib.get_close_matches performance (#142940)
files:
A Misc/NEWS.d/next/Library/2025-12-29-21-12-12.gh-issue-142939.OyQQr5.rst
M Lib/difflib.py
diff --git a/Lib/difflib.py b/Lib/difflib.py
index 4a0600e4ebb01b..7c7e233b013a76 100644
--- a/Lib/difflib.py
+++ b/Lib/difflib.py
@@ -638,15 +638,15 @@ def quick_ratio(self):
# avail[x] is the number of times x appears in 'b' less the
# number of times we've seen it in 'a' so far ... kinda
avail = {}
- availhas, matches = avail.__contains__, 0
+ matches = 0
for elt in self.a:
- if availhas(elt):
+ if elt in avail:
numb = avail[elt]
else:
numb = fullbcount.get(elt, 0)
avail[elt] = numb - 1
if numb > 0:
- matches = matches + 1
+ matches += 1
return _calculate_ratio(matches, len(self.a) + len(self.b))
def real_quick_ratio(self):
@@ -702,10 +702,12 @@ def get_close_matches(word, possibilities, n=3,
cutoff=0.6):
s.set_seq2(word)
for x in possibilities:
s.set_seq1(x)
- if s.real_quick_ratio() >= cutoff and \
- s.quick_ratio() >= cutoff and \
- s.ratio() >= cutoff:
- result.append((s.ratio(), x))
+ if s.real_quick_ratio() < cutoff or s.quick_ratio() < cutoff:
+ continue
+
+ ratio = s.ratio()
+ if ratio >= cutoff:
+ result.append((ratio, x))
# Move the best scorers to head of list
result = _nlargest(n, result)
diff --git
a/Misc/NEWS.d/next/Library/2025-12-29-21-12-12.gh-issue-142939.OyQQr5.rst
b/Misc/NEWS.d/next/Library/2025-12-29-21-12-12.gh-issue-142939.OyQQr5.rst
new file mode 100644
index 00000000000000..65523f08dcc0b0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-12-29-21-12-12.gh-issue-142939.OyQQr5.rst
@@ -0,0 +1 @@
+Performance optimisations for :func:`difflib.get_close_matches`
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]