https://github.com/python/cpython/commit/79f6caf8f1f52788e15c97debd822e99672e5181
commit: 79f6caf8f1f52788e15c97debd822e99672e5181
branch: main
author: dgpb <[email protected]>
committer: tim-one <[email protected]>
date: 2026-02-22T19:59:40-06:00
summary:

gh-145131: difflib.Differ._fancy_replace performance (re-use ratio) (#145133)

re-use calculated ratio

files:
M Lib/difflib.py

diff --git a/Lib/difflib.py b/Lib/difflib.py
index 7c7e233b013a76..8f3cdaed9564d8 100644
--- a/Lib/difflib.py
+++ b/Lib/difflib.py
@@ -942,10 +942,12 @@ def _fancy_replace(self, a, alo, ahi, b, blo, bhi):
                 cruncher.set_seq1(a[i])
                 # Ordering by cheapest to most expensive ratio is very
                 # valuable, most often getting out early.
-                if (crqr() > best_ratio
-                      and cqr() > best_ratio
-                      and cr() > best_ratio):
-                    best_i, best_j, best_ratio = i, j, cr()
+                if crqr() <= best_ratio or cqr() <= best_ratio:
+                    continue
+
+                ratio = cr()
+                if ratio > best_ratio:
+                    best_i, best_j, best_ratio = i, j, ratio
 
             if best_i is None:
                 # found nothing to synch on yet - move to next j

_______________________________________________
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]

Reply via email to