From: dgpb <3577712+dg-pb@users.noreply.github.com> Date: Mon, 23 Feb 2026 01:59:40 +0000 (+0200) Subject: gh-145131: difflib.Differ._fancy_replace performance (re-use ratio) (#145133) X-Git-Tag: v3.15.0a7~179 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=79f6caf8f1f52788e15c97debd822e99672e5181;p=thirdparty%2FPython%2Fcpython.git gh-145131: difflib.Differ._fancy_replace performance (re-use ratio) (#145133) re-use calculated ratio --- diff --git a/Lib/difflib.py b/Lib/difflib.py index 7c7e233b013a..8f3cdaed9564 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -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