]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-145131: difflib.Differ._fancy_replace performance (re-use ratio) (#145133)
authordgpb <3577712+dg-pb@users.noreply.github.com>
Mon, 23 Feb 2026 01:59:40 +0000 (03:59 +0200)
committerGitHub <noreply@github.com>
Mon, 23 Feb 2026 01:59:40 +0000 (19:59 -0600)
re-use calculated ratio

Lib/difflib.py

index 7c7e233b013a7648860ab1b1f78bb7b097d899f4..8f3cdaed9564d87e382e12e71d0d785ffff479c6 100644 (file)
@@ -942,10 +942,12 @@ class Differ:
                 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